droboticflow/internal/api/annotation_source_queue_test.go
2026-10-17 10:38:53 +08:00

100 lines
3.4 KiB
Go

package api
import (
"context"
"droboticflow/internal/config"
"droboticflow/internal/domain"
"net/http"
"os"
"path/filepath"
"testing"
"time"
)
func TestSourceIndexColdValidationDoesNotFailOrOccupyRevisionWorker(t *testing.T) {
t.Setenv("ANNOTATION_SOURCE_INDEX", "true")
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
t.Setenv("ANNOTATION_PROCESSOR_ROLE", "revision")
f, root := annotationProcessorFixture(t)
slices := []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 4}}
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-confirmations", f.annotator, map[string]any{"version": f.item.Version, "sliceSegments": slices})
if r.Code != 202 {
t.Fatal(r.Code, r.Body.String())
}
ctx, cancel := context.WithCancel(f.ctx)
done := make(chan error, 1)
go func() {
done <- RunAnnotationProcessor(ctx, f.store, config.AppConfig{WorkspaceMountPath: root}, 8, false)
}()
deadline := time.Now().Add(5 * time.Second)
var waiting *domain.HumanItemRecord
for time.Now().Before(deadline) {
h, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if h.ProcessingJob != nil && h.ProcessingJob.Status == "queued" && h.ProcessingJob.Stage == "validating" {
waiting = h
break
}
time.Sleep(10 * time.Millisecond)
}
cancel()
<-done
if waiting == nil {
t.Fatal("cold source was not deferred")
}
if waiting.ProcessingJob.Attempt != 0 || stringValue(mapValue(waiting.Subject.Payload["sliceWorkflow"])["status"]) != "processing" {
t.Fatal("waiting was counted as crash or workflow failure")
}
if job, _ := f.store.ClaimAnnotationProcessing(f.ctx, "other", time.Now().UTC().Format(time.RFC3339Nano), time.Now().Add(time.Minute).UTC().Format(time.RFC3339Nano)); job != nil {
t.Fatal("deferred job hot-looped before retry time")
}
t.Setenv("ANNOTATION_PROCESSOR_ROLE", "all")
ctx, cancel = context.WithCancel(f.ctx)
done = make(chan error, 1)
go func() {
done <- RunAnnotationProcessor(ctx, f.store, config.AppConfig{WorkspaceMountPath: root}, 8, false)
}()
defer func() { cancel(); <-done }()
deadline = time.Now().Add(10 * time.Second)
for time.Now().Before(deadline) {
h, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if h.ProcessingJob.Status == "completed" {
if h.ProcessingJob.Attempt > 1 {
t.Fatal("validation consumed retry budget")
}
return
}
if h.ProcessingJob.Status == "failed" {
t.Fatal(h.ProcessingJob.Error)
}
time.Sleep(20 * time.Millisecond)
}
t.Fatal("deferred task did not resume after validation/restart")
}
func TestSourceIndexLegacyTimelineQueueDoesNotBackfillSourceCopies(t *testing.T) {
t.Setenv("ANNOTATION_SOURCE_INDEX", "true")
t.Setenv("ANNOTATION_PROCESSOR_ROLE", "validation")
f, root := annotationProcessorFixture(t)
ctx, cancel := context.WithCancel(f.ctx)
done := make(chan error, 1)
go func() {
done <- RunAnnotationProcessor(ctx, f.store, config.AppConfig{WorkspaceMountPath: root}, 1, false)
}()
defer func() { cancel(); <-done }()
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
job, err := f.store.GetAnnotationIndexJob(ctx, f.task.ID)
if err != nil {
t.Fatal(err)
}
if job != nil && job.Status == "completed" {
if _, err := os.Stat(filepath.Join(root, "system", "runs", f.task.RunID, "annotation-sources")); !os.IsNotExist(err) {
t.Fatal("legacy timeline job triggered source-copy backfill", err)
}
return
}
time.Sleep(10 * time.Millisecond)
}
t.Fatal("legacy timeline job did not complete")
}