Merge pull request #1200 from libp2p/fix-flaky-id-test

fix flaky TestFastDisconnect identify test
This commit is contained in:
Marten Seemann 2021-09-28 15:31:37 +01:00 committed by GitHub
commit 72b58240e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 19 deletions

View File

@ -5,12 +5,13 @@ import (
"testing" "testing"
"time" "time"
blhost "github.com/libp2p/go-libp2p-blankhost"
"github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/require"
blhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing" swarmt "github.com/libp2p/go-libp2p-swarm/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestFastDisconnect(t *testing.T) { func TestFastDisconnect(t *testing.T) {
@ -27,7 +28,7 @@ func TestFastDisconnect(t *testing.T) {
sync := make(chan struct{}) sync := make(chan struct{})
target.SetStreamHandler(ID, func(s network.Stream) { target.SetStreamHandler(ID, func(s network.Stream) {
// Wait till the stream is setup on both sides. // Wait till the stream is set up on both sides.
select { select {
case <-sync: case <-sync:
case <-ctx.Done(): case <-ctx.Done():
@ -35,11 +36,16 @@ func TestFastDisconnect(t *testing.T) {
} }
// Kill the connection, and make sure we're completely disconnected. // Kill the connection, and make sure we're completely disconnected.
s.Conn().Close() assert.Eventually(t,
for target.Network().Connectedness(s.Conn().RemotePeer()) == network.Connected { func() bool {
// let something else run for _, conn := range target.Network().ConnsToPeer(s.Conn().RemotePeer()) {
time.Sleep(time.Millisecond) conn.Close()
} }
return target.Network().Connectedness(s.Conn().RemotePeer()) != network.Connected
},
2*time.Second,
time.Millisecond,
)
// Now try to handle the response. // Now try to handle the response.
// This should not block indefinitely, or panic, or anything like that. // This should not block indefinitely, or panic, or anything like that.
// //
@ -57,14 +63,10 @@ func TestFastDisconnect(t *testing.T) {
source := blhost.NewBlankHost(swarmt.GenSwarm(t)) source := blhost.NewBlankHost(swarmt.GenSwarm(t))
defer source.Close() defer source.Close()
err = source.Connect(ctx, peer.AddrInfo{ID: target.ID(), Addrs: target.Addrs()}) // only connect to the first address, to make sure we only end up with one connection
if err != nil { require.NoError(t, source.Connect(ctx, peer.AddrInfo{ID: target.ID(), Addrs: target.Addrs()}))
t.Fatal(err)
}
s, err := source.NewStream(ctx, target.ID(), ID) s, err := source.NewStream(ctx, target.ID(), ID)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
select { select {
case sync <- struct{}{}: case sync <- struct{}{}:
case <-ctx.Done(): case <-ctx.Done():
@ -77,7 +79,5 @@ func TestFastDisconnect(t *testing.T) {
t.Fatal(ctx.Err()) t.Fatal(ctx.Err())
} }
// double-check to make sure we didn't actually timeout somewhere. // double-check to make sure we didn't actually timeout somewhere.
if ctx.Err() != nil { require.NoError(t, ctx.Err())
t.Fatal(ctx.Err())
}
} }