fix: race in TestConnectionStatusChanges (#257)

This commit is contained in:
Martin Kobetic 2022-06-10 08:15:00 -04:00 committed by GitHub
parent c93cda7a95
commit 8e295a2a57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 6 deletions

View File

@ -13,8 +13,12 @@ import (
"github.com/stretchr/testify/require"
)
func checkConnectedness(t *testing.T, wg *sync.WaitGroup, connStatusChan chan ConnStatus, clientNode *WakuNode, node *WakuNode, nodeShouldBeConnected bool, shouldBeOnline bool, shouldHaveHistory bool, expectedPeers int) {
func goCheckConnectedness(t *testing.T, wg *sync.WaitGroup, connStatusChan chan ConnStatus, clientNode *WakuNode, node *WakuNode, nodeShouldBeConnected bool, shouldBeOnline bool, shouldHaveHistory bool, expectedPeers int) {
wg.Add(1)
go checkConnectedness(t, wg, connStatusChan, clientNode, node, nodeShouldBeConnected, shouldBeOnline, shouldHaveHistory, expectedPeers)
}
func checkConnectedness(t *testing.T, wg *sync.WaitGroup, connStatusChan chan ConnStatus, clientNode *WakuNode, node *WakuNode, nodeShouldBeConnected bool, shouldBeOnline bool, shouldHaveHistory bool, expectedPeers int) {
defer wg.Done()
timeout := time.After(5 * time.Second)
@ -85,31 +89,31 @@ func TestConnectionStatusChanges(t *testing.T) {
var wg sync.WaitGroup
go checkConnectedness(t, &wg, connStatusChan, node1, node2, true, true, false, 1)
goCheckConnectedness(t, &wg, connStatusChan, node1, node2, true, true, false, 1)
err = node1.DialPeer(ctx, node2.ListenAddresses()[0].String())
require.NoError(t, err)
wg.Wait()
go checkConnectedness(t, &wg, connStatusChan, node1, node3, true, true, true, 2)
goCheckConnectedness(t, &wg, connStatusChan, node1, node3, true, true, true, 2)
err = node1.DialPeer(ctx, node3.ListenAddresses()[0].String())
require.NoError(t, err)
go checkConnectedness(t, &wg, connStatusChan, node1, node3, false, true, false, 1)
goCheckConnectedness(t, &wg, connStatusChan, node1, node3, false, true, false, 1)
node3.Stop()
wg.Wait()
go checkConnectedness(t, &wg, connStatusChan, node1, node2, false, false, false, 0)
goCheckConnectedness(t, &wg, connStatusChan, node1, node2, false, false, false, 0)
err = node1.ClosePeerById(node2.Host().ID())
require.NoError(t, err)
wg.Wait()
go checkConnectedness(t, &wg, connStatusChan, node1, node2, true, true, false, 1)
goCheckConnectedness(t, &wg, connStatusChan, node1, node2, true, true, false, 1)
err = node1.DialPeerByID(ctx, node2.Host().ID())
require.NoError(t, err)