Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2014-09-21 05:41:24 +0400
committerFeross Aboukhadijeh <feross@feross.org>2014-09-21 05:41:24 +0400
commit93686505fbc90522c75b6c151ec7261aa76098de (patch)
tree5b474b920e79b7b39b6804fac5e6a641de6ae843 /test/rarity-map.js
parent2e14192c311f64c20496a72af7ffce36495be92b (diff)
merge `bittorrent-client` into this module
When I started the WebTorrent project I thought there were going to need to be two separate client implementations (bittorrent-client and webtorrent-client) that would get tied together in a higher-level module. Fortunately, this was not necessary because of the awesome “browser” field support in browserify. By substituting just a few modules, we can make the same module (webtorrent) work in node AND the browser, with the same codebase! So, from now on, you can just `require(‘webtorrent’)` in node or the browser, and it will just work. You can also `npm install webtorrent` if you want to use bittorrent in a node app or script. Lastly, you can `npm install webtorrent -g` if you want to use webtorrent as a command line app (it installs a `webtorrent` command).
Diffstat (limited to 'test/rarity-map.js')
-rw-r--r--test/rarity-map.js113
1 files changed, 113 insertions, 0 deletions
diff --git a/test/rarity-map.js b/test/rarity-map.js
new file mode 100644
index 0000000..5e04ddc
--- /dev/null
+++ b/test/rarity-map.js
@@ -0,0 +1,113 @@
+var RarityMap = require('../lib/rarity-map')
+var BitField = require('bitfield')
+var Swarm = require('bittorrent-swarm')
+var EventEmitter = require('events').EventEmitter
+var test = require('tape')
+var hat = require('hat')
+
+var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36'
+var peerId1 = '-WW0001-' + hat(48)
+
+test('Rarity map usage', function (t) {
+ t.plan(16)
+
+ var swarm = new Swarm(infoHash, peerId1)
+ var numPieces = 4
+ swarm.wires = [ new EventEmitter(), new EventEmitter() ]
+ swarm.wires.forEach(function (wire) {
+ wire.peerPieces = new BitField(numPieces)
+ })
+ var rarityMap = new RarityMap(swarm, numPieces)
+
+ 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.
+ var 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)
+ }
+
+ // test initial / empty case
+ validateInitial()
+
+ rarityMap.recalculate()
+
+ // test initial / empty case after recalc
+ validateInitial()
+
+ function setPiece (wire, index) {
+ wire.peerPieces.set(index)
+ wire.emit('have', index)
+ }
+
+ setPiece(swarm.wires[0], 0)
+ setPiece(swarm.wires[1], 0)
+
+ setPiece(swarm.wires[0], 1)
+ setPiece(swarm.wires[1], 3)
+
+ // test rarest piece after setting pieces and handling 'have' events
+ var 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)
+
+ function addWire () {
+ var wire = new EventEmitter()
+ wire.peerPieces = new BitField(numPieces)
+ wire.peerPieces.set(1)
+ wire.peerPieces.set(2)
+ swarm.wires.push(wire)
+ swarm.emit('wire', wire)
+ }
+
+ 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)
+
+ function removeWire (index) {
+ var wire = swarm.wires.splice(index, 1)[0]
+ wire.emit('close')
+ }
+
+ 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(function (i) { return i <= 1 })
+ t.equal(piece, 0)
+
+ piece = rarityMap.getRarestPiece(function (i) { return i === 1 || i === 2 })
+ t.equal(piece, 2)
+})