81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAnnotationOutboxBSONAndExclusiveClaims(t *testing.T) {
|
|
ctx := context.Background()
|
|
s := NewMemoryStore()
|
|
j := domain.AnnotationProcessingJob{ID: "job", TaskID: "task", Status: "queued", Input: map[string]any{"sliceSegments": []map[string]any{{"id": "slice", "start": 0.0, "end": 1.0}}}}
|
|
encoded, e := bson.Marshal(j)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var restored domain.AnnotationProcessingJob
|
|
if e = bson.Unmarshal(encoded, &restored); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
body, _ := json.Marshal(restored.Input)
|
|
if strings.Contains(string(body), "Key") || !strings.Contains(string(body), "sliceSegments") {
|
|
t.Fatal("BSON changed queued input", string(body))
|
|
}
|
|
h := domain.HumanItemRecord{ID: "item", OrganizationID: "org", Version: 3, ProcessingJob: &restored}
|
|
s.UpsertHumanItem(ctx, h)
|
|
var claims atomic.Int32
|
|
var wg sync.WaitGroup
|
|
now := time.Now().UTC()
|
|
for n := 0; n < 16; n++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
h, e := s.ClaimAnnotationProcessing(ctx, domain.NewID("worker"), now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
if e != nil {
|
|
t.Error(e)
|
|
}
|
|
if h != nil {
|
|
claims.Add(1)
|
|
if len(h.ProcessingJob.Input) == 0 {
|
|
t.Error("private input lost in store clone")
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if claims.Load() != 1 {
|
|
t.Fatal("duplicate claims", claims.Load())
|
|
}
|
|
}
|
|
|
|
func TestWorkerSnapshotCannotErasePublishedAnnotationArtifacts(t *testing.T) {
|
|
s := NewMemoryStore()
|
|
ctx := context.Background()
|
|
r := domain.RunRecord{ID: "run", OrganizationID: "org", NodeExecutions: []domain.NodeExecutionRecord{{NodeID: "human"}}}
|
|
s.UpsertRun(ctx, r)
|
|
a := domain.ExecutionArtifact{ID: "media", RunID: r.ID, NodeID: "human", PortID: "materializedSliceDirectory"}
|
|
if e := s.AppendAnnotationArtifact(ctx, "org", "human", a); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
r.Status = domain.RunStatusWaitingForHuman
|
|
s.UpsertRun(ctx, r)
|
|
fresh, _ := s.GetRun(ctx, r.ID)
|
|
if len(fresh.Artifacts) != 1 || fresh.Artifacts[0].ID != a.ID {
|
|
t.Fatal("stale worker erased media")
|
|
}
|
|
if e := s.AppendAnnotationArtifact(ctx, "org", "human", a); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
fresh, _ = s.GetRun(ctx, r.ID)
|
|
if len(fresh.Artifacts) != 1 {
|
|
t.Fatal("duplicate artifact")
|
|
}
|
|
}
|