forked from longtao.wu/droboticflow
181 lines
6.8 KiB
Go
181 lines
6.8 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCaptureStatusDoesNotWaitForBOS(t *testing.T) {
|
|
f := newCaptureCutoverFixture(t, "blocked")
|
|
c := f.seed("blocked", domain.CaptureRecordLaunching, "run-blocked")
|
|
r := domain.RunRecord{ID: c.RunID, TaskID: c.ID, OrganizationID: c.OrganizationID, ProjectID: c.ProjectID, WorkflowID: c.WorkflowID, Status: domain.RunStatusWaitingForHuman}
|
|
if err := f.db.UpsertRun(f.ctx, r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entered, release := make(chan struct{}), make(chan struct{})
|
|
f.beforeList = func() { close(entered); <-release }
|
|
scanDone := make(chan error, 1)
|
|
go func() { scanDone <- f.runtime.processCaptureAutomation(f.ctx, f.old) }()
|
|
defer func() { close(release); <-scanDone }()
|
|
select {
|
|
case <-entered:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("scan did not reach BOS")
|
|
}
|
|
if err := f.runtime.reconcileCaptureStatuses(f.ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
saved, _ := f.db.GetCaptureRecord(f.ctx, c.ID)
|
|
if saved.Status != domain.CaptureRecordWaiting || saved.WorkflowID != c.WorkflowID || saved.RunID != c.RunID || len(saved.SourceKeys) == 0 {
|
|
t.Fatal("status not synchronized or source lost")
|
|
}
|
|
}
|
|
func TestCaptureStatusPagesProgressPastUnresolvedClaims(t *testing.T) {
|
|
f := newCaptureCutoverFixture(t)
|
|
for i := 0; i < 56; i++ {
|
|
c := domain.CaptureRecord{ID: fmt.Sprintf("c-%03d", i), OrganizationID: f.old.OrganizationID, ProjectID: f.old.ProjectID, WorkflowID: f.old.ID, Status: domain.CaptureRecordLaunching, UpdatedAt: "before"}
|
|
if i > 0 {
|
|
c.RunID = fmt.Sprintf("r-%03d", i)
|
|
if e := f.db.UpsertRun(f.ctx, domain.RunRecord{ID: c.RunID, TaskID: c.ID, WorkflowID: c.WorkflowID, ProjectID: c.ProjectID, OrganizationID: c.OrganizationID, Status: domain.RunStatusWaitingForHuman}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
if e := f.db.UpsertCaptureRecord(f.ctx, c); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
if e := f.runtime.reconcileCaptureStatuses(context.Background()); e == nil {
|
|
t.Fatal("unresolved claim hidden")
|
|
}
|
|
c, _ := f.db.GetCaptureRecord(f.ctx, "c-049")
|
|
if c.Status != domain.CaptureRecordWaiting {
|
|
t.Fatal("first page starved")
|
|
}
|
|
c, _ = f.db.GetCaptureRecord(f.ctx, "c-050")
|
|
if c.Status != domain.CaptureRecordLaunching {
|
|
t.Fatal("page exceeded bound")
|
|
}
|
|
if e := f.runtime.reconcileCaptureStatuses(f.ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
c, _ = f.db.GetCaptureRecord(f.ctx, "c-055")
|
|
if c.Status != domain.CaptureRecordWaiting || f.runtime.captureStatusCursor != "" {
|
|
t.Fatal("second page missing")
|
|
}
|
|
c, _ = f.db.GetCaptureRecord(f.ctx, "c-000")
|
|
if c.RunID != "" || c.Status != domain.CaptureRecordLaunching {
|
|
t.Fatal("unknown claim relaunched")
|
|
}
|
|
}
|
|
|
|
func TestCaptureStatusConditionalWritePreservesNewerDecision(t *testing.T) {
|
|
f := newCaptureCutoverFixture(t, "race")
|
|
before := f.seed("race", domain.CaptureRecordRunning, "race-run")
|
|
changed := before
|
|
changed.Status = domain.CaptureRecordDiscarded
|
|
changed.UpdatedAt = "concurrent-decision"
|
|
if err := f.db.UpsertCaptureRecord(f.ctx, changed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
next := before
|
|
next.Status = domain.CaptureRecordWaiting
|
|
next.UpdatedAt = "stale-result"
|
|
ok, err := f.db.UpdateCaptureRunStatus(f.ctx, before, next)
|
|
if err != nil || ok {
|
|
t.Fatal("concurrent decision overwritten")
|
|
}
|
|
saved, _ := f.db.GetCaptureRecord(f.ctx, before.ID)
|
|
if saved.Status != domain.CaptureRecordDiscarded || len(saved.SourceKeys) == 0 {
|
|
t.Fatal("decision or source lost")
|
|
}
|
|
}
|
|
|
|
type slowCaptureLookup struct {
|
|
store.Store
|
|
slowID string
|
|
}
|
|
|
|
func (s *slowCaptureLookup) GetRunByWorkflowTaskID(ctx context.Context, w, id string) (*domain.RunRecord, error) {
|
|
if id == s.slowID {
|
|
<-ctx.Done()
|
|
return nil, ctx.Err()
|
|
}
|
|
return s.Store.GetRunByWorkflowTaskID(ctx, w, id)
|
|
}
|
|
func TestCaptureStatusTimeoutDoesNotStarveFollowingRecords(t *testing.T) {
|
|
f := newCaptureCutoverFixture(t)
|
|
for i := 0; i < 2; i++ {
|
|
c := domain.CaptureRecord{ID: fmt.Sprintf("bounded-%d", i), WorkflowID: f.old.ID, ProjectID: f.old.ProjectID, OrganizationID: f.old.OrganizationID, Status: domain.CaptureRecordLaunching, RunID: fmt.Sprintf("run-%d", i)}
|
|
if e := f.db.UpsertCaptureRecord(f.ctx, c); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := f.db.UpsertRun(f.ctx, domain.RunRecord{ID: c.RunID, TaskID: c.ID, WorkflowID: c.WorkflowID, ProjectID: c.ProjectID, OrganizationID: c.OrganizationID, Status: domain.RunStatusWaitingForHuman}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
f.runtime.store = &slowCaptureLookup{Store: f.db, slowID: "bounded-0"}
|
|
ctx, cancel := context.WithTimeout(f.ctx, 10*time.Millisecond)
|
|
defer cancel()
|
|
if e := f.runtime.reconcileCaptureStatuses(ctx); e == nil {
|
|
t.Fatal("timeout swallowed")
|
|
}
|
|
if f.runtime.captureStatusCursor != "bounded-0" {
|
|
t.Fatal("timed-out record will starve queue")
|
|
}
|
|
if e := f.runtime.reconcileCaptureStatuses(f.ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
c, _ := f.db.GetCaptureRecord(f.ctx, "bounded-1")
|
|
if c.Status != domain.CaptureRecordWaiting {
|
|
t.Fatal("following record starved")
|
|
}
|
|
}
|
|
func TestCaptureStatusRejectsCrossProjectRun(t *testing.T) {
|
|
f := newCaptureCutoverFixture(t, "scope")
|
|
c := f.seed("scope", domain.CaptureRecordLaunching, "scope-run")
|
|
if e := f.db.UpsertRun(f.ctx, domain.RunRecord{ID: c.RunID, TaskID: c.ID, WorkflowID: c.WorkflowID, ProjectID: "another-project", OrganizationID: c.OrganizationID, Status: domain.RunStatusWaitingForHuman}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := f.runtime.reconcileCaptureStatuses(f.ctx); e == nil {
|
|
t.Fatal("wrong project accepted")
|
|
}
|
|
saved, _ := f.db.GetCaptureRecord(f.ctx, c.ID)
|
|
if saved.Status != domain.CaptureRecordLaunching {
|
|
t.Fatal("scope mismatch changed status")
|
|
}
|
|
}
|
|
|
|
func TestCaptureStatusConcurrentCreateResponseKeepsNewerState(t *testing.T) {
|
|
f := newCaptureCutoverFixture(t, "create-race")
|
|
claim := f.seed("create-race", domain.CaptureRecordReady, "")
|
|
f.runtime.store = &captureCutoverFaultStore{Store: f.db, createRun: func(ctx context.Context, id string, trigger domain.RunTrigger, inputs map[string]domain.ArtifactPayload, taskIDs ...string) (*domain.RunRecord, error) {
|
|
run, err := f.db.CreateRunWithInputs(ctx, id, trigger, inputs, taskIDs...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
run.Status = domain.RunStatusWaitingForHuman
|
|
if err := f.db.UpsertRun(ctx, *run); err != nil {
|
|
return nil, err
|
|
}
|
|
current, err := f.db.GetCaptureRecord(ctx, claim.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := f.runtime.reconcileCaptureRun(ctx, current); err != nil {
|
|
return nil, err
|
|
}
|
|
return run, nil
|
|
}}
|
|
if err := f.runtime.launchCaptureRun(f.ctx, f.old, normalizedCaptureAutomation(f.old.CaptureAutomation), &claim); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
saved, _ := f.db.GetCaptureRecord(f.ctx, claim.ID)
|
|
if saved.Status != domain.CaptureRecordWaiting || claim.Status != domain.CaptureRecordWaiting || saved.RunID == "" {
|
|
t.Fatal("create response overwrote newer state")
|
|
}
|
|
}
|