Skip to content

Commit

Permalink
Fix flaky libp2p tests (#105)
Browse files Browse the repository at this point in the history
Addresses #103
  • Loading branch information
guillaumemichel authored Aug 18, 2023
1 parent 579cf69 commit 070b692
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions libp2p/libp2pendpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/net/swarm"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/stretchr/testify/require"

"github.com/plprobelab/go-kademlia/event"
Expand Down Expand Up @@ -135,12 +136,16 @@ func TestConnections(t *testing.T) {
connectedness, err = endpoints[0].Connectedness(ids[1])
require.NoError(t, err)
require.Equal(t, endpoint.Connected, connectedness)

// test peerinfo
peerinfo, err = endpoints[0].PeerInfo(ids[1])
require.NoError(t, err)
require.Len(t, peerinfo.Addrs, len(addrs[1].Addrs))
for _, addr := range peerinfo.Addrs {
require.Contains(t, addrs[1].Addrs, addr)
// filter out loopback addresses
expectedAddrs := ma.FilterAddrs(addrs[1].Addrs, func(a ma.Multiaddr) bool {
return !manet.IsIPLoopback(a)
})
for _, addr := range expectedAddrs {
require.Contains(t, peerinfo.Addrs, addr, addr.String(), expectedAddrs)
}
peerinfo, err = endpoints[0].PeerInfo(ids[2])
require.NoError(t, err)
Expand Down Expand Up @@ -181,7 +186,7 @@ func TestAsyncDial(t *testing.T) {
// AsyncDialAndReport adds the dial action to the event queue, so we
// need to run the scheduler
for !scheds[0].RunOne(ctx) {
time.Sleep(time.Millisecond)
scheds[0].Clock().Sleep(time.Millisecond)
}
wg.Done()
}()
Expand Down Expand Up @@ -216,7 +221,7 @@ func TestAsyncDial(t *testing.T) {
// AsyncDialAndReport adds the dial action to the event queue, so we
// need to run the scheduler
for !scheds[0].RunOne(ctx) {
time.Sleep(time.Millisecond)
scheds[0].Clock().Sleep(time.Millisecond)
}
wg.Done()
}()
Expand Down Expand Up @@ -327,15 +332,15 @@ func TestSuccessfulRequest(t *testing.T) {
go func() {
// run server 1
for !scheds[1].RunOne(ctx) {
time.Sleep(time.Millisecond)
scheds[1].Clock().Sleep(time.Millisecond)
}
require.False(t, scheds[1].RunOne(ctx)) // only 1 action should run on server
wg.Done()
}()
go func() {
// timeout is queued in the scheduler 0
for !scheds[0].RunOne(ctx) {
time.Sleep(time.Millisecond)
scheds[0].Clock().Sleep(time.Millisecond)
}
require.False(t, scheds[0].RunOne(ctx))
wg.Done()
Expand Down Expand Up @@ -384,7 +389,7 @@ func TestReqUnknownPeer(t *testing.T) {
go func() {
// timeout is queued in the scheduler 0
for !scheds[0].RunOne(ctx) {
time.Sleep(time.Millisecond)
scheds[0].Clock().Sleep(time.Millisecond)
}
require.False(t, scheds[0].RunOne(ctx))
wg.Done()
Expand Down Expand Up @@ -425,7 +430,7 @@ func TestReqTimeout(t *testing.T) {
go func() {
// timeout is queued in the scheduler 0
for !scheds[0].RunOne(ctx) {
time.Sleep(1 * time.Millisecond)
scheds[0].Clock().Sleep(time.Millisecond)
}
require.False(t, scheds[0].RunOne(ctx))
wg.Done()
Expand Down Expand Up @@ -473,7 +478,7 @@ func TestReqHandlerError(t *testing.T) {
wg.Add(2)
go func() {
for !scheds[1].RunOne(ctx) {
time.Sleep(time.Millisecond)
scheds[1].Clock().Sleep(time.Millisecond)
}
require.False(t, scheds[1].RunOne(ctx))
cancel()
Expand All @@ -482,7 +487,7 @@ func TestReqHandlerError(t *testing.T) {
go func() {
// timeout is queued in the scheduler 0
for !scheds[0].RunOne(ctx) {
time.Sleep(time.Millisecond)
scheds[0].Clock().Sleep(time.Millisecond)
}
require.False(t, scheds[0].RunOne(ctx))
wg.Done()
Expand Down Expand Up @@ -527,7 +532,7 @@ func TestReqHandlerReturnsWrongType(t *testing.T) {
wg.Add(2)
go func() {
for !scheds[1].RunOne(ctx) {
time.Sleep(time.Millisecond)
scheds[1].Clock().Sleep(time.Millisecond)
}
require.False(t, scheds[1].RunOne(ctx))
cancel()
Expand All @@ -536,7 +541,7 @@ func TestReqHandlerReturnsWrongType(t *testing.T) {
go func() {
// timeout is queued in the scheduler 0
for !scheds[0].RunOne(ctx) {
time.Sleep(time.Millisecond)
scheds[0].Clock().Sleep(time.Millisecond)
}
require.False(t, scheds[0].RunOne(ctx))
wg.Done()
Expand Down

0 comments on commit 070b692

Please sign in to comment.