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

fix flaky TestAcceptQueueBacklogged test #96

Merged
merged 1 commit into from
Dec 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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