| new file mode 100644 |
| index 000000000..8e4b53068 |
| --- /dev/null |
| +++ b/internal/activator/hadrainquiet_test.go |
| @@ -0,0 +1,142 @@ |
| +package activator |
| + |
| +import ( |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/edgesfile" |
| + "github.com/carlosframework/platform/internal/lease" |
| +) |
| + |
| +// assertRecoveryEnded is #1062's check: once a lost host's recovery entry |
| +// has left a.entries, nothing is recovering, the drain's Recovering count |
| +// is zero, and the loss itself is still latched (local wakes refused). |
| +func assertRecoveryEnded(t *testing.T, s *haSvc) { |
| + t.Helper() |
| + s.a.mu.Lock() |
| + l := s.a.haLoss.hosts[s.rt.Host] |
| + var stale bool |
| + if l != nil { |
| + stale = l.recovery != nil || l.running |
| + } |
| + s.a.mu.Unlock() |
| + if l == nil { |
| + t.Fatal("the loss record went with its recovery entry: the loss is discharged") |
| + } |
| + if stale { |
| + t.Fatal("the loss record still names a recovery that has ended") |
| + } |
| + if q := s.a.DrainQuietNow(); q.Recovering != 0 { |
| + t.Fatalf("Recovering = %d after the recovery ended (%+v)", q.Recovering, q) |
| + } |
| + assertLossLatched(t, s) |
| +} |
| + |
| +// The recovery wakes, then the route is removed (the edge's RetireExcept, |
| +// as on a route deletion or a trashed app) before the recovery's success |
| +// check: the recovery entry stops and goes. Field case #1062. |
| +func TestDrainQuietRecoveryRetiredRouteClears(t *testing.T) { |
| + s, e, r := lostTestEntry(t) |
| + finishLostTestEntry(t, s, e, r) |
| + var recovered *actEntry |
| + s.a.wakeEnded = func(host string) { |
| + s.a.mu.Lock() |
| + recovered = s.a.entries[host] |
| + s.a.mu.Unlock() |
| + s.a.RetireExcept(map[string]bool{}) |
| + } |
| + s.a.retryHALostHost(s.rt) |
| + s.a.haLoss.joined.Wait() |
| + if recovered == nil { |
| + t.Fatal("recovery did not wake") |
| + } |
| + s.a.mu.Lock() |
| + stopped := recovered.stopped |
| + s.a.mu.Unlock() |
| + if stopped == nil { |
| + t.Fatal("retirement did not stop the recovery entry") |
| + } |
| + select { |
| + case <-stopped: |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("retired recovery did not stop") |
| + } |
| + assertRecoveryEnded(t, s) |
| + if q := s.a.DrainQuietNow(); !q.Zero() { |
| + t.Fatalf("drain never quiesces: %+v", q) |
| + } |
| + // The loss is still owed a recovery: the next request starts one, and |
| + // its healthy fresh acquisition discharges the loss. |
| + s.a.wakeEnded = nil |
| + clk := &fakeClock{t: time.Now().Add(haLossRetryInterval)} |
| + s.a.now = clk.now |
| + recoverLostTestHost(t, s) |
| + s.a.mu.Lock() |
| + left := s.a.haLoss.hosts[s.rt.Host] |
| + s.a.mu.Unlock() |
| + if left != nil { |
| + t.Fatal("a recovered host kept its loss record") |
| + } |
| +} |
| + |
| +// The recovery wakes, then fails its post-check (the pool withdrawn): the |
| +// worker stops it for the lease, and the failed recovery entry goes. |
| +func TestDrainQuietRecoveryFailedPostCheckClears(t *testing.T) { |
| + s, e, r := lostTestEntry(t) |
| + finishLostTestEntry(t, s, e, r) |
| + var withdrawn atomic.Bool |
| + original := s.b.ServedPools |
| + s.b.ServedPools = func() edgesfile.PoolEvidence { |
| + if withdrawn.Load() { |
| + return edgesfile.PoolEvidence(`["another-pool"]`) |
| + } |
| + return original() |
| + } |
| + var recovered *actEntry |
| + s.a.wakeEnded = func(host string) { |
| + s.a.mu.Lock() |
| + recovered = s.a.entries[host] |
| + s.a.mu.Unlock() |
| + withdrawn.Store(true) |
| + } |
| + s.a.retryHALostHost(s.rt) |
| + s.a.haLoss.joined.Wait() |
| + if recovered == nil { |
| + t.Fatal("recovery did not wake") |
| + } |
| + s.a.mu.Lock() |
| + stopped := recovered.stopped |
| + s.a.mu.Unlock() |
| + if stopped == nil { |
| + t.Fatal("failed post-check left the recovery entry awake") |
| + } |
| + select { |
| + case <-stopped: |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("failed recovery did not stop") |
| + } |
| + assertRecoveryEnded(t, s) |
| +} |
| + |
| +// The recovery's wake itself fails (its owed marker cannot settle): the |
| +// failed wake's entry goes. |
| +func TestDrainQuietRecoveryFailedWakeClears(t *testing.T) { |
| + s, e, r := lostTestEntry(t) |
| + s.failMarkerPuts(2) // the release, then the recovery's settlement |
| + finishLostTestEntry(t, s, e, r) |
| + clk := &fakeClock{t: time.Now()} |
| + s.a.now = clk.now |
| + s.a.retryHALostHost(s.rt) |
| + s.a.haLoss.joined.Wait() |
| + s.a.mu.Lock() |
| + current := s.a.entries[s.rt.Host] |
| + s.a.mu.Unlock() |
| + if current != nil { |
| + t.Fatalf("failed recovery wake left an entry in state %v", current.state) |
| + } |
| + if len(lease.PendingObligations()) != 1 { |
| + t.Fatal("failed settlement discarded the obligation") |
| + } |
| + assertRecoveryEnded(t, s) |
| +} |