carlos / platform Public

mediatest: model ECS eventual consistency in the fake engine

Whole-branch review round 2, Astra P1: the fake listed every retained
task at once, so the simulation could not show an empty ECS list
hiding a task. The fake gains RunAmbiguousCreatedLate, whose task is
created Lag.Create after the call returns, a list lag (Lag.Hidden)
during which ListByStartedBy does not show a new task, Hide for a
status change the lists have not caught up with, and Hidden, the
oracle a list's empty answer is checked against. Describe by ARN and
Tasks are not delayed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 29c4f287ddef031099063dcec808ec163fc5755c parent e316d46
2 files changed, +190 −12
  • internal/media/mediatest/engine.go +111 −12
  • internal/media/mediatest/fakes_test.go +79 −0
diff --git a/internal/media/mediatest/engine.go b/internal/media/mediatest/engine.go
index f9a20e4c4..a3f181bd1 100644
--- a/internal/media/mediatest/engine.go
+++ b/internal/media/mediatest/engine.go
@@ -27,6 +27,10 @@ const (
RunAmbiguousCreated
// RunAmbiguousNotCreated creates nothing and answers a transport error.
RunAmbiguousNotCreated
+ // RunAmbiguousCreatedLate answers a transport error with nothing
+ // created yet; the task is created Lag.Create after the call, as when
+ // ECS finishes a request after its client gave up.
+ RunAmbiguousCreatedLate
)
func (o RunOutcome) String() string {
@@ -41,6 +45,8 @@ func (o RunOutcome) String() string {
return "ambiguous-created"
case RunAmbiguousNotCreated:
return "ambiguous-not-created"
+ case RunAmbiguousCreatedLate:
+ return "ambiguous-created-late"
}
return fmt.Sprintf("outcome(%d)", int(o))
}
@@ -49,13 +55,37 @@ func (o RunOutcome) String() string {
var ErrTransport = errors.New("mediatest: transport error")
type fakeTask struct {
- t media.Task
- req media.RunRequest
- bound time.Time // set at Start: when the deadline container has ended the task at the latest
+ t media.Task
+ req media.RunRequest
+ bound time.Time // set at Start: when the deadline container has ended the task at the latest
+ visible time.Time // ListByStartedBy finds the task only from then on
+}
+
+// Lag is how ECS's eventual consistency shows the tasks of later Runs.
+type Lag struct {
+ // Create is how long after a RunAmbiguousCreatedLate call its task is
+ // created.
+ Create time.Duration
+ // Hidden is how long after its creation a task is absent from
+ // ListByStartedBy (ECS's lists). Describe by ARN is not delayed.
+ Hidden time.Duration
+}
+
+// lateTask is a task an ambiguous Run will create at at.
+type lateTask struct {
+ req media.RunRequest
+ at time.Time
+ hidden time.Duration
}
// FakeEngine is an in-memory media.Engine, safe for concurrent use.
//
+// ListByStartedBy answers as ECS's lists eventually do: a task is absent
+// from it until its creation plus the Lag.Hidden in force at its Run, or
+// until Hide's instant, while Tasks shows every task created. A task of a
+// RunAmbiguousCreatedLate Run is created once the clock reaches the call
+// plus Lag.Create.
+//
// Tasks stay PENDING until Start, then run until Finish, DeadlineFires,
// FailStart or Stop. Once the clock (Now) passes a running task's bound,
// start + DeadlineSleep(start, deadline, grant expiry) + StopGrace, the
@@ -74,6 +104,8 @@ type FakeEngine struct {
execOrder []string
misconfig map[media.ClassKey]string
revision map[media.ClassKey]int
+ lag Lag
+ late []lateTask
}
// NewFakeEngine is a FakeEngine on the clock now.
@@ -97,11 +129,21 @@ func (f *FakeEngine) now() time.Time {
return f.Now()
}
-// settle ends every running task whose bound the clock has passed. The
-// caller holds mu.
+// settle creates every late task whose instant the clock has reached and
+// ends every running task whose bound the clock has passed. The caller
+// holds mu.
func (f *FakeEngine) settle() {
f.init()
now := f.now()
+ kept := f.late[:0]
+ for _, l := range f.late {
+ if now.Before(l.at) {
+ kept = append(kept, l)
+ continue
+ }
+ f.create(l.req, l.at, l.hidden)
+ }
+ f.late = kept
for _, arn := range f.order {
ft := f.tasks[arn]
if ft == nil || ft.t.Stopped || ft.t.StartedAt.IsZero() {
@@ -126,6 +168,61 @@ func (f *FakeEngine) deadlineFires(ft *fakeTask, at time.Time) {
ft.t.StopCode = "EssentialContainerExited"
}
+// create makes a PENDING task for req at at, absent from the lists for
+// hidden. The caller holds mu.
+func (f *FakeEngine) create(req media.RunRequest, at time.Time, hidden time.Duration) string {
+ f.seq++
+ arn := fmt.Sprintf("arn:aws:ecs:test:task/media/%06d", f.seq)
+ f.tasks[arn] = &fakeTask{t: media.Task{ARN: arn, StartedBy: req.Execution}, req: req, visible: at.Add(hidden)}
+ f.order = append(f.order, arn)
+ return arn
+}
+
+// SetLag sets the lags of the tasks of later Runs.
+func (f *FakeEngine) SetLag(l Lag) {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ f.init()
+ f.lag = l
+}
+
+// Hide keeps the task of arn out of ListByStartedBy until until, as a
+// status change ECS's lists have not caught up with yet.
+func (f *FakeEngine) Hide(arn string, until time.Time) error {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ f.settle()
+ ft := f.tasks[arn]
+ if ft == nil {
+ return fmt.Errorf("mediatest: hide %s: %w", arn, media.ErrNotFound)
+ }
+ if until.After(ft.visible) {
+ ft.visible = until
+ }
+ return nil
+}
+
+// Hidden reports whether execution has a task ListByStartedBy does not
+// show yet, or one an ambiguous Run has still to create: the oracle a
+// list's answer is checked against.
+func (f *FakeEngine) Hidden(execution string) bool {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ f.settle()
+ for _, l := range f.late {
+ if l.req.Execution == execution {
+ return true
+ }
+ }
+ now := f.now()
+ for _, ft := range f.tasks {
+ if ft.t.StartedBy == execution && now.Before(ft.visible) {
+ return true
+ }
+ }
+ return false
+}
+
// Script queues the answers of the next Runs, in order. With the queue
// empty, Run answers RunOK.
func (f *FakeEngine) Script(outcomes ...RunOutcome) {
@@ -155,11 +252,11 @@ func (f *FakeEngine) Run(ctx context.Context, req media.RunRequest) (string, err
return "", &media.Rejected{Reason: "invalid request"}
case RunAmbiguousNotCreated:
return "", ErrTransport
+ case RunAmbiguousCreatedLate:
+ f.late = append(f.late, lateTask{req: req, at: f.now().Add(f.lag.Create), hidden: f.lag.Hidden})
+ return "", ErrTransport
}
- f.seq++
- arn := fmt.Sprintf("arn:aws:ecs:test:task/media/%06d", f.seq)
- f.tasks[arn] = &fakeTask{t: media.Task{ARN: arn, StartedBy: req.Execution}, req: req}
- f.order = append(f.order, arn)
+ arn := f.create(req, f.now(), f.lag.Hidden)
if out == RunAmbiguousCreated {
return "", ErrTransport
}
@@ -184,13 +281,14 @@ func (f *FakeEngine) Describe(ctx context.Context, arns []string) ([]media.Task,
}
// ListByStartedBy answers the task started for execution, if its record
-// remains.
+// remains and the lists show it yet.
func (f *FakeEngine) ListByStartedBy(ctx context.Context, execution string) (media.Task, bool, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.settle()
+ now := f.now()
for i := len(f.order) - 1; i >= 0; i-- {
- if ft := f.tasks[f.order[i]]; ft != nil && ft.t.StartedBy == execution {
+ if ft := f.tasks[f.order[i]]; ft != nil && ft.t.StartedBy == execution && !now.Before(ft.visible) {
return copyTask(ft.t), true, nil
}
}
@@ -341,7 +439,8 @@ func (f *FakeEngine) Requests(execution string) []media.RunRequest {
return append([]media.RunRequest(nil), f.requests[execution]...)
}
-// Tasks are every task record the fake still has, in creation order.
+// Tasks are every task record the fake still has, in creation order,
+// hidden from the lists or not.
func (f *FakeEngine) Tasks() []media.Task {
f.mu.Lock()
defer f.mu.Unlock()
diff --git a/internal/media/mediatest/fakes_test.go b/internal/media/mediatest/fakes_test.go
index 52a393871..7045cc13a 100644
--- a/internal/media/mediatest/fakes_test.go
+++ b/internal/media/mediatest/fakes_test.go
@@ -128,6 +128,85 @@ func TestFakeEngineDeadlineBound(t *testing.T) {
}
}
+// TestFakeEngineEventualConsistency (whole-branch review round 2, A5): an
+// ambiguous Run's task can be created after the call returns, and a task
+// can be absent from ListByStartedBy for a while after it exists, while
+// Tasks, the oracle, shows every task created.
+func TestFakeEngineEventualConsistency(t *testing.T) {
+ now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC)
+ f := NewFakeEngine(func() time.Time { return now })
+ k := media.ClassKey{Preset: media.PresetVideo720p, Class: media.ClassShort}
+ req := func(x string) media.RunRequest {
+ return media.RunRequest{Execution: x, Class: k, DeadlineS: 900, GrantExpiry: now.Add(time.Hour)}
+ }
+ listed := func(x string) bool {
+ _, ok, err := f.ListByStartedBy(bg, x)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return ok
+ }
+
+ // Created late, then hidden: nothing at all until Create, then a task
+ // the lists do not show until Create + Hidden.
+ f.SetLag(Lag{Create: 3 * time.Minute, Hidden: 2 * time.Minute})
+ f.Script(RunAmbiguousCreatedLate)
+ if arn, err := f.Run(bg, req("late")); arn != "" || !errors.Is(err, ErrTransport) {
+ t.Fatalf("ambiguous created late: %q %v", arn, err)
+ }
+ if n := len(f.Tasks()); n != 0 || listed("late") || !f.Hidden("late") {
+ t.Fatalf("at the call: %d tasks, listed %v, hidden %v", n, listed("late"), f.Hidden("late"))
+ }
+ now = now.Add(3*time.Minute - time.Second)
+ if n := len(f.Tasks()); n != 0 {
+ t.Fatalf("%d tasks before Create", n)
+ }
+ now = now.Add(time.Second)
+ if n := len(f.Tasks()); n != 1 || listed("late") || !f.Hidden("late") {
+ t.Fatalf("at Create: %d tasks, listed %v", n, listed("late"))
+ }
+ now = now.Add(2 * time.Minute)
+ if !listed("late") || f.Hidden("late") {
+ t.Fatal("not listed once Hidden passed")
+ }
+
+ // An ordinary Run's task is hidden from the lists for Hidden, but
+ // Describe by its ARN answers at once.
+ arn, err := f.Run(bg, req("ok"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if listed("ok") {
+ t.Fatal("listed inside Hidden")
+ }
+ if found, _, _ := f.Describe(bg, []string{arn}); len(found) != 1 {
+ t.Fatal("Describe by ARN waited for the lists")
+ }
+
+ // Hide holds a listed task back again, and never shortens a lag.
+ f.SetLag(Lag{})
+ now = now.Add(2 * time.Minute)
+ if !listed("ok") {
+ t.Fatal("not listed after its lag")
+ }
+ if err := f.Hide(arn, now.Add(time.Minute)); err != nil {
+ t.Fatal(err)
+ }
+ if listed("ok") {
+ t.Fatal("listed while hidden")
+ }
+ if err := f.Hide(arn, now); err != nil {
+ t.Fatal(err)
+ }
+ if listed("ok") {
+ t.Fatal("a shorter Hide shortened the lag")
+ }
+ now = now.Add(time.Minute)
+ if !listed("ok") {
+ t.Fatal("not listed after Hide's instant")
+ }
+}
+
func TestFakeGrantsObjects(t *testing.T) {
now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC)
g := NewFakeGrants(func() time.Time { return now })