forked from webtorrent/webtorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrarity-map.js
128 lines (99 loc) · 3.1 KB
/
rarity-map.js
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
const fixtures = require('webtorrent-fixtures')
const randombytes = require('randombytes')
const test = require('tape')
const Wire = require('bittorrent-protocol')
const Torrent = require('../lib/torrent.js')
test('Rarity map usage', t => {
t.plan(16)
const numPieces = 4
const torrentId = Object.assign({}, fixtures.numbers.parsedTorrent, {
pieces: Array(numPieces)
})
const client = {
listening: true,
peerId: randombytes(20).toString('hex'),
torrentPort: 6889,
dht: false,
tracker: false,
lsd: false,
_remove () {}
}
const opts = {}
const torrent = new Torrent(torrentId, client, opts)
torrent.on('metadata', () => {
torrent._onWire(new Wire())
torrent._onWire(new Wire())
const rarityMap = torrent._rarityMap
// test initial / empty case
validateInitial()
rarityMap.recalculate()
// test initial / empty case after recalc
validateInitial()
setPiece(torrent.wires[0], 0)
setPiece(torrent.wires[1], 0)
setPiece(torrent.wires[0], 1)
setPiece(torrent.wires[1], 3)
// test rarest piece after setting pieces and handling 'have' events
let piece = rarityMap.getRarestPiece()
t.equal(piece, 2)
rarityMap.recalculate()
// test rarest piece after recalc to ensure its the same
piece = rarityMap.getRarestPiece()
t.equal(piece, 2)
addWire()
addWire()
// test rarest piece after adding wires
piece = rarityMap.getRarestPiece()
t.equal(piece, 3)
rarityMap.recalculate()
// test rarest piece after adding wires and recalc
piece = rarityMap.getRarestPiece()
t.equal(piece, 3)
removeWire(3)
removeWire(1)
// test rarest piece after removing wires
piece = rarityMap.getRarestPiece()
t.equal(piece, 3)
rarityMap.recalculate()
// test rarest piece after removing wires and recalc
piece = rarityMap.getRarestPiece()
t.equal(piece, 3)
// test piece filter func
piece = rarityMap.getRarestPiece(i => i <= 1)
t.equal(piece, 0)
piece = rarityMap.getRarestPiece(i => i === 1 || i === 2)
t.equal(piece, 2)
function validateInitial () {
// note that getRarestPiece will return a random piece since they're all equal
// so repeat the test several times to reasonably ensure its correctness.
let piece = rarityMap.getRarestPiece()
t.ok(piece >= 0 && piece < numPieces)
piece = rarityMap.getRarestPiece()
t.ok(piece >= 0 && piece < numPieces)
piece = rarityMap.getRarestPiece()
t.ok(piece >= 0 && piece < numPieces)
piece = rarityMap.getRarestPiece()
t.ok(piece >= 0 && piece < numPieces)
}
function setPiece (wire, index) {
wire.peerPieces.set(index)
wire.emit('have', index)
}
function addWire () {
const wire = new Wire()
wire.peerPieces.set(1)
wire.peerPieces.set(2)
torrent._onWire(wire)
}
function removeWire (index) {
const wire = torrent.wires.splice(index, 1)[0]
wire.destroy()
}
})
t.on('end', () => {
torrent.wires.forEach(wire => {
wire.destroy()
})
torrent.destroy()
})
})