WeKnora/internal/infrastructure/docparser/cloud_poll_timeout_test.go

163 lines
5.1 KiB
Go
Raw Normal View History

fix(docparser): 云解析超时改为跟随既有配置,不再硬编码 (#3891) * fix(docparser): 云解析轮询上限接入配置,去掉两处硬编码超时 PaddleOCR-VL Cloud 与 WeKnoraCloud 两个 reader 的整批轮询上限仍是写死的: paddleocr_vl_cloud_converter.go 的 paddleOCRVLCloudTimeout(600s)直接决定了 pollJob 的 deadline,reader 结构体没有任何 timeout 字段;weknoracloud_http_reader.go 的 pollTimeout 固定 20 分钟。 同族的另外两个 reader 早就可以配置:MinerU Cloud 走 WEKNORA_MINERU_CLOUD_TIMEOUT(mineru_cloud_converter.go),自建 PaddleOCR-VL 走 WEKNORA_PADDLEOCR_VL_TIMEOUT(paddleocr_vl_converter.go)。后果是运维把 WEKNORA_DOCREADER_CALL_TIMEOUT(默认 30m)调大也无效:WeKnoraCloud 的 20 分钟 比它还短,硬编码上限先触发,大体积扫描件固定在第 10/20 分钟失败。 改动: - PaddleOCRVLCloudReader 增加 timeout / pollInterval 字段(与 MinerUCloudReader 同形),构造函数用 requestTimeoutFromEnv 读 WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT (默认仍是 600s),pollJob 用 c.timeout / c.pollInterval;超时文案对齐 MinerU 的 "task timed out after %s (%d polls)"(旧的 "after %d polls" 是 27b0c84b 已经 替 MinerU 换掉的写法)。 - WeKnoraCloudSignedDocumentReader 的 pollTimeout 改读 WEKNORA_WEKNORACLOUD_TIMEOUT(默认仍是 20m)。 - .env.example 补两条说明,位置紧跟 WEKNORA_MINERU_CLOUD_TIMEOUT。 新增用例:两个 reader 的 env→超时值映射(含空/非法/0/负数回退默认)、超时真的会 结束一个厂商持续报 running 的任务(假服务器与假 Transport,带看门狗,回归时 5 秒 内失败而不是轮询 600 秒)。只回退接线(保留字段)时四条用例在 10 秒内全部变红: PaddleOCR-VL Cloud 的 200ms 配置被忽略、WeKnoraCloud 读回 20m 而非配置值; 把两个源文件整体回退则用例引用的新配置面不存在,包测试无法编译。 * docs(docparser): document PaddleOCR-VL Cloud / WeKnoraCloud poll timeouts Add the two new env vars to both website-docs timeout tables and pass them through docker-compose.yml next to WEKNORA_MINERU_CLOUD_TIMEOUT. * fix(docparser): WeKnoraCloud 轮询上限在调用方已有 deadline 时也生效 入库和临时文档调用 pollTaskResult 时 ctx 都带有 DocReader 调用超时, 原先的 !ok 条件让 WEKNORA_WEKNORACLOUD_TIMEOUT 在生产路径上被跳过。 改为始终叠加 WithTimeout,context 自动取内外较早的 deadline; 回归测试补上父 ctx 带更长 deadline 的场景。 --------- Co-authored-by: linus-liu-web <11640009+linus-liu-web@users.noreply.github.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-10-08 06:02:42 +00:00
package docparser
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPaddleOCRVLCloudTimeoutConfig(t *testing.T) {
for _, tt := range []struct {
name, value string
want time.Duration
}{
{"empty", "", paddleOCRVLCloudTimeout},
{"whitespace", " ", paddleOCRVLCloudTimeout},
{"minutes", "90m", 90 * time.Minute},
{"trimmed", " 5400s ", 90 * time.Minute},
{"invalid", "invalid", paddleOCRVLCloudTimeout},
{"unitless", "5400", paddleOCRVLCloudTimeout},
{"zero", "0s", paddleOCRVLCloudTimeout},
{"negative", "-1s", paddleOCRVLCloudTimeout},
} {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT", tt.value)
assert.Equal(t, tt.want, NewPaddleOCRVLCloudReader(nil).timeout)
})
}
}
// The configured timeout, not the old hardcoded 600s, must end a job that the
// cloud keeps reporting as running.
func TestPaddleOCRVLCloudPollStopsAtConfiguredTimeout(t *testing.T) {
allowLoopbackSSRF(t)
t.Setenv("WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT", "200ms")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{"code":0,"data":{"state":"running"}}`)
}))
defer server.Close()
reader := NewPaddleOCRVLCloudReader(nil)
reader.baseURL = server.URL
reader.pollInterval = 10 * time.Millisecond
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Watchdog: without the configured budget this loop keeps polling for the
// old hardcoded 600s, which must fail the test instead of hanging it.
done := make(chan error, 1)
start := time.Now()
go func() {
_, pollErr := reader.pollJob(ctx, "job-1")
done <- pollErr
}()
select {
case err := <-done:
require.Error(t, err)
assert.Contains(t, err.Error(), fmt.Sprintf("timed out after %s", 200*time.Millisecond))
assert.Less(t, time.Since(start), 5*time.Second)
case <-time.After(5 * time.Second):
t.Fatalf("poll loop ignored WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT")
}
}
func TestWeKnoraCloudPollTimeoutConfig(t *testing.T) {
for _, tt := range []struct {
name, value string
want time.Duration
}{
{"empty", "", defaultWeKnoraCloudPollTimeout},
{"minutes", "90m", 90 * time.Minute},
{"trimmed", " 3600s ", time.Hour},
{"invalid", "invalid", defaultWeKnoraCloudPollTimeout},
{"zero", "0s", defaultWeKnoraCloudPollTimeout},
{"negative", "-1s", defaultWeKnoraCloudPollTimeout},
} {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("WEKNORA_WEKNORACLOUD_TIMEOUT", tt.value)
reader, err := NewWeKnoraCloudSignedDocumentReader("app-id", "api-key")
require.NoError(t, err)
assert.Equal(t, tt.want, reader.pollTimeout)
})
}
}
// weKnoraCloudPollTransport answers every poll with "still running" so the
// configured poll budget is the only thing that can end the loop.
type weKnoraCloudPollTransport struct {
polls atomic.Int64
}
func (t *weKnoraCloudPollTransport) RoundTrip(req *http.Request) (*http.Response, error) {
t.polls.Add(1)
body := `{"task_id":"task-1","status":"processing","progress":0.1}`
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(strings.NewReader(body)),
Request: req,
}, nil
}
// The configured budget, not the old hardcoded 20m, must end the poll loop
// whether or not the caller has a deadline of its own. Ingestion always calls
// with a (much longer) DocReader deadline, so that case must not bypass it.
func TestWeKnoraCloudPollStopsAtConfiguredTimeout(t *testing.T) {
for _, tt := range []struct {
name string
parentCtx func() (context.Context, context.CancelFunc)
}{
{"no parent deadline", func() (context.Context, context.CancelFunc) {
return context.WithCancel(context.Background())
}},
{"longer parent deadline", func() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), time.Minute)
}},
} {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("WEKNORA_WEKNORACLOUD_TIMEOUT", "300ms")
reader, err := NewWeKnoraCloudSignedDocumentReader("app-id", "api-key")
require.NoError(t, err)
transport := &weKnoraCloudPollTransport{}
reader.client = &http.Client{Transport: transport}
reader.initialPollInterval = 5 * time.Millisecond
reader.maxPollInterval = 10 * time.Millisecond
ctx, cancel := tt.parentCtx()
defer cancel()
done := make(chan error, 1)
start := time.Now()
go func() {
_, pollErr := reader.pollTaskResult(ctx, "task-1")
done <- pollErr
}()
select {
case pollErr := <-done:
require.Error(t, pollErr)
assert.True(t, errors.Is(pollErr, context.DeadlineExceeded), "err = %v", pollErr)
assert.Less(t, time.Since(start), 5*time.Second)
assert.Greater(t, transport.polls.Load(), int64(1),
"the job must actually be polled before the budget ends it")
case <-time.After(5 * time.Second):
t.Fatalf("poll loop ignored WEKNORA_WEKNORACLOUD_TIMEOUT after %d polls", transport.polls.Load())
}
})
}
}