Skip to content

Commit

Permalink
Merge pull request #1147 from lthibault/enhancement/stateful-addr-events
Browse files Browse the repository at this point in the history
Make BasicHost.evtLocalAddrsUpdated event emitter stateful.
  • Loading branch information
Stebalien committed Aug 4, 2021
2 parents 159a6fc + fbcfca7 commit 989fba5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func NewHost(ctx context.Context, n network.Network, opts *HostOpts) (*BasicHost
if h.emitters.evtLocalProtocolsUpdated, err = h.eventbus.Emitter(&event.EvtLocalProtocolsUpdated{}); err != nil {
return nil, err
}
if h.emitters.evtLocalAddrsUpdated, err = h.eventbus.Emitter(&event.EvtLocalAddressesUpdated{}); err != nil {
if h.emitters.evtLocalAddrsUpdated, err = h.eventbus.Emitter(&event.EvtLocalAddressesUpdated{}, eventbus.Stateful); err != nil {
return nil, err
}

Expand Down
21 changes: 21 additions & 0 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -728,6 +729,26 @@ func TestAddrChangeImmediatelyIfAddressNonEmpty(t *testing.T) {
require.Equal(t, taddrs, rc.Addrs)
}

func TestStatefulAddrEvents(t *testing.T) {
ctx := context.Background()
h := New(swarmt.GenSwarm(t, ctx))
defer h.Close()

sub, err := h.EventBus().Subscribe(&event.EvtLocalAddressesUpdated{}, eventbus.BufSize(10))
if err != nil {
t.Error(err)
}
defer sub.Close()

select {
case v := <-sub.Out():
assert.NotNil(t, v)

case <-time.After(time.Second * 5):
t.Error("timed out waiting for event")
}
}

func TestHostAddrChangeDetection(t *testing.T) {
// This test uses the address factory to provide several
// sets of listen addresses for the host. It advances through
Expand Down

0 comments on commit 989fba5

Please sign in to comment.