diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2016-04-21 09:10:32 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2016-04-21 09:10:32 +0300 |
commit | 3daee2c66cbf752b9e6e49b99492b8c1914a4a58 (patch) | |
tree | fbdd6c10f6a64674268dc77d1b3ccb4a37c44a65 /test/rarity-map.js | |
parent | 7a7c4a8b8c49f5c92b7c20ff439bc8614f7d607e (diff) |
BREAKING: Major cleanup
### Added
- `client.listening` property to signal whether TCP server is listening
for incoming
connections.
### Changed
- Merged `Swarm` class into `Torrent` object. Properties on
`torrent.swarm` (like
`torrent.swarm.wires`) now exist on `torrent` (e.g. `torrent.wires`).
- `torrent.addPeer` can no longer be called before the `infoHash` event
has been
emitted.
- Remove `torrent.on('listening')` event. Use `client.on('listening')`
instead.
- Remove support from `TCPPool` for listening on multiple ports. This
was not used by
WebTorrent and just added complexity. There is now a single `TCPPool`
instance for the
whole WebTorrent client.
- Deprecate: Do not use `client.download()` anymore. Use `client.add()`
instead.
- Deprecate: Do not use `torrent.swarm` anymore. Use `torrent` instead.
### Fixed
- When there is a `torrent.on('error')` listener, don't also emit
`client.on('error')`.
- Do not return existing torrent object when duplicate torrent is
added. Fire an
`'error'` event instead.
- Memory leak of `Torrent` object caused by `RarityMap`
- Memory leak of `Torrent` object caused by `TCPPool`
- `client.ratio` and `torrent.ratio` are now calculated as `uploaded /
received` instead
of `uploaded / downloaded`.
Diffstat (limited to 'test/rarity-map.js')
-rw-r--r-- | test/rarity-map.js | 185 |
1 files changed, 100 insertions, 85 deletions
diff --git a/test/rarity-map.js b/test/rarity-map.js index c516464..7a630ad 100644 --- a/test/rarity-map.js +++ b/test/rarity-map.js @@ -1,113 +1,128 @@ -var BitField = require('bitfield') -var EventEmitter = require('events').EventEmitter +var extend = require('xtend') +var fixtures = require('webtorrent-fixtures') var hat = require('hat') -var RarityMap = require('../lib/rarity-map') -var Swarm = require('../lib/swarm') var test = require('tape') - -var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' -var peerId1 = new Buffer('-WW0001-' + hat(48)) +var Torrent = require('../lib/torrent') +var Wire = require('bittorrent-protocol') 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 torrentId = extend(fixtures.numbers.parsedTorrent, { + pieces: Array(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) + var client = { + listening: true, + peerId: hat(160), + torrentPort: 6889, + dht: false, + tracker: false, + remove: function () {} } + var opts = {} + var torrent = new Torrent(torrentId, client, opts) + torrent.on('metadata', function () { + torrent._onWire(new Wire()) + torrent._onWire(new Wire()) - // test initial / empty case - validateInitial() + var rarityMap = torrent._rarityMap - rarityMap.recalculate() - - // test initial / empty case after recalc - validateInitial() - - function setPiece (wire, index) { - wire.peerPieces.set(index) - wire.emit('have', index) - } + // test initial / empty case + validateInitial() - setPiece(swarm.wires[0], 0) - setPiece(swarm.wires[1], 0) + rarityMap.recalculate() - setPiece(swarm.wires[0], 1) - setPiece(swarm.wires[1], 3) + // test initial / empty case after recalc + validateInitial() - // test rarest piece after setting pieces and handling 'have' events - var piece = rarityMap.getRarestPiece() - t.equal(piece, 2) + setPiece(torrent.wires[0], 0) + setPiece(torrent.wires[1], 0) - rarityMap.recalculate() + setPiece(torrent.wires[0], 1) + setPiece(torrent.wires[1], 3) - // 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) - } + // test rarest piece after setting pieces and handling 'have' events + var piece = rarityMap.getRarestPiece() + t.equal(piece, 2) - addWire() - addWire() + rarityMap.recalculate() - // test rarest piece after adding wires - piece = rarityMap.getRarestPiece() - t.equal(piece, 3) + // test rarest piece after recalc to ensure its the same + piece = rarityMap.getRarestPiece() + t.equal(piece, 2) - rarityMap.recalculate() + addWire() + addWire() - // test rarest piece after adding wires and recalc - piece = rarityMap.getRarestPiece() - t.equal(piece, 3) + // test rarest piece after adding wires + piece = rarityMap.getRarestPiece() + t.equal(piece, 3) - function removeWire (index) { - var wire = swarm.wires.splice(index, 1)[0] - wire.emit('close') - } + rarityMap.recalculate() - removeWire(3) - removeWire(1) + // test rarest piece after adding wires and recalc + piece = rarityMap.getRarestPiece() + t.equal(piece, 3) - // test rarest piece after removing wires - piece = rarityMap.getRarestPiece() - t.equal(piece, 3) + removeWire(3) + removeWire(1) - rarityMap.recalculate() + // test rarest piece after removing wires + piece = rarityMap.getRarestPiece() + t.equal(piece, 3) - // test rarest piece after removing wires and recalc - piece = rarityMap.getRarestPiece() - t.equal(piece, 3) + rarityMap.recalculate() - // test piece filter func - piece = rarityMap.getRarestPiece(function (i) { return i <= 1 }) - t.equal(piece, 0) + // 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) + + 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) + } + + function setPiece (wire, index) { + wire.peerPieces.set(index) + wire.emit('have', index) + } + + function addWire () { + var wire = new Wire() + wire.peerPieces.set(1) + wire.peerPieces.set(2) + torrent._onWire(wire) + } + + function removeWire (index) { + var wire = torrent.wires.splice(index, 1)[0] + wire.destroy() + } + }) - piece = rarityMap.getRarestPiece(function (i) { return i === 1 || i === 2 }) - t.equal(piece, 2) + t.on('end', function () { + torrent.wires.forEach(function (wire) { + wire.destroy() + }) + torrent.destroy() + }) }) |