-
Notifications
You must be signed in to change notification settings - Fork 3
/
notifee_test.go
89 lines (66 loc) · 2.23 KB
/
notifee_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package zikade
import (
"testing"
"time"
"github.com/libp2p/go-libp2p/core/network"
"github.com/probe-lab/zikade/internal/kadtest"
"github.com/libp2p/go-libp2p/core/event"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDHT_consumeNetworkEvents_onEvtLocalReachabilityChanged(t *testing.T) {
newModeDHT := func(m ModeOpt) *DHT {
cfg := DefaultConfig()
cfg.Mode = m
return newTestDHTWithConfig(t, cfg)
}
t.Run("set server", func(t *testing.T) {
d := newModeDHT(ModeOptAutoClient)
d.onEvtLocalReachabilityChanged(event.EvtLocalReachabilityChanged{
Reachability: network.ReachabilityPublic,
})
assert.Equal(t, modeServer, d.mode)
})
t.Run("set client", func(t *testing.T) {
d := newModeDHT(ModeOptAutoClient)
d.onEvtLocalReachabilityChanged(event.EvtLocalReachabilityChanged{
Reachability: network.ReachabilityPrivate,
})
assert.Equal(t, modeClient, d.mode)
})
t.Run("on unknown set client when auto client", func(t *testing.T) {
d := newModeDHT(ModeOptAutoClient)
d.onEvtLocalReachabilityChanged(event.EvtLocalReachabilityChanged{
Reachability: network.ReachabilityUnknown,
})
assert.Equal(t, modeClient, d.mode)
})
t.Run("on unknown set server when auto server", func(t *testing.T) {
d := newModeDHT(ModeOptAutoServer)
d.onEvtLocalReachabilityChanged(event.EvtLocalReachabilityChanged{
Reachability: network.ReachabilityUnknown,
})
assert.Equal(t, modeServer, d.mode)
})
t.Run("handles unknown event gracefully", func(t *testing.T) {
d := newModeDHT(ModeOptAutoServer)
d.onEvtLocalReachabilityChanged(event.EvtLocalReachabilityChanged{
Reachability: network.Reachability(99),
})
assert.Equal(t, modeServer, d.mode)
})
}
func TestDHT_consumeNetworkEvents_onEvtPeerIdentificationCompleted(t *testing.T) {
ctx := kadtest.CtxShort(t)
top := NewTopology(t)
d1 := top.AddServer(nil)
d2 := top.AddServer(nil)
// make sure d1 has the address of d2 in its peerstore
d1.host.Peerstore().AddAddrs(d2.host.ID(), d2.host.Addrs(), time.Minute)
// send the event
d1.onEvtPeerIdentificationCompleted(event.EvtPeerIdentificationCompleted{
Peer: d2.host.ID(),
})
_, err := top.ExpectRoutingUpdated(ctx, d1, d2.host.ID())
require.NoError(t, err)
}