Skip to content

Commit

Permalink
fix: flaky test by decreasing local dial timeout (#50)
Browse files Browse the repository at this point in the history
fixes: #48
  • Loading branch information
dennis-tra authored Oct 5, 2023
1 parent f9b6f32 commit 4a89594
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
29 changes: 21 additions & 8 deletions diversity_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,30 +165,43 @@ func (suite *DiversityFilterTestSuite) TestRtPeerIPGroupFilter() {
// TestRTPeerDiversityFilter tests the TrieRTPeerDiversityFilter implementation
func TestRTPeerDiversityFilter(t *testing.T) {
ctx := context.Background()
h, err := libp2p.New()

listenOpt := libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")

h, err := libp2p.New(listenOpt)
require.NoError(t, err)

// create 2 remote peers
h1, err := libp2p.New()
require.NoError(t, err)
h2, err := libp2p.New()
h1, err := libp2p.New(listenOpt)
require.NoError(t, err)

// connect h to h1 and h2
err = h.Connect(ctx, peer.AddrInfo{ID: h1.ID(), Addrs: h1.Addrs()})
require.NoError(t, err)
err = h.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
h2, err := libp2p.New(listenOpt)
require.NoError(t, err)

// clean up after ourselves
t.Cleanup(func() {
require.NoError(t, h.Close())
require.NoError(t, h1.Close())
require.NoError(t, h2.Close())
})

// create peer filter and routing table
peerFilter, err := NewRTPeerDiversityFilter(h, 1, 1)
require.NoError(t, err)

rtcfg := &triert.Config[kadt.Key, kadt.PeerID]{
NodeFilter: peerFilter,
}
rt, err := triert.New[kadt.Key, kadt.PeerID](kadt.PeerID(h.ID()), rtcfg)
require.NoError(t, err)

// connect h to h1 and h2
err = h.Connect(ctx, peer.AddrInfo{ID: h1.ID(), Addrs: h1.Addrs()})
require.NoError(t, err)

err = h.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
require.NoError(t, err)

// try to add h1 to the routing table. succeeds because it is the first peer
success := rt.AddNode(kadt.PeerID(h1.ID()))
require.True(t, success)
Expand Down
21 changes: 19 additions & 2 deletions topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/benbjohnson/clock"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/net/swarm"
"github.com/plprobelab/zikade/internal/coord"
"github.com/plprobelab/zikade/kadt"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -41,7 +42,7 @@ func (t *Topology) AddServer(cfg *Config) *DHT {

listenAddr := libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")

h, err := libp2p.New(listenAddr)
h, err := libp2p.New(append(t.hostOpts(), listenAddr)...)
require.NoError(t.tb, err)

t.tb.Cleanup(func() {
Expand Down Expand Up @@ -79,7 +80,7 @@ func (t *Topology) AddServer(cfg *Config) *DHT {
func (t *Topology) AddClient(cfg *Config) *DHT {
t.tb.Helper()

h, err := libp2p.New(libp2p.NoListenAddrs)
h, err := libp2p.New(append(t.hostOpts(), libp2p.NoListenAddrs)...)
require.NoError(t.tb, err)

t.tb.Cleanup(func() {
Expand Down Expand Up @@ -112,6 +113,22 @@ func (t *Topology) AddClient(cfg *Config) *DHT {
return d
}

// hostOpts returns libp2p host options common to DHT clients and servers.
func (t *Topology) hostOpts() []libp2p.Option {
// If two peers simultaneously connect, they could end up in a state where
// one peer is waiting on the connection for the other one, although there
// already exists a valid connection. The libp2p dial loop doesn't recognize
// the new connection immediately, but only after the local dial has timed
// out. By default, the timeout is set to 5s which results in failing tests
// as the tests time out. By setting the timeout to a much lower value, we
// work around the timeout issue. Try to remove the following swarm options
// after https://github.com/libp2p/go-libp2p/issues/2589 was resolved.
localDialTimeout := 100 * time.Millisecond
swarmOpts := libp2p.SwarmOpts(swarm.WithDialTimeoutLocal(localDialTimeout))

return []libp2p.Option{swarmOpts}
}

func (t *Topology) makeid(d *DHT) string {
return kadt.PeerID(d.host.ID()).String()
}
Expand Down

0 comments on commit 4a89594

Please sign in to comment.