Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #96 from libp2p/fix-flaky-accept-queue-backlogged
Browse files Browse the repository at this point in the history
fix flaky TestAcceptQueueBacklogged test
  • Loading branch information
marten-seemann committed Dec 12, 2021
2 parents a2b5749 + 073a2b0 commit e66e77a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"os"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e66e77a

Please sign in to comment.