| index 87f3fc993..860904179 100644 |
| --- a/internal/host/ingressdrain_test.go |
| +++ b/internal/host/ingressdrain_test.go |
| @@ -219,6 +219,8 @@ type ingressFixture struct { |
| edge *fakeIngressEdge |
| health *fakeIngressHealth |
| exits *[]int |
| + // terms counts the SIGTERMs the drain sent its own process. |
| + terms *int |
| } |
| |
| func newIngressFixture(t *testing.T, script func(at time.Duration) (ingresshealth.Verdict, error)) *ingressFixture { |
| @@ -241,8 +243,10 @@ func newIngressFixture(t *testing.T, script func(at time.Duration) (ingresshealt |
| } |
| exits := &[]int{} |
| d.exit = func(code int) { *exits = append(*exits, code) } |
| + terms := new(int) |
| + d.terminate = func() error { *terms++; return nil } |
| d.setEdge(e) |
| - return &ingressFixture{d: d, clock: c, tg: tg, edge: e, health: h, exits: exits} |
| + return &ingressFixture{d: d, clock: c, tg: tg, edge: e, health: h, exits: exits, terms: terms} |
| } |
| |
| // drain runs one drain to its end and returns the report. |
| @@ -616,8 +620,11 @@ func TestIngressDrainJoins(t *testing.T) { |
| } |
| } |
| |
| -// Three minutes after step 3 the agent exits non-zero, unless its own |
| -// shutdown began first. |
| +// Three minutes after step 3 the agent shuts itself down, unless its own |
| +// shutdown began first: SIGTERM to its own process, so the normal shutdown |
| +// runs (clean marker, lease release), and os.Exit(1) only as a backstop if |
| +// the process is still alive agentShutdownBudget later (W6 final review |
| +// F6). |
| func TestIngressDrainExitsThreeMinutesAfterStepThree(t *testing.T) { |
| f := newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy)) |
| f.drain(t) |
| @@ -625,16 +632,36 @@ func TestIngressDrainExitsThreeMinutesAfterStepThree(t *testing.T) { |
| if len(as) != 1 || as[0].d != 3*time.Minute { |
| t.Fatalf("scheduled %+v", as) |
| } |
| - if len(*f.exits) != 0 { |
| - t.Fatal("exited at step 3") |
| + if len(*f.exits) != 0 || *f.terms != 0 { |
| + t.Fatal("ended at step 3") |
| } |
| as[0].f() |
| + if *f.terms != 1 || len(*f.exits) != 0 { |
| + t.Fatalf("at 3 minutes: %d SIGTERM(s), exits %v; want one SIGTERM and no exit", *f.terms, *f.exits) |
| + } |
| + // The SIGTERM starts the agent's shutdown, which must not disarm the |
| + // backstop: it is there for a shutdown that hangs. |
| + f.d.shutdownStarted() |
| + as = f.clock.afters() |
| + if len(as) != 2 || as[1].d != agentShutdownBudget || *as[1].stopped { |
| + t.Fatalf("backstop %+v", as) |
| + } |
| + as[1].f() |
| + if !slices.Equal(*f.exits, []int{1}) { |
| + t.Fatalf("backstop exits %v", *f.exits) |
| + } |
| + |
| + // A SIGTERM that cannot be sent: the exit at once. |
| + f = newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy)) |
| + f.d.terminate = func() error { return errors.New("no signal") } |
| + f.drain(t) |
| + f.clock.afters()[0].f() |
| if !slices.Equal(*f.exits, []int{1}) { |
| t.Fatalf("exits %v", *f.exits) |
| } |
| |
| - // The stop came: shutdown cancels the exit, and a timer that fires |
| - // anyway (the race) does not exit either. |
| + // The stop came: shutdown cancels the timer, and a timer that fires |
| + // anyway (the race) sends nothing either. |
| f = newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy)) |
| f.drain(t) |
| f.d.shutdownStarted() |
| @@ -643,8 +670,8 @@ func TestIngressDrainExitsThreeMinutesAfterStepThree(t *testing.T) { |
| t.Fatalf("the exit was not cancelled: %+v", as) |
| } |
| as[0].f() |
| - if len(*f.exits) != 0 { |
| - t.Fatalf("exited during shutdown: %v", *f.exits) |
| + if len(*f.exits) != 0 || *f.terms != 0 || len(f.clock.afters()) != 1 { |
| + t.Fatalf("ended during shutdown: exits %v, SIGTERMs %d", *f.exits, *f.terms) |
| } |
| |
| // Shutdown before step 3 ends: no exit is scheduled at all. |
| @@ -960,15 +987,16 @@ func TestIngressDrainBoundsHungCalls(t *testing.T) { |
| } |
| |
| // The exit and the shutdown are one decision: a shutdown that starts while |
| -// the exit is running waits for it (os.Exit never returns), and an exit |
| -// timer that fires while the shutdown is starting does nothing after it. |
| +// the 3-minute exit is deciding waits for it, and a timer that fires while |
| +// the shutdown is starting does nothing after it. |
| func TestIngressDrainExitAndShutdownInterleave(t *testing.T) { |
| f := newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy)) |
| entered, release := make(chan struct{}), make(chan struct{}) |
| - f.d.exit = func(code int) { |
| + f.d.terminate = func() error { |
| close(entered) |
| <-release |
| - *f.exits = append(*f.exits, code) |
| + *f.terms++ |
| + return nil |
| } |
| f.drain(t) |
| timer := f.clock.afters()[0].f |
| @@ -978,17 +1006,17 @@ func TestIngressDrainExitAndShutdownInterleave(t *testing.T) { |
| go func() { f.d.shutdownStarted(); close(shut) }() |
| select { |
| case <-shut: |
| - t.Fatal("the shutdown began while the exit was running") |
| + t.Fatal("the shutdown began while the exit was deciding") |
| case <-time.After(50 * time.Millisecond): |
| } |
| close(release) |
| <-shut |
| - if !slices.Equal(*f.exits, []int{1}) { |
| - t.Fatalf("exits %v", *f.exits) |
| + if *f.terms != 1 || len(f.clock.afters()) != 2 { |
| + t.Fatalf("SIGTERMs %d, scheduled %+v", *f.terms, f.clock.afters()) |
| } |
| |
| // The shutdown holds the decision: a timer fired while shutdownStarted |
| - // is inside its critical section exits nothing. |
| + // is inside its critical section sends nothing after it. |
| f = newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy)) |
| f.drain(t) |
| timer = f.clock.afters()[0].f |
| @@ -1003,8 +1031,8 @@ func TestIngressDrainExitAndShutdownInterleave(t *testing.T) { |
| f.d.stopping = true |
| f.d.mu.Unlock() |
| <-fired |
| - if len(*f.exits) != 0 { |
| - t.Fatalf("exited during the shutdown: %v", *f.exits) |
| + if len(*f.exits) != 0 || *f.terms != 0 { |
| + t.Fatalf("ended during the shutdown: exits %v, SIGTERMs %d", *f.exits, *f.terms) |
| } |
| } |
| |