Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(dot/peerset): remove the time.Sleep() dependency. #2785

Closed
wants to merge 2 commits into from
Closed
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
70 changes: 55 additions & 15 deletions dot/peerset/peerset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package peerset

import (
"sync"
"testing"
"time"

Expand Down Expand Up @@ -78,9 +79,15 @@ func TestAddReservedPeers(t *testing.T) {

reservedPeers := peer.IDSlice{reservedPeer, reservedPeer2}

var waitGroup sync.WaitGroup

for _, peerID := range reservedPeers {
handler.AddReservedPeer(testSetID, peerID)
time.Sleep(time.Millisecond * 100)
waitGroup.Add(1)
go func() {
handler.AddReservedPeer(testSetID, peerID)
waitGroup.Done()
}()
waitGroup.Wait()
Comment on lines +85 to +90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't do anything (same as just handler.AddReservedPeer(testSetID, peerID))

I think we want to wait for a fire-and-forget goroutine launched within AddReservedPeer, so that would require a refactor of the production code of the method AddReservedPeer. Just a wild guess though, I haven't really digged into that method either.

Same comment for the other changes.

Parallel programming is hard too, so take it easy 😉


checkReservedNodePeerExists(t, ps, peerID)
checkPeerIsInNoSlotsNode(t, ps.peerState, peerID, testSetID)
Expand Down Expand Up @@ -152,14 +159,20 @@ func TestPeerSetIncoming(t *testing.T) {
},
}

var waitGroup sync.WaitGroup

for _, tt := range incomingPeers {

// all the incoming peers are unknow before calling the Incoming method
status := ps.peerState.peerStatus(testSetID, tt.pid)
require.Equal(t, unknownPeer, status)

handler.Incoming(testSetID, tt.pid)
time.Sleep(time.Millisecond * 100)
waitGroup.Add(1)
go func() {
handler.Incoming(testSetID, tt.pid)
waitGroup.Done()
}()
waitGroup.Wait()

checkNodePeerExists(t, ps.peerState, tt.pid)

Expand Down Expand Up @@ -190,11 +203,18 @@ func TestPeerSetDiscovered(t *testing.T) {
// reserved nodes should not increase the numOut count
checkPeerStateSetNumOut(t, ps.peerState, testSetID, 0)

handler.AddPeer(0, discovered1)
handler.AddPeer(0, discovered1)
handler.AddPeer(0, discovered2)
var waitGroup sync.WaitGroup

time.Sleep(200 * time.Millisecond)
waitGroup.Add(3)
go func() {
handler.AddPeer(0, discovered1)
handler.AddPeer(0, discovered1)
handler.AddPeer(0, discovered2)
waitGroup.Done()
waitGroup.Done()
waitGroup.Done()
}()
waitGroup.Wait()

checkNodePeerExists(t, ps.peerState, discovered1)
checkNodePeerExists(t, ps.peerState, discovered2)
Expand Down Expand Up @@ -227,9 +247,16 @@ func TestReAllocAfterBanned(t *testing.T) {

// We ban a node by setting its reputation under the threshold.
rep := newReputationChange(BannedThresholdValue-1, "")
handler.ReportPeer(rep, peer1)

time.Sleep(time.Millisecond * 100)
var waitGroup sync.WaitGroup

waitGroup.Add(1)
go func() {
handler.ReportPeer(rep, peer1)
waitGroup.Done()
}()
waitGroup.Wait()

checkMessageStatus(t, <-ps.resultMsgCh, Drop)

// banning a incoming peer should decrease the numIn count by 1
Expand Down Expand Up @@ -276,8 +303,14 @@ func TestRemovePeer(t *testing.T) {
require.Len(t, ps.peerState.nodes, 2)
checkPeerStateSetNumOut(t, ps.peerState, testSetID, 2)

handler.RemovePeer(testSetID, discovered1, discovered2)
time.Sleep(200 * time.Millisecond)
var waitGroup sync.WaitGroup

waitGroup.Add(1)
go func() {
handler.RemovePeer(testSetID, discovered1, discovered2)
waitGroup.Done()
}()
waitGroup.Wait()

require.Len(t, ps.resultMsgCh, 2)
for len(ps.resultMsgCh) != 0 {
Expand All @@ -304,9 +337,16 @@ func TestSetReservePeer(t *testing.T) {
require.Len(t, ps.reservedNode, 2)

newRsrPeerSet := peer.IDSlice{reservedPeer, peer.ID("newRsrPeer")}
// add newRsrPeer but remove reservedPeer2
handler.SetReservedPeer(testSetID, newRsrPeerSet...)
time.Sleep(200 * time.Millisecond)

var waitGroup sync.WaitGroup

waitGroup.Add(1)
go func() {
// add newRsrPeer but remove reservedPeer2
handler.SetReservedPeer(testSetID, newRsrPeerSet...)
waitGroup.Done()
}()
waitGroup.Wait()

checkPeerSetReservedNodeCount(t, ps, 2)
for _, p := range newRsrPeerSet {
Expand Down