From 7612b4d299194efabda37932bf325e502da9caf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Wed, 12 Aug 2026 10:14:59 +0200 Subject: [PATCH] [client] Stop the guard test poller from outliving the test pollUntil span a goroutine that looped forever when the condition never held, which is exactly the path the test takes when it fails. Pass the test context in and give up when it is done. --- .../internal/peer/guard/guard_netstate_test.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/client/internal/peer/guard/guard_netstate_test.go b/client/internal/peer/guard/guard_netstate_test.go index 601dce33c..2ab736428 100644 --- a/client/internal/peer/guard/guard_netstate_test.go +++ b/client/internal/peer/guard/guard_netstate_test.go @@ -55,11 +55,14 @@ func TestGuard_RecoversAfterOfflineToOnline(t *testing.T) { // The next organic tick is now several seconds out, so anything within // this window can only come from reacting to the transition itself. + pollCtx, stopPolling := context.WithTimeout(ctx, 2*time.Second) + defer stopPolling() + select { - case <-time.After(2 * time.Second): + case <-pollCtx.Done(): t.Fatal("peer was not retried within 2s of the network coming back, " + "with neither a signal nor a relay event to fall back on") - case <-pollUntil(func() bool { return attempts.Load() > 0 }): + case <-pollUntil(pollCtx, func() bool { return attempts.Load() > 0 }): } } @@ -83,7 +86,9 @@ func TestGuard_OfflineTransitionDoesNotRetry(t *testing.T) { } } -func pollUntil(cond func() bool) <-chan struct{} { +// pollUntil closes the returned channel once cond holds. It gives up when ctx +// is done, so the polling goroutine never outlives the test that started it. +func pollUntil(ctx context.Context, cond func() bool) <-chan struct{} { done := make(chan struct{}) go func() { for { @@ -91,7 +96,11 @@ func pollUntil(cond func() bool) <-chan struct{} { close(done) return } - time.Sleep(10 * time.Millisecond) + select { + case <-ctx.Done(): + return + case <-time.After(10 * time.Millisecond): + } } }() return done