-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdht_test.go
114 lines (96 loc) · 2.44 KB
/
dht_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package zikade
import (
"sync"
"testing"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/probe-lab/zikade/internal/kadtest"
"github.com/probe-lab/zikade/kadt"
)
func TestNew(t *testing.T) {
h := newTestHost(t, libp2p.NoListenAddrs)
tests := []struct {
name string
cfgBuilder func(*Config) *Config
wantBuilder func(*DHT) *DHT
wantErr bool
}{
{
name: "mode set to server",
cfgBuilder: func(c *Config) *Config {
c.Mode = ModeOptServer
return c
},
wantBuilder: func(dht *DHT) *DHT {
dht.mode = modeServer
return dht
},
wantErr: false,
},
{
name: "mode set to auto client",
cfgBuilder: func(c *Config) *Config {
c.Mode = ModeOptAutoClient
return c
},
wantBuilder: func(dht *DHT) *DHT {
dht.mode = modeClient
return dht
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := DefaultConfig()
d, err := New(h, c)
if err != nil {
t.Fatal(err)
}
got, err := New(h, tt.cfgBuilder(c))
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
want := tt.wantBuilder(d)
assert.Equal(t, want.mode, got.mode)
})
}
}
func TestAddAddresses(t *testing.T) {
ctx := kadtest.CtxShort(t)
top := NewTopology(t)
local := top.AddClient(nil)
remote := top.AddServer(nil)
// local routing table should not contain the node
require.False(t, local.kad.IsRoutable(ctx, kadt.PeerID(remote.host.ID())))
remoteAddrInfo := peer.AddrInfo{
ID: remote.host.ID(),
Addrs: remote.host.Addrs(),
}
require.NotEmpty(t, remoteAddrInfo.ID)
require.NotEmpty(t, remoteAddrInfo.Addrs)
// Add remote's addresss to the local dht
err := local.AddAddresses(ctx, []peer.AddrInfo{remoteAddrInfo}, time.Minute)
require.NoError(t, err)
// the include state machine runs in the background and eventually should add the node to routing table
_, err = top.ExpectRoutingUpdated(ctx, local, remote.host.ID())
require.NoError(t, err)
// the routing table should now contain the node
require.True(t, local.kad.IsRoutable(ctx, kadt.PeerID(remote.host.ID())))
}
func TestDHT_Close_idempotent(t *testing.T) {
d := newTestDHT(t)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
assert.NoError(t, d.Close())
wg.Done()
}()
}
wg.Wait()
}