|
8 | 8 | "github.com/benbjohnson/clock"
|
9 | 9 | "github.com/libp2p/go-libp2p"
|
10 | 10 | "github.com/libp2p/go-libp2p/core/peer"
|
| 11 | + "github.com/libp2p/go-libp2p/p2p/net/swarm" |
11 | 12 | "github.com/plprobelab/zikade/internal/coord"
|
12 | 13 | "github.com/plprobelab/zikade/kadt"
|
13 | 14 | "github.com/stretchr/testify/require"
|
@@ -41,7 +42,7 @@ func (t *Topology) AddServer(cfg *Config) *DHT {
|
41 | 42 |
|
42 | 43 | listenAddr := libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")
|
43 | 44 |
|
44 |
| - h, err := libp2p.New(listenAddr) |
| 45 | + h, err := libp2p.New(append(t.hostOpts(), listenAddr)...) |
45 | 46 | require.NoError(t.tb, err)
|
46 | 47 |
|
47 | 48 | t.tb.Cleanup(func() {
|
@@ -79,7 +80,7 @@ func (t *Topology) AddServer(cfg *Config) *DHT {
|
79 | 80 | func (t *Topology) AddClient(cfg *Config) *DHT {
|
80 | 81 | t.tb.Helper()
|
81 | 82 |
|
82 |
| - h, err := libp2p.New(libp2p.NoListenAddrs) |
| 83 | + h, err := libp2p.New(append(t.hostOpts(), libp2p.NoListenAddrs)...) |
83 | 84 | require.NoError(t.tb, err)
|
84 | 85 |
|
85 | 86 | t.tb.Cleanup(func() {
|
@@ -112,6 +113,22 @@ func (t *Topology) AddClient(cfg *Config) *DHT {
|
112 | 113 | return d
|
113 | 114 | }
|
114 | 115 |
|
| 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 | + |
115 | 132 | func (t *Topology) makeid(d *DHT) string {
|
116 | 133 | return kadt.PeerID(d.host.ID()).String()
|
117 | 134 | }
|
|
0 commit comments