| index c39457a6..949419b3 100644 |
| --- a/internal/isolatedjob/isolatedjobtest/contract.go |
| +++ b/internal/isolatedjob/isolatedjobtest/contract.go |
| @@ -21,21 +21,81 @@ type Harness struct { |
| // Settle waits until the VM's state satisfies want and returns the job |
| // as last read. It returns an error when the VM cannot be read (which |
| // may wrap isolatedjob.ErrNotFound) or when the state does not arrive in |
| - // the implementation's own time bound. |
| + // the implementation's own time bound, and it bounds each read it makes. |
| Settle func(ctx context.Context, ref isolatedjob.Ref, want func(isolatedjob.State) bool) (isolatedjob.Job, error) |
| // ListLag is how long List may take to show a VM that Get already shows |
| // as running. Zero means at once, as for the fake. |
| ListLag time.Duration |
| + // Visibility is how long Get may answer ErrNotFound for a VM that |
| + // Launch has just returned, as a provider whose reads are eventually |
| + // consistent does. It bounds only the wait for a new VM to appear, never |
| + // a wait for a state. Zero means at once, as for the fake. |
| + Visibility time.Duration |
| + // CallTimeout bounds each Runner call the contract makes, so a stalled |
| + // request fails the run instead of hanging it. Zero means |
| + // DefaultCallTimeout. |
| + CallTimeout time.Duration |
| +} |
| + |
| +// DefaultCallTimeout bounds each Runner call when the Harness sets no |
| +// CallTimeout. |
| +const DefaultCallTimeout = 30 * time.Second |
| + |
| +// call returns the context for one Runner call, bounded by CallTimeout. |
| +func (h Harness) call() (context.Context, context.CancelFunc) { |
| + d := h.CallTimeout |
| + if d <= 0 { |
| + d = DefaultCallTimeout |
| + } |
| + return context.WithTimeout(context.Background(), d) |
| +} |
| + |
| +func (h Harness) launch(s isolatedjob.LaunchSpec) (isolatedjob.Ref, error) { |
| + ctx, cancel := h.call() |
| + defer cancel() |
| + return h.Runner.Launch(ctx, s) |
| +} |
| + |
| +func (h Harness) get(ref isolatedjob.Ref) (isolatedjob.Job, error) { |
| + ctx, cancel := h.call() |
| + defer cancel() |
| + return h.Runner.Get(ctx, ref) |
| +} |
| + |
| +func (h Harness) list() ([]isolatedjob.Job, error) { |
| + ctx, cancel := h.call() |
| + defer cancel() |
| + return h.Runner.List(ctx) |
| +} |
| + |
| +func (h Harness) stop(ref isolatedjob.Ref, reason string) error { |
| + ctx, cancel := h.call() |
| + defer cancel() |
| + return h.Runner.Stop(ctx, ref, reason) |
| +} |
| + |
| +func (h Harness) suspend(ref isolatedjob.Ref) error { |
| + ctx, cancel := h.call() |
| + defer cancel() |
| + return h.Runner.Suspend(ctx, ref) |
| +} |
| + |
| +func (h Harness) resume(ref isolatedjob.Ref) error { |
| + ctx, cancel := h.call() |
| + defer cancel() |
| + return h.Runner.Resume(ctx, ref) |
| } |
| |
| // contractLifetime keeps VMs launched by a live run short. |
| const contractLifetime = 15 * time.Minute |
| |
| // Contract checks the behaviour every Runner must share. It uses only the |
| -// Runner interface, h.Settle and h.ListLag, so it runs against the fake and, behind the |
| -// hostedlive build tag, against a real provider. Each subtest stops the VMs it |
| -// launched. Attempt IDs carry a random part, so a second run against a |
| -// provider that remembers client tokens does not get the first run's VMs. |
| +// Runner interface and the Harness, so it runs against the fake and, behind |
| +// the hostedlive build tag, against a real provider. Each subtest stops the |
| +// VMs it launched. Attempt IDs carry a random part, so a second run against |
| +// a provider that remembers client tokens does not get the first run's VMs. |
| +// Every VM the contract launches must show in Get within h.Visibility |
| +// before any check reads it. |
| func Contract(t *testing.T, h Harness) { |
| t.Helper() |
| run := randomHex(t, 6) |
| @@ -55,15 +115,16 @@ func Contract(t *testing.T, h Harness) { |
| } |
| launch := func(t *testing.T, s isolatedjob.LaunchSpec) isolatedjob.Ref { |
| t.Helper() |
| - ref, err := h.Runner.Launch(context.Background(), s) |
| + ref, err := h.launch(s) |
| if err != nil { |
| t.Fatalf("Launch(%s) = %v, want a ref", s.AttemptID, err) |
| } |
| t.Cleanup(func() { |
| - if err := h.Runner.Stop(context.Background(), ref, "contract cleanup"); err != nil { |
| + if err := h.stop(ref, "contract cleanup"); err != nil { |
| t.Errorf("cleanup Stop(%s) = %v", ref, err) |
| } |
| }) |
| + appear(t, h, ref) |
| return ref |
| } |
| settle := func(t *testing.T, ref isolatedjob.Ref, want isolatedjob.State) isolatedjob.Job { |
| @@ -78,14 +139,14 @@ func Contract(t *testing.T, h Harness) { |
| t.Run("LaunchIdempotentOnAttempt", func(t *testing.T) { |
| s := spec() |
| first := launch(t, s) |
| - again, err := h.Runner.Launch(context.Background(), s) |
| + again, err := h.launch(s) |
| if err != nil { |
| t.Fatalf("second Launch(%s) = %v, want the first ref", s.AttemptID, err) |
| } |
| if again != first { |
| t.Fatalf("second Launch(%s) = %s, want %s: one attempt must be one VM", s.AttemptID, again, first) |
| } |
| - job, err := h.Runner.Get(context.Background(), first) |
| + job, err := h.get(first) |
| if err != nil { |
| t.Fatalf("Get(%s) = %v", first, err) |
| } |
| @@ -99,7 +160,7 @@ func Contract(t *testing.T, h Harness) { |
| first := launch(t, s) |
| changed := s |
| changed.Bootstrap = []byte("another-bootstrap") |
| - ref, err := h.Runner.Launch(context.Background(), changed) |
| + ref, err := h.launch(changed) |
| if !errors.Is(err, isolatedjob.ErrConflict) { |
| t.Fatalf("Launch with the same attempt and a new bootstrap = (%q, %v), want ErrConflict", ref, err) |
| } |
| @@ -111,7 +172,7 @@ func Contract(t *testing.T, h Harness) { |
| t.Run("StopTwice", func(t *testing.T) { |
| ref := launch(t, spec()) |
| for i := 1; i <= 2; i++ { |
| - if err := h.Runner.Stop(context.Background(), ref, "contract stop twice"); err != nil { |
| + if err := h.stop(ref, "contract stop twice"); err != nil { |
| t.Fatalf("Stop %d of %s = %v, want nil", i, ref, err) |
| } |
| } |
| @@ -119,21 +180,21 @@ func Contract(t *testing.T, h Harness) { |
| |
| t.Run("StopUnknownRef", func(t *testing.T) { |
| ref := isolatedjob.Ref("contract-never-launched-" + run) |
| - if err := h.Runner.Stop(context.Background(), ref, "contract unknown ref"); err != nil { |
| + if err := h.stop(ref, "contract unknown ref"); err != nil { |
| t.Fatalf("Stop(%s) of a ref the provider never held = %v, want nil", ref, err) |
| } |
| }) |
| |
| t.Run("GetAfterStop", func(t *testing.T) { |
| ref := launch(t, spec()) |
| - if err := h.Runner.Stop(context.Background(), ref, "contract get after stop"); err != nil { |
| + if err := h.stop(ref, "contract get after stop"); err != nil { |
| t.Fatalf("Stop(%s) = %v", ref, err) |
| } |
| _, err := h.Settle(context.Background(), ref, isolatedjob.State.Terminal) |
| if err != nil && !errors.Is(err, isolatedjob.ErrNotFound) { |
| t.Fatalf("Settle(%s, terminal) = %v, want a terminal state or ErrNotFound", ref, err) |
| } |
| - job, err := h.Runner.Get(context.Background(), ref) |
| + job, err := h.get(ref) |
| switch { |
| case errors.Is(err, isolatedjob.ErrNotFound): |
| case err != nil: |
| @@ -146,11 +207,11 @@ func Contract(t *testing.T, h Harness) { |
| t.Run("SuspendResumeRoundTrip", func(t *testing.T) { |
| ref := launch(t, spec()) |
| settle(t, ref, isolatedjob.StateRunning) |
| - if err := h.Runner.Suspend(context.Background(), ref); err != nil { |
| + if err := h.suspend(ref); err != nil { |
| t.Fatalf("Suspend(%s) = %v", ref, err) |
| } |
| settle(t, ref, isolatedjob.StateSuspended) |
| - if err := h.Runner.Resume(context.Background(), ref); err != nil { |
| + if err := h.resume(ref); err != nil { |
| t.Fatalf("Resume(%s) = %v", ref, err) |
| } |
| settle(t, ref, isolatedjob.StateRunning) |
| @@ -159,10 +220,10 @@ func Contract(t *testing.T, h Harness) { |
| t.Run("ResumeRunningIsNoop", func(t *testing.T) { |
| ref := launch(t, spec()) |
| settle(t, ref, isolatedjob.StateRunning) |
| - if err := h.Runner.Resume(context.Background(), ref); err != nil { |
| + if err := h.resume(ref); err != nil { |
| t.Fatalf("Resume(%s) of a running VM = %v, want nil", ref, err) |
| } |
| - job, err := h.Runner.Get(context.Background(), ref) |
| + job, err := h.get(ref) |
| if err != nil { |
| t.Fatalf("Get(%s) = %v", ref, err) |
| } |
| @@ -176,20 +237,20 @@ func Contract(t *testing.T, h Harness) { |
| // that always comes back empty cannot pass the check below. |
| known := launch(t, spec()) |
| settle(t, known, isolatedjob.StateRunning) |
| - before := listUntil(t, h.Runner, known, h.ListLag) |
| + before := listUntil(t, h, known) |
| s := spec() |
| s.Bootstrap = make([]byte, h.Runner.Limits().MaxBootstrapBytes+1) |
| for i := range s.Bootstrap { |
| s.Bootstrap[i] = 'x' |
| } |
| - ref, err := h.Runner.Launch(context.Background(), s) |
| + ref, err := h.launch(s) |
| if ref != "" { |
| - t.Cleanup(func() { _ = h.Runner.Stop(context.Background(), ref, "contract cleanup") }) |
| + t.Cleanup(func() { _ = h.stop(ref, "contract cleanup") }) |
| } |
| if !errors.Is(err, isolatedjob.ErrBootstrapTooLarge) { |
| t.Fatalf("Launch with a %d-byte bootstrap = (%q, %v), want ErrBootstrapTooLarge", len(s.Bootstrap), ref, err) |
| } |
| - after := listRefs(t, h.Runner) |
| + after := listRefs(t, h) |
| if !slices.Contains(after, known) { |
| t.Fatalf("List after a refused launch = %v, want it still to hold running VM %s", after, known) |
| } |
| @@ -211,9 +272,34 @@ func Contract(t *testing.T, h Harness) { |
| }) |
| } |
| |
| -func listRefs(t *testing.T, r isolatedjob.Runner) []isolatedjob.Ref { |
| +// appear reads ref with Get until the provider shows it, for at most |
| +// h.Visibility after the first read. Only ErrNotFound and ErrThrottled are |
| +// read again; any other error, or ref still missing when the time is up, |
| +// fails the test. Zero Visibility is one read. Reads are spaced a |
| +// twentieth of Visibility apart, at most a second, so a short window in a |
| +// test costs little. |
| +func appear(t *testing.T, h Harness, ref isolatedjob.Ref) { |
| + t.Helper() |
| + deadline := time.Now().Add(h.Visibility) |
| + gap := min(time.Second, max(h.Visibility/20, time.Millisecond)) |
| + for { |
| + _, err := h.get(ref) |
| + if err == nil { |
| + return |
| + } |
| + if !errors.Is(err, isolatedjob.ErrNotFound) && !errors.Is(err, isolatedjob.ErrThrottled) { |
| + t.Fatalf("Get(%s) of a VM Launch just returned = %v", ref, err) |
| + } |
| + if !time.Now().Before(deadline) { |
| + t.Fatalf("Get(%s) = %v for %v after Launch returned it, want the VM to appear", ref, err, h.Visibility) |
| + } |
| + time.Sleep(min(gap, time.Until(deadline))) |
| + } |
| +} |
| + |
| +func listRefs(t *testing.T, h Harness) []isolatedjob.Ref { |
| t.Helper() |
| - jobs, err := r.List(context.Background()) |
| + jobs, err := h.list() |
| if err != nil { |
| t.Fatalf("List = %v", err) |
| } |
| @@ -224,13 +310,15 @@ func listRefs(t *testing.T, r isolatedjob.Runner) []isolatedjob.Ref { |
| return refs |
| } |
| |
| -// listUntil lists until the result holds a live job for ref, for at most lag, |
| -// and returns the refs of that list. It fails the test when ref never shows. |
| -func listUntil(t *testing.T, r isolatedjob.Runner, ref isolatedjob.Ref, lag time.Duration) []isolatedjob.Ref { |
| +// listUntil lists until the result holds a live job for ref, for at most |
| +// h.ListLag, and returns the refs of that list. It fails the test when ref |
| +// never shows. Each List is bounded by h.CallTimeout, so a stalled request |
| +// overruns ListLag by at most that; zero ListLag is one List. |
| +func listUntil(t *testing.T, h Harness, ref isolatedjob.Ref) []isolatedjob.Ref { |
| t.Helper() |
| - deadline := time.Now().Add(lag) |
| + deadline := time.Now().Add(h.ListLag) |
| for { |
| - jobs, err := r.List(context.Background()) |
| + jobs, err := h.list() |
| if err != nil { |
| t.Fatalf("List = %v", err) |
| } |
| @@ -244,7 +332,7 @@ func listUntil(t *testing.T, r isolatedjob.Runner, ref isolatedjob.Ref, lag time |
| return refs |
| } |
| if !time.Now().Before(deadline) { |
| - t.Fatalf("List = %v after %v, want it to hold running VM %s", refs, lag, ref) |
| + t.Fatalf("List = %v after %v, want it to hold running VM %s", refs, h.ListLag, ref) |
| } |
| time.Sleep(min(time.Second, time.Until(deadline))) |
| } |