mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-10-25 00:50:36 +08:00
Make BasicHost.evtLocalAddrsUpdated event emitter stateful.
This commit is contained in:
@@ -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 {
|
if h.emitters.evtLocalProtocolsUpdated, err = h.eventbus.Emitter(&event.EvtLocalProtocolsUpdated{}); err != nil {
|
||||||
return nil, err
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import (
|
|||||||
|
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
madns "github.com/multiformats/go-multiaddr-dns"
|
madns "github.com/multiformats/go-multiaddr-dns"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -728,6 +729,26 @@ func TestAddrChangeImmediatelyIfAddressNonEmpty(t *testing.T) {
|
|||||||
require.Equal(t, taddrs, rc.Addrs)
|
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) {
|
func TestHostAddrChangeDetection(t *testing.T) {
|
||||||
// This test uses the address factory to provide several
|
// This test uses the address factory to provide several
|
||||||
// sets of listen addresses for the host. It advances through
|
// sets of listen addresses for the host. It advances through
|
||||||
|
|||||||
Reference in New Issue
Block a user