forked from libp2p/go-libp2p-rendezvous
-
Notifications
You must be signed in to change notification settings - Fork 6
/
discovery_test.go
170 lines (138 loc) · 4.3 KB
/
discovery_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package rendezvous
import (
"context"
"math/rand"
"testing"
"time"
"github.com/libp2p/go-libp2p/core/discovery"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)
func getRendezvousDiscovery(hosts []host.Host) []discovery.Discovery {
clients := make([]discovery.Discovery, len(hosts)-1)
rendezvousPeer := hosts[0].ID()
for i, h := range hosts[1:] {
rp := NewRendezvousPoint(h, rendezvousPeer)
rng := rand.New(rand.NewSource(int64(i)))
clients[i] = &rendezvousDiscovery{rp: rp, peerCache: make(map[string]*discoveryCache), rng: rng}
}
return clients
}
func peerChannelToArray(pch <-chan peer.AddrInfo) []peer.AddrInfo {
pi := make([]peer.AddrInfo, len(pch))
peerIndex := 0
for p := range pch {
pi[peerIndex] = p
peerIndex++
}
return pi
}
func checkAvailablePeers(t *testing.T, ctx context.Context, client discovery.Discovery, namespace string, expectedNumPeers int) {
pch, err := client.FindPeers(ctx, namespace)
if err != nil {
t.Fatal(err)
}
pi := peerChannelToArray(pch)
if len(pi) != expectedNumPeers {
t.Fatalf("Expected %d peers", expectedNumPeers)
}
}
func TestDiscoveryClientAdvertiseAndFindPeers(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
m := mocknet.New()
defer m.Close()
// Define parameters
const namespace = "foo1"
const numClients = 4
const ttl = DefaultTTL * time.Second
// Instantiate server and clients
hosts := getRendezvousHosts(t, ctx, m, numClients+1)
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
if err != nil {
t.Fatal(err)
}
defer svc.DB.Close()
clients := getRendezvousDiscovery(hosts)
// Advertise and check one peer
_, err = clients[0].Advertise(ctx, namespace, discovery.TTL(ttl))
if err != nil {
t.Fatal(err)
}
checkAvailablePeers(t, ctx, clients[0], namespace, 1)
// Advertise and check the rest of the peers incrementally
for i, client := range clients[1:] {
if _, err = client.Advertise(ctx, namespace, discovery.TTL(ttl)); err != nil {
t.Fatal(err)
}
checkAvailablePeers(t, ctx, client, namespace, i+2)
}
// Check that the first peer can get all the new records
checkAvailablePeers(t, ctx, clients[0], namespace, numClients)
}
func TestDiscoveryClientExpiredCachedRecords(t *testing.T) {
BaseDiscoveryClientCacheExpirationTest(t, true)
}
func TestDiscoveryClientExpiredManyCachedRecords(t *testing.T) {
BaseDiscoveryClientCacheExpirationTest(t, false)
}
func BaseDiscoveryClientCacheExpirationTest(t *testing.T, onlyRequestFromCache bool) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Define parameters
const numShortLivedRegs = 5
const everyIthRegIsLongTTL = 2
const numBaseRegs = numShortLivedRegs * everyIthRegIsLongTTL
const namespace = "foo1"
const longTTL = DefaultTTL * time.Second
const shortTTL = 2 * time.Second
m := mocknet.New()
defer m.Close()
// Instantiate server and clients
hosts := getRendezvousHosts(t, ctx, m, numBaseRegs+3)
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
if err != nil {
t.Fatal(err)
}
defer svc.DB.Close()
clients := getRendezvousDiscovery(hosts)
// Advertise most clients
for i, client := range clients[2:] {
ttl := shortTTL
if i%everyIthRegIsLongTTL == 0 {
ttl = longTTL
}
if _, err = client.Advertise(ctx, namespace, discovery.TTL(ttl)); err != nil {
t.Fatal(err)
}
}
// Find peers from an unrelated client (results should be cached)
pch, err := clients[0].FindPeers(ctx, namespace)
if err != nil {
t.Fatal(err)
}
pi := peerChannelToArray(pch)
if len(pi) != numBaseRegs {
t.Fatalf("expected %d registrations", numBaseRegs)
}
// Advertise from a new unrelated peer
if _, err := clients[1].Advertise(ctx, namespace, discovery.TTL(longTTL)); err != nil {
t.Fatal(err)
}
// Wait for cache expiration
time.Sleep(shortTTL + time.Second)
// Check if number of retrieved records matches caching expectations after expiration
expectedNumClients := numShortLivedRegs
if !onlyRequestFromCache {
expectedNumClients++
}
pch, err = clients[0].FindPeers(ctx, namespace, discovery.Limit(expectedNumClients))
if err != nil {
t.Fatal(err)
}
pi = peerChannelToArray(pch)
if len(pi) != expectedNumClients {
t.Fatalf("received an incorrect number of records: %d", len(pi))
}
}