-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconn_test.go
523 lines (456 loc) · 15.3 KB
/
conn_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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Copyright 2013 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package redis
import (
"math/rand"
"os"
"reflect"
"testing"
"time"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/storage"
"github.com/chihaya/chihaya/storage/tracker"
)
func createTestConn() tracker.Conn {
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
panicOnErr(err)
conf := &testConfig.Cache
testPool, err := tracker.Open(conf)
panicOnErr(err)
newConn, err := testPool.Get()
panicOnErr(err)
return newConn
}
func TestFindUserSuccess(t *testing.T) {
conn := createTestConn()
testUser := createTestUser()
panicOnErr(conn.AddUser(testUser))
foundUser, found, err := conn.FindUser(testUser.Passkey)
panicOnErr(err)
if !found {
t.Error("user not found", testUser)
}
if *foundUser != *testUser {
t.Error("found user mismatch", *foundUser, testUser)
}
// Cleanup
panicOnErr(conn.RemoveUser(testUser))
}
func TestFindUserFail(t *testing.T) {
conn := createTestConn()
testUser := createTestUser()
foundUser, found, err := conn.FindUser(testUser.Passkey)
panicOnErr(err)
if found {
t.Error("user found", foundUser)
}
}
func TestRemoveUser(t *testing.T) {
conn := createTestConn()
testUser := createTestUser()
panicOnErr(conn.AddUser(testUser))
err := conn.RemoveUser(testUser)
panicOnErr(err)
foundUser, found, err := conn.FindUser(testUser.Passkey)
panicOnErr(err)
if found {
t.Error("removed user found", foundUser)
}
}
func TestFindTorrentSuccess(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
if !found {
t.Error("torrent not found", testTorrent)
}
if !reflect.DeepEqual(foundTorrent, testTorrent) {
t.Error("found torrent mismatch", foundTorrent, testTorrent)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestFindTorrentFail(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
if found {
t.Error("torrent found", foundTorrent)
}
}
func TestRemoveTorrent(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
panicOnErr(conn.RemoveTorrent(testTorrent))
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
if found {
t.Error("removed torrent found", foundTorrent)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestClientWhitelistSuccess(t *testing.T) {
conn := createTestConn()
testPeerID := "-lt0D30-"
panicOnErr(conn.WhitelistClient(testPeerID))
found, err := conn.ClientWhitelisted(testPeerID)
panicOnErr(err)
if !found {
t.Error("peerID not found", testPeerID)
}
// Cleanup
panicOnErr(conn.UnWhitelistClient(testPeerID))
}
func TestClientWhitelistFail(t *testing.T) {
conn := createTestConn()
testPeerID2 := "TIX0192"
found, err := conn.ClientWhitelisted(testPeerID2)
panicOnErr(err)
if found {
t.Error("peerID found", testPeerID2)
}
}
func TestRecordSnatch(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
testUser := createTestUser()
panicOnErr(conn.AddTorrent(testTorrent))
panicOnErr(conn.AddUser(testUser))
userSnatches := testUser.Snatches
torrentSnatches := testTorrent.Snatches
panicOnErr(conn.RecordSnatch(testUser, testTorrent))
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundUser, _, err := conn.FindUser(testUser.Passkey)
panicOnErr(err)
if testUser.Snatches != userSnatches+1 {
t.Error("snatch not recorded to local user", testUser.Snatches, userSnatches+1)
}
if testTorrent.Snatches != torrentSnatches+1 {
t.Error("snatch not recorded to local torrent")
}
if foundUser.Snatches != userSnatches+1 {
t.Error("snatch not recorded to cached user", foundUser.Snatches, userSnatches+1)
}
if foundTorrent.Snatches != torrentSnatches+1 {
t.Error("snatch not recorded to cached torrent")
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
panicOnErr(conn.RemoveUser(testUser))
}
func TestMarkActive(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
testTorrent.Active = false
panicOnErr(conn.AddTorrent(testTorrent))
panicOnErr(conn.MarkActive(testTorrent))
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
if foundTorrent.Active != true {
t.Error("cached torrent not activated")
}
if testTorrent.Active != true {
t.Error("cached torrent not activated")
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestClientWhitelistRemove(t *testing.T) {
conn := createTestConn()
testPeerID := "-lt0D30-"
panicOnErr(conn.WhitelistClient(testPeerID))
panicOnErr(conn.UnWhitelistClient(testPeerID))
found, err := conn.ClientWhitelisted(testPeerID)
panicOnErr(err)
if found {
t.Error("removed peerID found", testPeerID)
}
}
func TestAddSeeder(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundSeeder, found := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
if found && foundSeeder != *testSeeder {
t.Error("seeder not added to cache", testSeeder)
}
foundSeeder, found = testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
if found && foundSeeder != *testSeeder {
t.Error("seeder not added to local", testSeeder)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestAddLeecher(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundLeecher, found := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if found && foundLeecher != *testLeecher {
t.Error("leecher not added to cache", testLeecher)
}
foundLeecher, found = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if found && foundLeecher != *testLeecher {
t.Error("leecher not added to local", testLeecher)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestRemoveSeeder(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
panicOnErr(conn.RemoveSeeder(testTorrent, testSeeder))
foundSeeder, found := testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
if found || foundSeeder == *testSeeder {
t.Error("seeder not removed from local", foundSeeder)
}
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundSeeder, found = foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
if found || foundSeeder == *testSeeder {
t.Error("seeder not removed from cache", foundSeeder, *testSeeder)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestRemoveLeecher(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
panicOnErr(conn.RemoveLeecher(testTorrent, testLeecher))
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundLeecher, found := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if found || foundLeecher == *testLeecher {
t.Error("leecher not removed from cache", foundLeecher, *testLeecher)
}
foundLeecher, found = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if found || foundLeecher == *testLeecher {
t.Error("leecher not removed from local", foundLeecher, *testLeecher)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestSetSeeder(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
r := rand.New(rand.NewSource(time.Now().UnixNano()))
testSeeder.Uploaded += uint64(r.Int63())
panicOnErr(conn.SetSeeder(testTorrent, testSeeder))
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundSeeder, _ := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
if foundSeeder != *testSeeder {
t.Error("seeder not updated in cache", foundSeeder, *testSeeder)
}
foundSeeder, _ = testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
if foundSeeder != *testSeeder {
t.Error("seeder not updated in local", foundSeeder, *testSeeder)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestSetLeecher(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
r := rand.New(rand.NewSource(time.Now().UnixNano()))
testLeecher.Uploaded += uint64(r.Int63())
panicOnErr(conn.SetLeecher(testTorrent, testLeecher))
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundLeecher, _ := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if foundLeecher != *testLeecher {
t.Error("leecher not updated in cache", testLeecher)
}
foundLeecher, _ = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if foundLeecher != *testLeecher {
t.Error("leecher not updated in local", testLeecher)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestLeecherFinished(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
testLeecher.Left = 0
panicOnErr(conn.LeecherFinished(testTorrent, testLeecher))
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundSeeder, _ := foundTorrent.Seeders[storage.PeerMapKey(testLeecher)]
if foundSeeder != *testLeecher {
t.Error("seeder not added to cache", foundSeeder, *testLeecher)
}
foundSeeder, _ = foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if foundSeeder == *testLeecher {
t.Error("leecher not removed from cache", testLeecher)
}
foundSeeder, _ = testTorrent.Seeders[storage.PeerMapKey(testLeecher)]
if foundSeeder != *testLeecher {
t.Error("seeder not added to local", testLeecher)
}
foundSeeder, _ = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if foundSeeder == *testLeecher {
t.Error("leecher not removed from local", testLeecher)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
// Add, update, verify remove
func TestUpdatePeer(t *testing.T) {
conn := createTestConn()
testTorrent := createTestTorrent()
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddTorrent(testTorrent))
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
// Update a seeder, set it, then check to make sure it updated
r := rand.New(rand.NewSource(time.Now().UnixNano()))
testSeeder.Uploaded += uint64(r.Int63())
panicOnErr(conn.SetSeeder(testTorrent, testSeeder))
panicOnErr(conn.RemoveSeeder(testTorrent, testSeeder))
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
if seeder, exists := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]; exists {
t.Error("seeder not removed from cache", seeder)
}
if seeder, exists := testTorrent.Seeders[storage.PeerMapKey(testSeeder)]; exists {
t.Error("seeder not removed from local", seeder)
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestParallelFindUser(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip()
}
conn := createTestConn()
testUserSuccess := createTestUser()
testUserFail := createTestUser()
panicOnErr(conn.AddUser(testUserSuccess))
for i := 0; i < 10; i++ {
foundUser, found, err := conn.FindUser(testUserFail.Passkey)
panicOnErr(err)
if found {
t.Error("user found", foundUser)
}
foundUser, found, err = conn.FindUser(testUserSuccess.Passkey)
panicOnErr(err)
if !found {
t.Error("user not found", testUserSuccess)
}
if *foundUser != *testUserSuccess {
t.Error("found user mismatch", *foundUser, testUserSuccess)
}
}
// Cleanup
panicOnErr(conn.RemoveUser(testUserSuccess))
}
func TestParallelFindTorrent(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip()
}
conn := createTestConn()
testTorrentSuccess := createTestTorrent()
testTorrentFail := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrentSuccess))
for i := 0; i < 10; i++ {
foundTorrent, found, err := conn.FindTorrent(testTorrentSuccess.Infohash)
panicOnErr(err)
if !found {
t.Error("torrent not found", testTorrentSuccess)
}
if !reflect.DeepEqual(foundTorrent, testTorrentSuccess) {
t.Error("found torrent mismatch", foundTorrent, testTorrentSuccess)
}
foundTorrent, found, err = conn.FindTorrent(testTorrentFail.Infohash)
panicOnErr(err)
if found {
t.Error("torrent found", foundTorrent)
}
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrentSuccess))
}
func TestParallelSetSeeder(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip()
}
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 10; i++ {
testSeeder.Uploaded += uint64(r.Int63())
panicOnErr(conn.SetSeeder(testTorrent, testSeeder))
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundSeeder, _ := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
if foundSeeder != *testSeeder {
t.Error("seeder not updated in cache", foundSeeder, *testSeeder)
}
foundSeeder, _ = testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
if foundSeeder != *testSeeder {
t.Error("seeder not updated in local", foundSeeder, *testSeeder)
}
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}
func TestParallelAddLeecher(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip()
}
conn := createTestConn()
testTorrent := createTestTorrent()
panicOnErr(conn.AddTorrent(testTorrent))
for i := 0; i < 10; i++ {
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
panicOnErr(err)
foundLeecher, found := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if found && foundLeecher != *testLeecher {
t.Error("leecher not added to cache", testLeecher)
}
foundLeecher, found = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
if found && foundLeecher != *testLeecher {
t.Error("leecher not added to local", testLeecher)
}
}
// Cleanup
panicOnErr(conn.RemoveTorrent(testTorrent))
}