Skip to content

Commit

Permalink
Merge pull request #1278 from libp2p/fix-flaky-notifications-test
Browse files Browse the repository at this point in the history
fix flaky TestNotifications test
  • Loading branch information
marten-seemann authored Jan 3, 2022
2 parents 767629f + fa2c607 commit e144423
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions p2p/net/mock/mock_notif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"

"github.com/stretchr/testify/require"
)

func TestNotifications(t *testing.T) {
Expand Down Expand Up @@ -80,14 +82,10 @@ func TestNotifications(t *testing.T) {
}
}

acceptedStream := make(chan struct{}, 1000)
for _, s := range nets {
s.SetStreamHandler(func(s network.Stream) {
s.Close()
})
}

for _, s := range nets {
s.SetStreamHandler(func(s network.Stream) {
acceptedStream <- struct{}{}
s.Close()
})
}
Expand Down Expand Up @@ -150,9 +148,17 @@ func TestNotifications(t *testing.T) {
str2 := StreamComplement(str1)
n2 := notifiees[str1.Conn().RemotePeer()]

n2.streamState.Lock()
ch2 := n2.streamState.m[str2]
n2.streamState.Unlock()
// make sure the OpenedStream notification was processed first
var ch2 chan struct{}
require.Eventually(t, func() bool {
n2.streamState.Lock()
defer n2.streamState.Unlock()
ch, ok := n2.streamState.m[str2]
if ok {
ch2 = ch
}
return ok
}, time.Second, 10*time.Millisecond)

<-ch2
}
Expand Down

0 comments on commit e144423

Please sign in to comment.