| index 0f2b75d69..847067577 100644 |
| --- a/internal/activator/rootfsrelease_test.go |
| +++ b/internal/activator/rootfsrelease_test.go |
| @@ -24,6 +24,7 @@ import ( |
| "sync" |
| "sync/atomic" |
| "testing" |
| + "testing/synctest" |
| "time" |
| |
| "github.com/carlosframework/platform/internal/hostclaim" |
| @@ -702,14 +703,11 @@ func (r *releaseRig) noRecord(t *testing.T, when string) { |
| } |
| |
| // pinned reports whether the generations file on disk names build v under |
| -// the host. |
| +// the host. Publication writes the sequence marker before the generations |
| +// file, so pinnedSettled retries reads made between those two writes. |
| func pinned(t *testing.T, a *Service, host, v string) bool { |
| t.Helper() |
| - g, err := release.ReadGenerations(a.GenerationsFile) |
| - if err != nil { |
| - t.Fatalf("the generations file: %v", err) |
| - } |
| - return g != nil && slices.Contains(g.Hosts[host], v) |
| + return pinnedSettled(t, a, host, v) |
| } |
| |
| // shrinkRelease shrinks the lease lead, the recheck and the retries. Not |
| @@ -1139,72 +1137,78 @@ func TestReleaseKeepsRunningWhileTheLeaseRenews(t *testing.T) { |
| } |
| |
| func TestReleaseLeaseLossUnprovenLeavesTheHold(t *testing.T) { |
| - shrinkRelease(t, 200*time.Millisecond, 10*time.Millisecond) |
| - r := newReleaseRig(t) |
| - until := time.Now().Add(800 * time.Millisecond) |
| - r.held = newOwnedLease(until, false) |
| - var stuck sync.Mutex |
| - isStuck := true |
| - r.b.unit.settle = func(int) error { |
| - stuck.Lock() |
| - defer stuck.Unlock() |
| - if isStuck { |
| - return errors.New("processes remained in the unit's cgroup") |
| + // The settle loop, the supervisor and the lease deadline all read the real |
| + // clock, so under load a settle could start after the deadline by sleep |
| + // overshoot alone. A synctest bubble gives them one fake clock: sleeps are |
| + // exact and the deadline comparison is deterministic. |
| + synctest.Test(t, func(t *testing.T) { |
| + shrinkRelease(t, 200*time.Millisecond, 10*time.Millisecond) |
| + r := newReleaseRig(t) |
| + until := time.Now().Add(800 * time.Millisecond) |
| + r.held = newOwnedLease(until, false) |
| + var stuck sync.Mutex |
| + isStuck := true |
| + r.b.unit.settle = func(int) error { |
| + stuck.Lock() |
| + defer stuck.Unlock() |
| + if isStuck { |
| + return errors.New("processes remained in the unit's cgroup") |
| + } |
| + return nil |
| } |
| - return nil |
| - } |
| - ch := r.run(context.Background()) |
| - r.waitStarted(t) |
| - r.held.lose(lease.ErrLost) |
| - res := wait(t, ch) |
| - if !errors.Is(res.err, errReleaseUnproven) || !errors.Is(res.err, errSwapLeaseLost) { |
| - t.Fatalf("runRelease = %v, want errReleaseUnproven and errSwapLeaseLost", res.err) |
| - } |
| - if o := classifyRelease(res.err, true); o != releaseUnproven { |
| - t.Errorf("classified %v, want unproven", o) |
| - } |
| - stops := r.b.unit.stopTimes() |
| - if len(stops) < 2 { |
| - t.Errorf("the settle was tried %d times after the trip, want it retried until the deadline", len(stops)) |
| - } |
| - if last := stops[len(stops)-1]; last.After(until) { |
| - t.Errorf("a settle began at %v, after the deadline %v", last, until) |
| - } |
| - op := r.record() |
| - if op == nil { |
| - t.Fatal("the record went with its unit unproven") |
| - } |
| - r.a.mu.Lock() |
| - state := op.state |
| - r.a.mu.Unlock() |
| - if state != releaseNotProven { |
| - t.Errorf("record state %d, want releaseNotProven (the reaper's)", state) |
| - } |
| - if evs := r.events(); slices.Contains(evs, fmt.Sprintf(rolloutwords.EventReleaseLeaseLost, "v2")) { |
| - t.Errorf("C29 with the unit unproven: %q", evs) |
| - } |
| - if !pinned(t, r.a, r.rt.Host, "v2") { |
| - t.Error("the generations file does not name v2 while its unit is unproven") |
| - } |
| - // The reaper owns it now: once the group empties, the sweep's pass |
| - // proves it. |
| - n := len(stops) |
| - r.a.mu.Lock() |
| - r.a.finishPendingStops() |
| - r.a.mu.Unlock() |
| - time.Sleep(50 * time.Millisecond) |
| - if got := len(r.b.unit.stopTimes()); got != n { |
| - t.Fatalf("the reaper retried %d times inside actStopTimeout", got-n) |
| - } |
| - stuck.Lock() |
| - isStuck = false |
| - stuck.Unlock() |
| - r.clk.advance(actStopTimeout) |
| - r.a.mu.Lock() |
| - r.a.finishPendingStops() |
| - r.a.mu.Unlock() |
| - waitFor(t, "the reaper's proof", func() bool { return r.record() == nil }) |
| - waitFor(t, "the pin to go", func() bool { return !pinned(t, r.a, r.rt.Host, "v2") }) |
| + ch := r.run(context.Background()) |
| + r.waitStarted(t) |
| + r.held.lose(lease.ErrLost) |
| + res := wait(t, ch) |
| + if !errors.Is(res.err, errReleaseUnproven) || !errors.Is(res.err, errSwapLeaseLost) { |
| + t.Fatalf("runRelease = %v, want errReleaseUnproven and errSwapLeaseLost", res.err) |
| + } |
| + if o := classifyRelease(res.err, true); o != releaseUnproven { |
| + t.Errorf("classified %v, want unproven", o) |
| + } |
| + stops := r.b.unit.stopTimes() |
| + if len(stops) < 2 { |
| + t.Errorf("the settle was tried %d times after the trip, want it retried until the deadline", len(stops)) |
| + } |
| + if last := stops[len(stops)-1]; last.After(until) { |
| + t.Errorf("a settle began at %v, after the deadline %v", last, until) |
| + } |
| + op := r.record() |
| + if op == nil { |
| + t.Fatal("the record went with its unit unproven") |
| + } |
| + r.a.mu.Lock() |
| + state := op.state |
| + r.a.mu.Unlock() |
| + if state != releaseNotProven { |
| + t.Errorf("record state %d, want releaseNotProven (the reaper's)", state) |
| + } |
| + if evs := r.events(); slices.Contains(evs, fmt.Sprintf(rolloutwords.EventReleaseLeaseLost, "v2")) { |
| + t.Errorf("C29 with the unit unproven: %q", evs) |
| + } |
| + if !pinned(t, r.a, r.rt.Host, "v2") { |
| + t.Error("the generations file does not name v2 while its unit is unproven") |
| + } |
| + // The reaper owns it now: once the group empties, the sweep's pass |
| + // proves it. |
| + n := len(stops) |
| + r.a.mu.Lock() |
| + r.a.finishPendingStops() |
| + r.a.mu.Unlock() |
| + time.Sleep(50 * time.Millisecond) |
| + if got := len(r.b.unit.stopTimes()); got != n { |
| + t.Fatalf("the reaper retried %d times inside actStopTimeout", got-n) |
| + } |
| + stuck.Lock() |
| + isStuck = false |
| + stuck.Unlock() |
| + r.clk.advance(actStopTimeout) |
| + r.a.mu.Lock() |
| + r.a.finishPendingStops() |
| + r.a.mu.Unlock() |
| + waitFor(t, "the reaper's proof", func() bool { return r.record() == nil }) |
| + waitFor(t, "the pin to go", func() bool { return !pinned(t, r.a, r.rt.Host, "v2") }) |
| + }) |
| } |
| |
| func TestReleaseLeaseLossRacesExit(t *testing.T) { |