From 5b34ae6309f37e4ccc39a7f4847b87d43ec8ec3a Mon Sep 17 00:00:00 2001 From: Sanaei Date: Tue, 24 Nov 2026 20:23:28 +0330 Subject: [PATCH] fix(test): end the silent node's reset delivery with its test The silent-node test returns after the one-second inline wait, while its delivery goroutine keeps the node's delivery lock until the push context times out three seconds later. The lock is keyed by node id, and every test's fresh database starts its nodes at id 1, so under -shuffle=on a test that ran next and expected a reset to arrive at once waited behind that lock and failed (go-test on #6966, run 45439392969; reproduced with -shuffle=1 on the two tests). The stalled runtime is now released when the test ends, and the cleanup waits for the delivery to let go of the lock. --- .../service/node_reset_undelivered_test.go | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/internal/web/service/node_reset_undelivered_test.go b/internal/web/service/node_reset_undelivered_test.go index 653bbcf1b..278d1c2b0 100644 --- a/internal/web/service/node_reset_undelivered_test.go +++ b/internal/web/service/node_reset_undelivered_test.go @@ -311,12 +311,32 @@ func TestNodeResetsSettleEachClientOnItsOwn(t *testing.T) { } } -// stallingResetRuntime never answers a reset until its context ends. -type stallingResetRuntime struct{ fakeNodeRuntime } +// stallingResetRuntime never answers a reset until its context ends or its test is over. +type stallingResetRuntime struct { + fakeNodeRuntime + testOver chan struct{} +} -func (*stallingResetRuntime) ResetClientTraffic(ctx context.Context, _ *model.Inbound, _ string) error { - <-ctx.Done() - return ctx.Err() +func (r *stallingResetRuntime) ResetClientTraffic(ctx context.Context, _ *model.Inbound, _ string) error { + select { + case <-ctx.Done(): + return ctx.Err() + case <-r.testOver: + return errors.New("test over") + } +} + +// endStalledResetDelivery stops a delivery that outlives its request by design: it holds +// the node id's delivery lock, and the next test's fresh database reuses that id. +func endStalledResetDelivery(t *testing.T, rt *stallingResetRuntime, nodeID int) { + t.Helper() + t.Cleanup(func() { + close(rt.testOver) + lock, _ := nodeResetDeliveryLocks.LoadOrStore(nodeID, &sync.Mutex{}) + mu := lock.(*sync.Mutex) + mu.Lock() + defer mu.Unlock() + }) } // A node relays its master's reset to its own sub-nodes inline; one silent sub-node must @@ -328,7 +348,9 @@ func TestNodeResetDeliveryDoesNotHoldTheRequestBehindASilentNode(t *testing.T) { if err := database.GetDB().Create(node).Error; err != nil { t.Fatalf("create node: %v", err) } - mgr.SetRuntimeOverride(node.Id, &stallingResetRuntime{}) + silent := &stallingResetRuntime{testOver: make(chan struct{})} + mgr.SetRuntimeOverride(node.Id, silent) + endStalledResetDelivery(t, silent, node.Id) ib := nodeInbound(t, node.Id, 41912, []model.Client{{Email: "held", ID: "11111111-1111-1111-1111-1111111111ab", Enable: true}}) if err := (&InboundService{}).AddClientStat(database.GetDB(), ib.Id, &model.Client{Email: "held", Enable: true}); err != nil { t.Fatalf("AddClientStat: %v", err)