Skip to content

Commit 4a89594

Browse files
authored
fix: flaky test by decreasing local dial timeout (#50)
fixes: #48
1 parent f9b6f32 commit 4a89594

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

diversity_filter_test.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -165,30 +165,43 @@ func (suite *DiversityFilterTestSuite) TestRtPeerIPGroupFilter() {
165165
// TestRTPeerDiversityFilter tests the TrieRTPeerDiversityFilter implementation
166166
func TestRTPeerDiversityFilter(t *testing.T) {
167167
ctx := context.Background()
168-
h, err := libp2p.New()
168+
169+
listenOpt := libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")
170+
171+
h, err := libp2p.New(listenOpt)
169172
require.NoError(t, err)
170173

171174
// create 2 remote peers
172-
h1, err := libp2p.New()
173-
require.NoError(t, err)
174-
h2, err := libp2p.New()
175+
h1, err := libp2p.New(listenOpt)
175176
require.NoError(t, err)
176177

177-
// connect h to h1 and h2
178-
err = h.Connect(ctx, peer.AddrInfo{ID: h1.ID(), Addrs: h1.Addrs()})
179-
require.NoError(t, err)
180-
err = h.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
178+
h2, err := libp2p.New(listenOpt)
181179
require.NoError(t, err)
182180

181+
// clean up after ourselves
182+
t.Cleanup(func() {
183+
require.NoError(t, h.Close())
184+
require.NoError(t, h1.Close())
185+
require.NoError(t, h2.Close())
186+
})
187+
183188
// create peer filter and routing table
184189
peerFilter, err := NewRTPeerDiversityFilter(h, 1, 1)
185190
require.NoError(t, err)
191+
186192
rtcfg := &triert.Config[kadt.Key, kadt.PeerID]{
187193
NodeFilter: peerFilter,
188194
}
189195
rt, err := triert.New[kadt.Key, kadt.PeerID](kadt.PeerID(h.ID()), rtcfg)
190196
require.NoError(t, err)
191197

198+
// connect h to h1 and h2
199+
err = h.Connect(ctx, peer.AddrInfo{ID: h1.ID(), Addrs: h1.Addrs()})
200+
require.NoError(t, err)
201+
202+
err = h.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
203+
require.NoError(t, err)
204+
192205
// try to add h1 to the routing table. succeeds because it is the first peer
193206
success := rt.AddNode(kadt.PeerID(h1.ID()))
194207
require.True(t, success)

topology_test.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/benbjohnson/clock"
99
"github.com/libp2p/go-libp2p"
1010
"github.com/libp2p/go-libp2p/core/peer"
11+
"github.com/libp2p/go-libp2p/p2p/net/swarm"
1112
"github.com/plprobelab/zikade/internal/coord"
1213
"github.com/plprobelab/zikade/kadt"
1314
"github.com/stretchr/testify/require"
@@ -41,7 +42,7 @@ func (t *Topology) AddServer(cfg *Config) *DHT {
4142

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

44-
h, err := libp2p.New(listenAddr)
45+
h, err := libp2p.New(append(t.hostOpts(), listenAddr)...)
4546
require.NoError(t.tb, err)
4647

4748
t.tb.Cleanup(func() {
@@ -79,7 +80,7 @@ func (t *Topology) AddServer(cfg *Config) *DHT {
7980
func (t *Topology) AddClient(cfg *Config) *DHT {
8081
t.tb.Helper()
8182

82-
h, err := libp2p.New(libp2p.NoListenAddrs)
83+
h, err := libp2p.New(append(t.hostOpts(), libp2p.NoListenAddrs)...)
8384
require.NoError(t.tb, err)
8485

8586
t.tb.Cleanup(func() {
@@ -112,6 +113,22 @@ func (t *Topology) AddClient(cfg *Config) *DHT {
112113
return d
113114
}
114115

116+
// hostOpts returns libp2p host options common to DHT clients and servers.
117+
func (t *Topology) hostOpts() []libp2p.Option {
118+
// If two peers simultaneously connect, they could end up in a state where
119+
// one peer is waiting on the connection for the other one, although there
120+
// already exists a valid connection. The libp2p dial loop doesn't recognize
121+
// the new connection immediately, but only after the local dial has timed
122+
// out. By default, the timeout is set to 5s which results in failing tests
123+
// as the tests time out. By setting the timeout to a much lower value, we
124+
// work around the timeout issue. Try to remove the following swarm options
125+
// after https://github.com/libp2p/go-libp2p/issues/2589 was resolved.
126+
localDialTimeout := 100 * time.Millisecond
127+
swarmOpts := libp2p.SwarmOpts(swarm.WithDialTimeoutLocal(localDialTimeout))
128+
129+
return []libp2p.Option{swarmOpts}
130+
}
131+
115132
func (t *Topology) makeid(d *DHT) string {
116133
return kadt.PeerID(d.host.ID()).String()
117134
}

0 commit comments

Comments
 (0)