carlos / platform Public

activator: claimStartBackoff, so tests can shrink the claimed start backoff

startAdmitted's 1, 2, 4, 8 s wait between starts was time.Second inline.
It is now claimStartBackoff, a package var that is time.Second outside
tests (the value and the schedule are unchanged). The backoff test pins
the production base, then proves the doubling on a 250 ms base (3.75 s,
was 15 s, no longer skipped under -short); the planned claimed wake test
that only retries through it runs on 10 ms (was 15 s).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 8161f71782088c77e544fa2bd86c7df94760568f parent 58fcb30
3 files changed, +28 −6
  • internal/activator/claimloop_linux_test.go +10 −5
  • internal/activator/claimwake.go +6 −1
  • internal/activator/wakeplan_test.go +12 −0
diff --git a/internal/activator/claimloop_linux_test.go b/internal/activator/claimloop_linux_test.go
index 159b1b862..f51a84446 100644
--- a/internal/activator/claimloop_linux_test.go
+++ b/internal/activator/claimloop_linux_test.go
@@ -79,11 +79,16 @@ func driveLoop(t *testing.T, policy startPolicy) (*loopRun, error) {
// Five starts, then the loop gives up, with the delay between starts
// doubling: 1, 2, 4 and 8 seconds. The loop's backoff is time.NewTimer
// inline (claimwake.go), with no clock or sleeper to substitute, so this
-// reads real time and takes 15 s; -short skips it.
+// reads real time. It runs on a quarter of the production base
+// (claimStartBackoff), so 0.25, 0.5, 1 and 2 s, each delay checked to
+// within 200 ms, which keeps every step apart from its neighbours: the
+// doubling is what is proven, on 3.75 s of waiting rather than 15.
func TestStartAdmittedLoopStopsAfterFiveStartsWithDoublingBackoff(t *testing.T) {
- if testing.Short() {
- t.Skip("takes 15 s of real backoff")
+ if claimStartBackoff != time.Second {
+ t.Fatalf("claimStartBackoff = %v: the production schedule is 1, 2, 4, 8 s", claimStartBackoff)
}
+ base := 250 * time.Millisecond
+ shrinkClaimStartBackoff(t, base)
run, err := driveLoop(t, startPolicy{MaxStarts: 5, Window: 30 * time.Second, Deadline: time.Now().Add(time.Hour), Probe: true})
if !errors.Is(err, ErrClaimIsolationUnavailable) || errors.Is(err, ErrAttemptOver) {
t.Fatalf("the last start's refusal should end the loop: %v", err)
@@ -92,8 +97,8 @@ func TestStartAdmittedLoopStopsAfterFiveStartsWithDoublingBackoff(t *testing.T)
if len(at) != 5 || run.failed != 5 {
t.Fatalf("starts=%d failed=%d, want 5 and 5", len(at), run.failed)
}
- for i, want := range []time.Duration{time.Second, 2 * time.Second, 4 * time.Second, 8 * time.Second} {
- if gap := at[i+1].Sub(at[i]); gap < want || gap > want+400*time.Millisecond {
+ for i, want := range []time.Duration{base, 2 * base, 4 * base, 8 * base} {
+ if gap := at[i+1].Sub(at[i]); gap < want || gap > want+200*time.Millisecond {
t.Errorf("delay before start %d = %v, want %v", i+2, gap, want)
}
}
diff --git a/internal/activator/claimwake.go b/internal/activator/claimwake.go
index 77ee02923..414a71c00 100644
--- a/internal/activator/claimwake.go
+++ b/internal/activator/claimwake.go
@@ -388,6 +388,11 @@ func (a *Service) doClaimWake(ctx context.Context, e *actEntry) (*wokenInstance,
return a.claimedPlanResult(e, e.claimResult), nil
}
+// claimStartBackoff is startAdmitted's wait before its second start; the
+// wait doubles before each start after that (1, 2, 4, 8 s). A var so tests
+// can shrink it.
+var claimStartBackoff = time.Second
+
func (a *Service) startAdmitted(ctx context.Context, e *actEntry, admitted hostclaim.Observed, policy startPolicy) (result error) {
r := a.cfg.Claims
if !a.cfg.HostWrites.Enabled || r == nil || r.Read == nil || r.Prepare == nil || e == nil || admitted.Host != e.host || policy.MaxStarts < 1 || policy.MaxStarts > 5 || policy.Window <= 0 || policy.Window > 30*time.Second {
@@ -695,7 +700,7 @@ func (a *Service) startAdmitted(ctx context.Context, e *actEntry, admitted hostc
if policy.Probe && !budget.opened.IsZero() {
waitCtx, cancelWait = context.WithDeadline(ctx, budget.opened.Add(policy.Window))
}
- timer := time.NewTimer(time.Second << attempt)
+ timer := time.NewTimer(claimStartBackoff << attempt)
select {
case <-waitCtx.Done():
timer.Stop()
diff --git a/internal/activator/wakeplan_test.go b/internal/activator/wakeplan_test.go
index 686d6dc5b..1fc986e9e 100644
--- a/internal/activator/wakeplan_test.go
+++ b/internal/activator/wakeplan_test.go
@@ -533,12 +533,24 @@ func TestAClaimedFirstWakeReportsLikeAnUnclaimedOne(t *testing.T) {
}
}
+// shrinkClaimStartBackoff sets startAdmitted's backoff base for one test.
+func shrinkClaimStartBackoff(t *testing.T, d time.Duration) {
+ t.Helper()
+ old := claimStartBackoff
+ claimStartBackoff = d
+ t.Cleanup(func() { claimStartBackoff = old })
+}
+
// PF17: every claims-on launch refuses on this base (RoleLaunch.Start
// answers ErrClaimIsolationUnavailable before its exec hook). A planned
// claimed wake carries the plan into the start policy (the pin, the good
// build, the health cap, the ownership gate and the selection gate), meets
// the closed launch, and records nothing: no failure event, no fallback.
+// The refused start is retried on startAdmitted's backoff, whose schedule
+// TestStartAdmittedLoopStopsAfterFiveStartsWithDoublingBackoff pins; here
+// it is shrunk so the retries do not take 15 s.
func TestAPlannedClaimedWakeRefusesAtTheLaunchRecordingNothing(t *testing.T) {
+ shrinkClaimStartBackoff(t, 10*time.Millisecond)
dir := t.TempDir()
good, next := stagedID(t, dir, "v1"), stagedID(t, dir, "v2")
const host = "x.example"