forked from longtao.wu/droboticflow
125 lines
4.5 KiB
Go
125 lines
4.5 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 TestSourceIndexLegacyTimelineQueueRequiresCompleteSourceProof(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")); err != nil || job.SourceFingerprint == "" {
|
|
t.Fatal("queued legacy job published no complete source proof", err)
|
|
}
|
|
return
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
t.Fatal("legacy timeline job did not complete")
|
|
}
|
|
|
|
func TestSourceValidationWaitBacksOffAndIsBounded(t *testing.T) {
|
|
start := time.Date(2026, 10, 18, 12, 0, 0, 0, time.UTC)
|
|
j := domain.AnnotationProcessingJob{Attempt: 1}
|
|
want := []time.Duration{2 * time.Second, 4 * time.Second, 8 * time.Second, 16 * time.Second, 32 * time.Second, time.Minute, time.Minute}
|
|
for i, delay := range want {
|
|
now := start.Add(time.Duration(i) * time.Minute)
|
|
if !deferForSourceValidation(&j, now) {
|
|
t.Fatalf("wait %d failed before the limit", i+1)
|
|
}
|
|
next, err := time.Parse(time.RFC3339Nano, j.NextAttemptAt)
|
|
if err != nil || next.Sub(now) != delay {
|
|
t.Fatalf("wait %d next attempt in %v, want %v", i+1, next.Sub(now), delay)
|
|
}
|
|
if j.Status != "queued" || j.Stage != "validating" || j.Attempt != 0 {
|
|
t.Fatalf("wait %d job = %+v, want queued/validating without a consumed attempt", i+1, j)
|
|
}
|
|
}
|
|
if j.ValidationSince != start.Format(time.RFC3339Nano) {
|
|
t.Fatalf("wait start moved to %q", j.ValidationSince)
|
|
}
|
|
if deferForSourceValidation(&j, start.Add(sourceValidationWaitLimit)) {
|
|
t.Fatal("source validation wait was not bounded")
|
|
}
|
|
}
|