From 073a2b06bb37914454c2799221c7c32f4f060159 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 12 Dec 2021 16:29:19 +0400 Subject: [PATCH] fix flaky TestAcceptQueueBacklogged test --- listener_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/listener_test.go b/listener_test.go index 1d339d2..b47fd91 100644 --- a/listener_test.go +++ b/listener_test.go @@ -6,6 +6,7 @@ import ( "net" "os" "sync" + "sync/atomic" "testing" "time" @@ -285,33 +286,32 @@ func TestAcceptQueueBacklogged(t *testing.T) { defer ln.Close() // setup AcceptQueueLength connections, but don't accept any of them - errCh := make(chan error, st.AcceptQueueLength+1) + var counter int32 // to be used atomically doDial := func() { conn, err := dial(t, upgrader, ln.Multiaddr(), id) - errCh <- err - if conn != nil { - _ = conn.Close() - } + require.NoError(err) + atomic.AddInt32(&counter, 1) + t.Cleanup(func() { conn.Close() }) } for i := 0; i < st.AcceptQueueLength; i++ { go doDial() } - require.Eventually(func() bool { return len(errCh) == st.AcceptQueueLength }, 2*time.Second, 100*time.Millisecond) + require.Eventually(func() bool { return int(atomic.LoadInt32(&counter)) == st.AcceptQueueLength }, 2*time.Second, 50*time.Millisecond) // dial a new connection. This connection should not complete setup, since the queue is full go doDial() - time.Sleep(500 * time.Millisecond) - require.Len(errCh, st.AcceptQueueLength) + time.Sleep(100 * time.Millisecond) + require.Equal(int(atomic.LoadInt32(&counter)), st.AcceptQueueLength) // accept a single connection. Now the new connection should be set up, and fill the queue again conn, err := ln.Accept() require.NoError(err) - _ = conn.Close() + require.NoError(conn.Close()) - require.Eventually(func() bool { return len(errCh) == st.AcceptQueueLength+1 }, 2*time.Second, 100*time.Millisecond) + require.Eventually(func() bool { return int(atomic.LoadInt32(&counter)) == st.AcceptQueueLength+1 }, 2*time.Second, 50*time.Millisecond) } func TestListenerConnectionGater(t *testing.T) {