-
Notifications
You must be signed in to change notification settings - Fork 32
/
hashtable_test.go
177 lines (144 loc) · 3.37 KB
/
hashtable_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
171
172
173
174
175
176
177
package kademlia
import (
"bytes"
"math"
"net"
"testing"
"github.com/stretchr/testify/assert"
)
// Create a new node and bootstrap it. All nodes in the network know of a
// single node closer to the original node. This continues until every k bucket
// is occupied.
func TestFindNodeAllBuckets(t *testing.T) {
networking := newMockNetworking()
id := getIDWithValues(0)
dht, _ := NewDHT(getInMemoryStore(), &Options{
ID: id,
Port: "3000",
IP: "0.0.0.0",
BootstrapNodes: []*NetworkNode{{
ID: getZerodIDWithNthByte(0, byte(math.Pow(2, 7))),
Port: 3001,
IP: net.ParseIP("0.0.0.0"),
},
},
})
dht.networking = networking
dht.CreateSocket()
go func() {
dht.Listen()
}()
var k = 0
var i = 6
go func() {
for {
query := <-networking.recv
if query == nil {
return
}
res := mockFindNodeResponse(query, getZerodIDWithNthByte(k, byte(math.Pow(2, float64(i)))))
i--
if i < 0 {
i = 7
k++
}
if k > 19 {
k = 19
}
networking.send <- res
}
}()
dht.Bootstrap()
for _, v := range dht.ht.RoutingTable {
assert.Equal(t, 1, len(v))
}
dht.Disconnect()
}
// Tests timing out of nodes in a bucket. DHT bootstraps networks and learns
// about 20 subsequent nodes in the same bucket. Upon attempting to add the 21st
// node to the now full bucket, we should receive a ping to the very first node
// added in order to determine if it is still alive.
func TestAddNodeTimeout(t *testing.T) {
networking := newMockNetworking()
id := getIDWithValues(0)
done := make(chan (int))
pinged := make(chan (int))
dht, _ := NewDHT(getInMemoryStore(), &Options{
ID: id,
Port: "3000",
IP: "0.0.0.0",
BootstrapNodes: []*NetworkNode{{
ID: getZerodIDWithNthByte(1, byte(255)),
Port: 3001,
IP: net.ParseIP("0.0.0.0"),
},
},
})
dht.networking = networking
dht.CreateSocket()
go func() {
dht.Listen()
}()
var nodesAdded = 1
var firstNode []byte
var lastNode []byte
go func() {
for {
query := <-networking.recv
if query == nil {
return
}
switch query.Type {
case messageTypeFindNode:
id := getIDWithValues(0)
if nodesAdded > k+1 {
close(done)
return
}
if nodesAdded == 1 {
firstNode = id
}
if nodesAdded == k {
lastNode = id
}
id[1] = byte(255 - nodesAdded)
nodesAdded++
res := mockFindNodeResponse(query, id)
networking.send <- res
case messageTypePing:
assert.Equal(t, messageTypePing, query.Type)
assert.Equal(t, getZerodIDWithNthByte(1, byte(255)), query.Receiver.ID)
close(pinged)
}
}
}()
dht.Bootstrap()
// ensure the first node in the table is the second node contacted, and the
// last is the last node contacted
assert.Equal(t, 0, bytes.Compare(dht.ht.RoutingTable[b-9][0].ID, firstNode))
assert.Equal(t, 0, bytes.Compare(dht.ht.RoutingTable[b-9][19].ID, lastNode))
<-done
<-pinged
dht.Disconnect()
}
func TestGetRandomIDFromBucket(t *testing.T) {
id := getIDWithValues(0)
dht, _ := NewDHT(getInMemoryStore(), &Options{
ID: id,
Port: "3000",
IP: "0.0.0.0",
})
dht.CreateSocket()
go func() {
dht.Listen()
}()
// Bytes should be equal up to the bucket index that the random ID was
// generated for, and random afterwards
for i := 0; i < b/8; i++ {
r := dht.ht.getRandomIDFromBucket(i * 8)
for j := 0; j < i; j++ {
assert.Equal(t, byte(0), r[j])
}
}
dht.Disconnect()
}