feat(tests): enhance image multimodal service tests and runtime task handling

Added new tests for the ImageMultimodalService to verify the handling of orphan tasks and the finalization of pending counters. Improved runtime task inspection with better error handling and task cancellation logic, ensuring robust management of orphan tasks in the system.
This commit is contained in:
wizardchen
2026-07-14 15:29:35 +08:00
committed by lyingbug
parent 4ae48d6acf
commit f409bd756b
4 changed files with 144 additions and 12 deletions
@@ -151,6 +151,9 @@ func (s *ImageMultimodalService) Handle(ctx context.Context, task *asynq.Task) e
logger.Infof(ctx,
"[ImageMultimodal] Dropping task chunk=%s knowledge=%s kb=%s image=%s",
payload.ChunkID, payload.KnowledgeID, payload.KnowledgeBaseID, payload.ImageURL)
// Still count this image toward the parent finalize gate so a batch
// of dropped orphans cannot strand multimodal:pending forever.
s.checkAndFinalizeAllImages(ctx, payload)
return nil
}
@@ -9,7 +9,9 @@ import (
"github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/alicebob/miniredis/v2"
"github.com/hibiken/asynq"
"github.com/redis/go-redis/v9"
)
type orphanKnowledgeRepo struct {
@@ -96,6 +98,58 @@ func TestImageMultimodalHandleDropsMissingKnowledge(t *testing.T) {
}
}
type orphanTaskEnqueuer struct {
interfaces.TaskEnqueuer
enqueued []*asynq.Task
}
func (e *orphanTaskEnqueuer) Enqueue(task *asynq.Task, _ ...asynq.Option) (*asynq.TaskInfo, error) {
e.enqueued = append(e.enqueued, task)
return &asynq.TaskInfo{ID: "post-process"}, nil
}
func TestImageMultimodalHandleDropFinalizesPendingCounter(t *testing.T) {
t.Parallel()
mr, err := miniredis.Run()
if err != nil {
t.Fatalf("start miniredis: %v", err)
}
t.Cleanup(mr.Close)
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
t.Cleanup(func() { _ = rdb.Close() })
enqueuer := &orphanTaskEnqueuer{}
svc := &ImageMultimodalService{
knowledgeRepo: &orphanKnowledgeRepo{err: repository.ErrKnowledgeNotFound},
kbService: &orphanKBService{kb: &types.KnowledgeBase{ID: "kb-1"}},
redisClient: rdb,
taskEnqueuer: enqueuer,
}
const knowledgeID = "missing"
redisKey := "multimodal:pending:" + knowledgeID
if err := rdb.Set(context.Background(), redisKey, 1, 0).Err(); err != nil {
t.Fatal(err)
}
payload, err := json.Marshal(types.ImageMultimodalPayload{
TenantID: 1,
KnowledgeID: knowledgeID,
KnowledgeBaseID: "kb-1",
ImageURL: "minio://bucket/img.png",
})
if err != nil {
t.Fatal(err)
}
if err := svc.Handle(context.Background(), asynq.NewTask(types.TypeImageMultimodal, payload)); err != nil {
t.Fatalf("orphan drop should succeed: %v", err)
}
if mr.Exists(redisKey) {
t.Fatal("pending counter should be cleared after drop finalize")
}
if len(enqueuer.enqueued) != 1 || enqueuer.enqueued[0].Type() != types.TypeKnowledgePostProcess {
t.Fatalf("expected post-process enqueue, got %d tasks", len(enqueuer.enqueued))
}
}
func TestImageMultimodalHandlePropagatesTransientKnowledgeError(t *testing.T) {
t.Parallel()
dbErr := errors.New("db unavailable")