Skip to content

Commit

Permalink
check for limited connectivity event
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Apr 30, 2024
1 parent 246afbd commit e07c950
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
7 changes: 4 additions & 3 deletions p2p/host/blank/blank.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ func NewBlankHost(n network.Network, options ...Option) *BlankHost {
}

bh := &BlankHost{
n: n,
cmgr: cfg.cmgr,
mux: mstream.NewMultistreamMuxer[protocol.ID](),
n: n,
cmgr: cfg.cmgr,
mux: mstream.NewMultistreamMuxer[protocol.ID](),
eventbus: cfg.eventBus,
}
if bh.eventbus == nil {
bh.eventbus = eventbus.NewBus(eventbus.WithMetricsTracer(eventbus.NewMetricsTracer()))
Expand Down
28 changes: 26 additions & 2 deletions p2p/protocol/circuitv2/relay/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"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/metrics"
"github.com/libp2p/go-libp2p/core/network"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/stretchr/testify/require"

ma "github.com/multiformats/go-multiaddr"
)
Expand All @@ -49,7 +51,8 @@ func getNetHosts(t *testing.T, ctx context.Context, n int) (hosts []host.Host, u
}

bwr := metrics.NewBandwidthCounter()
netw, err := swarm.NewSwarm(p, ps, eventbus.NewBus(), swarm.WithMetrics(bwr))
bus := eventbus.NewBus()
netw, err := swarm.NewSwarm(p, ps, bus, swarm.WithMetrics(bwr))
if err != nil {
t.Fatal(err)
}
Expand All @@ -70,7 +73,7 @@ func getNetHosts(t *testing.T, ctx context.Context, n int) (hosts []host.Host, u
t.Fatal(err)
}

h := bhost.NewBlankHost(netw)
h := bhost.NewBlankHost(netw, bhost.WithEventBus(bus))

hosts = append(hosts, h)
}
Expand Down Expand Up @@ -145,10 +148,31 @@ func TestBasicRelay(t *testing.T) {
t.Fatal(err)
}

sub, err := hosts[2].EventBus().Subscribe(new(event.EvtPeerConnectednessChanged))
require.NoError(t, err)

err = hosts[2].Connect(ctx, peer.AddrInfo{ID: hosts[0].ID(), Addrs: []ma.Multiaddr{raddr}})
if err != nil {
t.Fatal(err)
}
for {
var e interface{}
select {
case e = <-sub.Out():
case <-time.After(2 * time.Second):
t.Fatal("expected limited connectivity event")
}
evt, ok := e.(event.EvtPeerConnectednessChanged)
if !ok {
t.Fatalf("invalid event: %s", e)
}
if evt.Peer == hosts[0].ID() {
if evt.Connectedness != network.Limited {
t.Fatalf("expected limited connectivity %s", evt.Connectedness)
}
break
}
}

conns := hosts[2].Network().ConnsToPeer(hosts[0].ID())
if len(conns) != 1 {
Expand Down

0 comments on commit e07c950

Please sign in to comment.