| 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() |