Skip to content

Commit

Permalink
fix flaky dial circuit address
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Aug 10, 2024
1 parent 025fb33 commit 5930779
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
28 changes: 10 additions & 18 deletions libp2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -429,24 +428,15 @@ func TestMain(m *testing.M) {
}

func TestDialCircuitAddrWithWrappedResourceManager(t *testing.T) {
relay, err := New(EnableRelayService())
relay, err := New(EnableRelayService(), ForceReachabilityPublic())
require.NoError(t, err)
defer relay.Close()

// Fake that the relay is publicly reachable
emitterForRelay, err := relay.EventBus().Emitter(&event.EvtLocalReachabilityChanged{})
require.NoError(t, err)
defer emitterForRelay.Close()
emitterForRelay.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityPublic})

peerBehindRelay, err := New(EnableAutoRelayWithStaticRelays([]peer.AddrInfo{{ID: relay.ID(), Addrs: relay.Addrs()}}))
peerBehindRelay, err := New(
EnableAutoRelayWithStaticRelays([]peer.AddrInfo{{ID: relay.ID(), Addrs: relay.Addrs()}}),
ForceReachabilityPrivate())
require.NoError(t, err)
defer peerBehindRelay.Close()
// Emit an event to tell this peer it is private
emitterForPeerBehindRelay, err := peerBehindRelay.EventBus().Emitter(&event.EvtLocalReachabilityChanged{})
require.NoError(t, err)
defer emitterForPeerBehindRelay.Close()
emitterForPeerBehindRelay.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityPrivate})

// Use a wrapped resource manager to test that the circuit dialing works
// with it. Look at the PR introducing this test for context
Expand All @@ -467,10 +457,12 @@ func TestDialCircuitAddrWithWrappedResourceManager(t *testing.T) {
)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
res := <-ping.Ping(ctx, h, peerBehindRelay.ID())
require.NoError(t, res.Error)
defer cancel()
require.Eventually(t, func() bool {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
res := <-ping.Ping(ctx, h, peerBehindRelay.ID())
return res.Error == nil
}, 5*time.Second, 50*time.Millisecond)
}

func TestHostAddrsFactoryAddsCerthashes(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestMultipleClose(t *testing.T) {
require.NoError(t, h.Close())
require.NoError(t, h.Close())
h2, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
defer h2.Close()
require.Error(t, h.Connect(context.Background(), peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()}))
h.Network().Peerstore().AddAddrs(h2.ID(), h2.Addrs(), peerstore.PermanentAddrTTL)
Expand Down

0 comments on commit 5930779

Please sign in to comment.