-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpeertaskqueue_test.go
349 lines (297 loc) · 8.75 KB
/
peertaskqueue_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package peertaskqueue
import (
"fmt"
"math"
"math/rand"
"sort"
"strings"
"testing"
"github.com/ipfs/go-peertaskqueue/peertask"
"github.com/ipfs/go-peertaskqueue/testutil"
"github.com/libp2p/go-libp2p/core/peer"
)
func TestPushPop(t *testing.T) {
ptq := New()
partners := testutil.GeneratePeers(2)
alphabet := strings.Split("abcdefghijklmnopqrstuvwxyz", "")
vowels := strings.Split("aeiou", "")
consonants := func() []string {
var out []string
for _, letter := range alphabet {
skip := false
for _, vowel := range vowels {
if letter == vowel {
skip = true
}
}
if !skip {
out = append(out, letter)
}
}
return out
}()
sort.Strings(alphabet)
sort.Strings(vowels)
sort.Strings(consonants)
// add a bunch of blocks. cancel some. drain the queue. the queue should only have the kept tasks
for _, partner := range partners {
for _, index := range rand.Perm(len(alphabet)) { // add blocks for all letters
letter := alphabet[index]
t.Log(letter)
// add tasks out of order, but with in-order priority
ptq.PushTasks(partner, peertask.Task{Topic: letter, Priority: math.MaxInt32 - index})
}
}
ptq.Clear(partners[1])
partner, cleared := partners[0], partners[1]
for _, consonant := range consonants {
ptq.Remove(consonant, partner)
}
ptq.FullThaw()
var out []string
for {
pid, received, _ := ptq.PopTasks(100)
if pid == cleared {
t.Fatal("task from cleared peer shows up")
}
if len(received) == 0 {
break
}
for _, task := range received {
out = append(out, task.Topic.(string))
}
}
// Tasks popped should already be in correct order
for i, expected := range vowels {
if out[i] != expected {
t.Fatal("received", out[i], "expected", expected)
}
}
}
func TestFreezeUnfreeze(t *testing.T) {
ptq := New()
peers := testutil.GeneratePeers(4)
a := peers[0]
b := peers[1]
c := peers[2]
d := peers[3]
// Push 5 blocks to each peer
for i := 0; i < 5; i++ {
is := fmt.Sprint(i)
ptq.PushTasks(a, peertask.Task{Topic: is, Work: 1})
ptq.PushTasks(b, peertask.Task{Topic: is, Work: 1})
ptq.PushTasks(c, peertask.Task{Topic: is, Work: 1})
ptq.PushTasks(d, peertask.Task{Topic: is, Work: 1})
}
// now, pop off four tasks, there should be one from each
matchNTasks(t, ptq, 4, a.String(), b.String(), c.String(), d.String())
ptq.Remove("1", b)
// b should be frozen, causing it to get skipped in the rotation
matchNTasks(t, ptq, 3, a.String(), c.String(), d.String())
ptq.ThawRound()
matchNTasks(t, ptq, 1, b.String())
// remove none existent task
ptq.Remove("-1", b)
// b should not be frozen
matchNTasks(t, ptq, 4, a.String(), b.String(), c.String(), d.String())
}
func TestFreezeUnfreezeNoFreezingOption(t *testing.T) {
ptq := New(IgnoreFreezing(true))
peers := testutil.GeneratePeers(4)
a := peers[0]
b := peers[1]
c := peers[2]
d := peers[3]
// Have each push some blocks
for i := 0; i < 5; i++ {
is := fmt.Sprint(i)
ptq.PushTasks(a, peertask.Task{Topic: is, Work: 1})
ptq.PushTasks(b, peertask.Task{Topic: is, Work: 1})
ptq.PushTasks(c, peertask.Task{Topic: is, Work: 1})
ptq.PushTasks(d, peertask.Task{Topic: is, Work: 1})
}
// now, pop off four tasks, there should be one from each
matchNTasks(t, ptq, 4, a.String(), b.String(), c.String(), d.String())
ptq.Remove("1", b)
// b should not be frozen, so it wont get skipped in the rotation
matchNTasks(t, ptq, 4, a.String(), b.String(), c.String(), d.String())
}
// This test checks that ordering of peers is correct
func TestPeerOrder(t *testing.T) {
ptq := New()
peers := testutil.GeneratePeers(3)
a := peers[0]
b := peers[1]
c := peers[2]
ptq.PushTasks(a, peertask.Task{Topic: "1", Work: 3, Priority: 2})
ptq.PushTasks(a, peertask.Task{Topic: "2", Work: 1, Priority: 1})
ptq.PushTasks(b, peertask.Task{Topic: "3", Work: 1, Priority: 3})
ptq.PushTasks(b, peertask.Task{Topic: "4", Work: 3, Priority: 2})
ptq.PushTasks(b, peertask.Task{Topic: "5", Work: 1, Priority: 1})
ptq.PushTasks(c, peertask.Task{Topic: "6", Work: 2, Priority: 2})
ptq.PushTasks(c, peertask.Task{Topic: "7", Work: 2, Priority: 1})
// All peers have nothing in their active queue, so equal chance of any
// peer being chosen
var ps []string
var ids []string
for i := 0; i < 3; i++ {
p, tasks, _ := ptq.PopTasks(1)
ps = append(ps, p.String())
ids = append(ids, fmt.Sprint(tasks[0].Topic))
}
matchArrays(t, ps, []string{a.String(), b.String(), c.String()})
matchArrays(t, ids, []string{"1", "3", "6"})
// Active queues:
// a: 3 Pending: [1]
// b: 1 Pending: [3, 1]
// c: 2 Pending: [2]
// So next peer should be b (least work in active queue)
p, tsk, pending := ptq.PopTasks(1)
if len(tsk) != 1 || p != b || tsk[0].Topic != "4" {
t.Fatal("Expected ID 4 from peer b")
}
if pending != 1 {
t.Fatal("Expected pending work to be 1")
}
// Active queues:
// a: 3 Pending: [1]
// b: 1 + 3 Pending: [1]
// c: 2 Pending: [2]
// So next peer should be c (least work in active queue)
p, tsk, pending = ptq.PopTasks(1)
if len(tsk) != 1 || p != c || tsk[0].Topic != "7" {
t.Fatal("Expected ID 7 from peer c")
}
if pending != 0 {
t.Fatal("Expected pending work to be 0")
}
// Active queues:
// a: 3 Pending: [1]
// b: 1 + 3 Pending: [1]
// c: 2 + 2
// So next peer should be a (least work in active queue)
p, tsk, pending = ptq.PopTasks(1)
if len(tsk) != 1 || p != a || tsk[0].Topic != "2" {
t.Fatal("Expected ID 2 from peer a")
}
if pending != 0 {
t.Fatal("Expected pending work to be 0")
}
// Active queues:
// a: 3 + 1
// b: 1 + 3 Pending: [1]
// c: 2 + 2
// a & c have no more pending tasks, so next peer should be b
p, tsk, pending = ptq.PopTasks(1)
if len(tsk) != 1 || p != b || tsk[0].Topic != "5" {
t.Fatal("Expected ID 5 from peer b")
}
if pending != 0 {
t.Fatal("Expected pending work to be 0")
}
// Active queues:
// a: 3 + 1
// b: 1 + 3 + 1
// c: 2 + 2
// No more pending tasks, so next pop should return nothing
_, tsk, pending = ptq.PopTasks(1)
if len(tsk) != 0 {
t.Fatal("Expected no more tasks")
}
if pending != 0 {
t.Fatal("Expected pending work to be 0")
}
}
func TestHooks(t *testing.T) {
var peersAdded []string
var peersRemoved []string
onPeerAdded := func(p peer.ID) {
peersAdded = append(peersAdded, p.String())
}
onPeerRemoved := func(p peer.ID) {
peersRemoved = append(peersRemoved, p.String())
}
ptq := New(OnPeerAddedHook(onPeerAdded), OnPeerRemovedHook(onPeerRemoved))
peers := testutil.GeneratePeers(2)
a := peers[0]
b := peers[1]
ptq.PushTasks(a, peertask.Task{Topic: "1"})
ptq.PushTasks(b, peertask.Task{Topic: "2"})
expected := []string{a.String(), b.String()}
sort.Strings(expected)
sort.Strings(peersAdded)
if len(peersAdded) != len(expected) {
t.Fatal("Incorrect number of peers added")
}
for i, s := range peersAdded {
if expected[i] != s {
t.Fatal("unexpected peer", s, expected[i])
}
}
p, task, _ := ptq.PopTasks(100)
ptq.TasksDone(p, task...)
p, task, _ = ptq.PopTasks(100)
ptq.TasksDone(p, task...)
ptq.PopTasks(100)
ptq.PopTasks(100)
sort.Strings(peersRemoved)
if len(peersRemoved) != len(expected) {
t.Fatal("Incorrect number of peers removed")
}
for i, s := range peersRemoved {
if expected[i] != s {
t.Fatal("unexpected peer", s, expected[i])
}
}
}
func TestCleaningUpQueues(t *testing.T) {
ptq := New()
peer := testutil.GeneratePeers(1)[0]
var peerTasks []peertask.Task
for i := 0; i < 5; i++ {
is := fmt.Sprint(i)
peerTasks = append(peerTasks, peertask.Task{Topic: is})
}
// push a block, pop a block, complete everything, should be removed
ptq.PushTasks(peer, peerTasks...)
p, task, _ := ptq.PopTasks(100)
ptq.TasksDone(p, task...)
_, task, _ = ptq.PopTasks(100)
if len(task) != 0 || len(ptq.peerTrackers) > 0 || ptq.pQueue.Len() > 0 {
t.Fatal("PeerTracker should have been removed because it's idle")
}
// push a block, remove each of its entries, should be removed
ptq.PushTasks(peer, peerTasks...)
for _, peerTask := range peerTasks {
ptq.Remove(peerTask.Topic, peer)
}
_, task, _ = ptq.PopTasks(100)
if len(task) != 0 || len(ptq.peerTrackers) > 0 || ptq.pQueue.Len() > 0 {
t.Fatal("Partner should have been removed because it's idle")
}
}
func matchNTasks(t *testing.T, ptq *PeerTaskQueue, n int, expected ...string) {
var targets []string
for i := 0; i < n; i++ {
p, tsk, _ := ptq.PopTasks(1)
if len(tsk) != 1 {
t.Fatal("expected 1 task at a time")
}
targets = append(targets, p.String())
}
matchArrays(t, expected, targets)
}
func matchArrays(t *testing.T, str1, str2 []string) {
if len(str1) != len(str2) {
t.Fatal("array lengths did not match", str1, str2)
}
sort.Strings(str1)
sort.Strings(str2)
t.Log(str1)
t.Log(str2)
for i, s := range str2 {
if str1[i] != s {
t.Fatal("unexpected peer", s, str1[i])
}
}
}