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 | |
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')
-rw-r--r-- | test/duplicate.js | 104 | ||||
-rw-r--r-- | test/node/download-private-dht.js | 2 | ||||
-rw-r--r-- | test/node/download-webseed-magnet.js | 12 | ||||
-rw-r--r-- | test/node/duplicates.js | 80 | ||||
-rw-r--r-- | test/node/extensions.js | 2 | ||||
-rw-r--r-- | test/node/metadata.js | 4 | ||||
-rw-r--r-- | test/node/swarm-basic.js | 120 | ||||
-rw-r--r-- | test/node/swarm-reconnect.js | 124 | ||||
-rw-r--r-- | test/node/swarm-timeout.js | 100 | ||||
-rw-r--r-- | test/rarity-map.js | 185 | ||||
-rw-r--r-- | test/swarm.js | 134 |
11 files changed, 453 insertions, 414 deletions
diff --git a/test/duplicate.js b/test/duplicate.js new file mode 100644 index 0000000..ca06b3d --- /dev/null +++ b/test/duplicate.js @@ -0,0 +1,104 @@ +// var fixtures = require('webtorrent-fixtures') +// var test = require('tape') +// var WebTorrent = require('../') + +// test('client.seed followed by duplicate client.add', function (t) { +// t.plan(6) + +// var client = new WebTorrent({ dht: false, tracker: false }) +// client.on('error', function (err) { t.fail(err) }) +// client.on('warning', function (err) { t.fail(err) }) + +// client.seed(fixtures.leaves.content, { +// name: 'Leaves of Grass by Walt Whitman.epub', +// announce: [] +// }, function (torrent1) { +// t.equal(client.torrents.length, 1) + +// var torrent2 = client.add(torrent1.infoHash) + +// torrent2.once('ready', function () { +// t.fail('torrent ready is not called') +// }) + +// torrent2.once('error', function (err) { +// t.ok(err, 'got expected error on duplicate add') +// t.equal(client.torrents.length, 1) +// t.ok(torrent2.destroyed) +// client.destroy(function (err) { +// t.error(err, 'destroyed client') +// t.equal(client.torrents.length, 0) +// }) +// }) +// }) +// }) + +// TODO +// test('client.seed followed by two duplicate client.add calls', function (t) { +// t.plan(9) + +// var client = new WebTorrent({ dht: false, tracker: false }) +// client.on('error', function (err) { t.fail(err) }) +// client.on('warning', function (err) { t.fail(err) }) + +// client.seed(fixtures.leaves.content, { +// name: 'Leaves of Grass by Walt Whitman.epub', +// announce: [] +// }, function (torrent1) { +// t.equal(client.torrents.length, 1) + +// var torrent2 = client.add(torrent1.infoHash) + +// torrent2.once('ready', function () { +// t.fail('torrent ready is not called') +// }) + +// torrent2.once('error', function (err) { +// t.ok(err, 'got expected error on duplicate add') +// t.equal(client.torrents.length, 1) +// t.ok(torrent2.destroyed) + +// var torrent3 = client.add(torrent1.infoHash) + +// torrent3.once('ready', function () { +// t.fail('torrent ready is not called') +// }) + +// torrent3.once('error', function (err) { +// t.ok(err, 'got expected error on duplicate add') +// t.equal(client.torrents.length, 1) +// t.ok(torrent3.destroyed) +// client.destroy(function (err) { +// t.error(err, 'destroyed client') +// t.equal(client.torrents.length, 0) +// }) +// }) +// }) +// }) +// }) + +// TODO +// test('successive sync client.add, client.remove, client.add, client.remove', function (t) { +// t.plan(3) + +// var client = new WebTorrent({ dht: false, tracker: false }) +// client.on('error', function (err) { t.fail(err) }) +// client.on('warning', function (err) { t.fail(err) }) + +// client.seed(fixtures.leaves.content, { +// name: 'Leaves of Grass by Walt Whitman.epub', +// announce: [] +// }, function (torrent1) { +// t.equal(client.torrents.length, 1) + +// client.add(torrent1.infoHash) +// client.remove(torrent1.infoHash) +// client.add(torrent1.infoHash) +// client.remove(torrent1.infoHash, function () { +// client.destroy(function (err) { +// t.error(err, 'destroyed client') +// t.equal(client.torrents.length, 0) +// }) +// }) +// }) +// }) diff --git a/test/node/download-private-dht.js b/test/node/download-private-dht.js index 023f7ff..0f1a984 100644 --- a/test/node/download-private-dht.js +++ b/test/node/download-private-dht.js @@ -35,7 +35,7 @@ test('private torrent should not use DHT', function (t) { }) client.on('torrent', function () { - if (!torrent.discovery.dht && !torrent.swarm.handshakeOpts.dht) { + if (!torrent.discovery.dht) { t.pass('dht is disabled for this torrent') cb(null) } diff --git a/test/node/download-webseed-magnet.js b/test/node/download-webseed-magnet.js index 47a10f6..e0d39bf 100644 --- a/test/node/download-webseed-magnet.js +++ b/test/node/download-webseed-magnet.js @@ -51,12 +51,12 @@ test('Download using webseed (via magnet uri)', function (t) { maybeDone() }) - client1.on('listening', function () { + var torrent = client1.add(fixtures.leaves.parsedTorrent) + + torrent.on('infoHash', function () { gotListening = true maybeDone() }) - - client1.add(fixtures.leaves.parsedTorrent) }, function (cb) { @@ -91,11 +91,11 @@ test('Download using webseed (via magnet uri)', function (t) { } }) - client2.on('listening', function (port, torrent) { + var torrent = client2.add(magnetURI) + + torrent.on('infoHash', function () { torrent.addPeer('127.0.0.1:' + client1.address().port) }) - - client2.add(magnetURI) } ], function (err) { t.error(err) diff --git a/test/node/duplicates.js b/test/node/duplicates.js deleted file mode 100644 index 8689b85..0000000 --- a/test/node/duplicates.js +++ /dev/null @@ -1,80 +0,0 @@ -var fixtures = require('webtorrent-fixtures') -var test = require('tape') -var WebTorrent = require('../../') - -test('client.seed followed by duplicate client.add', function (t) { - t.plan(5) - - var client = new WebTorrent({ dht: false, tracker: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) - - client.seed(fixtures.leaves.content, { - name: 'Leaves of Grass by Walt Whitman.epub' - }, function (torrent1) { - t.equal(client.torrents.length, 1) - - client.add(torrent1.infoHash, function (torrent2) { - t.equal(torrent1.infoHash, torrent2.infoHash) - t.equal(client.torrents.length, 1) - - client.destroy(function (err) { - t.error(err, 'destroyed client') - t.equal(client.torrents.length, 0) - }) - }) - }) -}) - -test('client.seed followed by two duplicate client.add calls', function (t) { - t.plan(7) - - var client = new WebTorrent({ dht: false, tracker: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) - - client.seed(fixtures.leaves.content, { - name: 'Leaves of Grass by Walt Whitman.epub' - }, function (torrent1) { - t.equal(client.torrents.length, 1) - - client.add(torrent1.infoHash, function (torrent2) { - t.equal(torrent1.infoHash, torrent2.infoHash) - t.equal(client.torrents.length, 1) - - client.add(torrent1.infoHash, function (torrent2) { - t.equal(torrent1.infoHash, torrent2.infoHash) - t.equal(client.torrents.length, 1) - - client.destroy(function (err) { - t.error(err, 'destroyed client') - t.equal(client.torrents.length, 0) - }) - }) - }) - }) -}) - -test('successive sync client.add, client.remove, client.add, client.remove', function (t) { - t.plan(3) - - var client = new WebTorrent({ dht: false, tracker: false }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) - - client.seed(fixtures.leaves.content, { - name: 'Leaves of Grass by Walt Whitman.epub' - }, function (torrent1) { - t.equal(client.torrents.length, 1) - - client.add(torrent1.infoHash) - client.remove(torrent1.infoHash) - client.add(torrent1.infoHash) - client.remove(torrent1.infoHash, function () { - client.destroy(function (err) { - t.error(err, 'destroyed client') - t.equal(client.torrents.length, 0) - }) - }) - }) -}) diff --git a/test/node/extensions.js b/test/node/extensions.js index 5b68950..9dccd1d 100644 --- a/test/node/extensions.js +++ b/test/node/extensions.js @@ -49,7 +49,7 @@ test('extension support', function (t) { t.pass('client2 onWire') wire.use(Extension) }) - client2.on('listening', function () { + torrent2.on('infoHash', function () { torrent2.addPeer('127.0.0.1:' + client1.address().port) }) }) diff --git a/test/node/metadata.js b/test/node/metadata.js index 6a40f25..2b3fe56 100644 --- a/test/node/metadata.js +++ b/test/node/metadata.js @@ -26,9 +26,9 @@ test('ut_metadata transfer', function (t) { t.deepEqual(torrent1.info, fixtures.leaves.parsedTorrent.info) // client2 starts with infohash - client2.add(fixtures.leaves.parsedTorrent.infoHash) + var torrent2 = client2.add(fixtures.leaves.parsedTorrent.infoHash) - client2.on('listening', function (port, torrent2) { + torrent2.on('infoHash', function () { // manually add the peer torrent2.addPeer('127.0.0.1:' + client1.address().port) diff --git a/test/node/swarm-basic.js b/test/node/swarm-basic.js index 64fdee4..f81c3a4 100644 --- a/test/node/swarm-basic.js +++ b/test/node/swarm-basic.js @@ -1,60 +1,60 @@ -var hat = require('hat') -var Swarm = require('../../lib/swarm') -var test = require('tape') - -var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' -var infoHash2 = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa37' -var peerId = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') -var peerId2 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') - -test('two swarms listen on same port', function (t) { - t.plan(2) - - var swarm1 = new Swarm(infoHash, peerId) - swarm1.listen(0, function () { - var port = swarm1.address().port - t.ok(typeof port === 'number' && port !== 0) - - var swarm2 = new Swarm(infoHash2, peerId) - swarm2.listen(port, function () { - t.equal(swarm2.address().port, port, 'listened on requested port') - swarm1.destroy() - swarm2.destroy() - }) - }) -}) - -test('swarm join', function (t) { - t.plan(10) - - var swarm1 = new Swarm(infoHash, peerId) - swarm1.listen(0, function () { - var swarm2 = new Swarm(infoHash, peerId2) - - t.equal(swarm1.wires.length, 0) - t.equal(swarm2.wires.length, 0) - - swarm2.addPeer('127.0.0.1:' + swarm1.address().port) - - swarm1.on('wire', function (wire, addr) { - t.ok(wire, 'Peer join our swarm via listening port') - - t.equal(swarm1.wires.length, 1) - t.ok(/127\.0\.0\.1:\d{1,5}/.test(addr)) - t.equal(wire.peerId.toString('hex'), peerId2) - }) - - swarm2.on('wire', function (wire, addr) { - t.ok(wire, 'Joined swarm, got wire') - - t.equal(swarm2.wires.length, 1) - t.ok(/127\.0\.0\.1:\d{1,5}/.test(addr)) - t.equal(wire.peerId.toString('hex'), peerId) - }) - - t.on('end', function () { - swarm1.destroy() - swarm2.destroy() - }) - }) -}) +// var hat = require('hat') +// var Swarm = require('../../lib/swarm') +// var test = require('tape') + +// var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' +// var infoHash2 = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa37' +// var peerId = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') +// var peerId2 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') + +// test('two swarms listen on same port', function (t) { +// t.plan(2) + +// var swarm1 = new Swarm(infoHash, peerId) +// swarm1.listen(0, function () { +// var port = swarm1.address().port +// t.ok(typeof port === 'number' && port !== 0) + +// var swarm2 = new Swarm(infoHash2, peerId) +// swarm2.listen(port, function () { +// t.equal(swarm2.address().port, port, 'listened on requested port') +// swarm1.destroy() +// swarm2.destroy() +// }) +// }) +// }) + +// test('swarm join', function (t) { +// t.plan(10) + +// var swarm1 = new Swarm(infoHash, peerId) +// swarm1.listen(0, function () { +// var swarm2 = new Swarm(infoHash, peerId2) + +// t.equal(swarm1.wires.length, 0) +// t.equal(swarm2.wires.length, 0) + +// swarm2.addPeer('127.0.0.1:' + swarm1.address().port) + +// swarm1.on('wire', function (wire, addr) { +// t.ok(wire, 'Peer join our swarm via listening port') + +// t.equal(swarm1.wires.length, 1) +// t.ok(/127\.0\.0\.1:\d{1,5}/.test(addr)) +// t.equal(wire.peerId.toString('hex'), peerId2) +// }) + +// swarm2.on('wire', function (wire, addr) { +// t.ok(wire, 'Joined swarm, got wire') + +// t.equal(swarm2.wires.length, 1) +// t.ok(/127\.0\.0\.1:\d{1,5}/.test(addr)) +// t.equal(wire.peerId.toString('hex'), peerId) +// }) + +// t.on('end', function () { +// swarm1.destroy() +// swarm2.destroy() +// }) +// }) +// }) diff --git a/test/node/swarm-reconnect.js b/test/node/swarm-reconnect.js index 4fad7d0..abf3cc2 100644 --- a/test/node/swarm-reconnect.js +++ b/test/node/swarm-reconnect.js @@ -1,62 +1,62 @@ -var hat = require('hat') -var Swarm = require('../../lib/swarm') -var test = require('tape') - -var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' -var peerId1 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') -var peerId2 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') - -test('reconnect when peer disconnects', function (t) { - t.plan(10) - - var swarm1 = new Swarm(infoHash, peerId1) - swarm1.listen(0, function () { - var swarm2 = new Swarm(infoHash, peerId2) - - var time1 = 0 - swarm1.on('wire', function (wire) { - if (time1 === 0) { - t.ok(wire, 'Peer joined via listening port') - t.equal(swarm1.wires.length, 1) - - // at some point in future, end wire - setTimeout(function () { - wire.destroy() - }, 100) - - // ...and prevent reconnect - swarm1._drain = function () {} - } else if (time1 === 1) { - t.ok(wire, 'Remote peer reconnected') - t.equal(swarm1.wires.length, 1) - } else { - throw new Error('too many wire events (1)') - } - time1 += 1 - }) - - var time2 = 0 - swarm2.on('wire', function (wire) { - if (time2 === 0) { - t.ok(wire, 'Joined swarm, got wire') - t.equal(swarm2.wires.length, 1) - - wire.on('end', function () { - t.pass('Wire ended by remote peer') - t.equal(swarm1.wires.length, 0) - }) - } else if (time2 === 1) { - t.ok(wire, 'Reconnected to remote peer') - t.equal(swarm2.wires.length, 1) - - swarm1.destroy() - swarm2.destroy() - } else { - throw new Error('too many wire events (2)') - } - time2 += 1 - }) - - swarm2.addPeer('127.0.0.1:' + swarm1.address().port) - }) -}) +// var hat = require('hat') +// var Swarm = require('../../lib/swarm') +// var test = require('tape') + +// var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' +// var peerId1 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') +// var peerId2 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') + +// test('reconnect when peer disconnects', function (t) { +// t.plan(10) + +// var swarm1 = new Swarm(infoHash, peerId1) +// swarm1.listen(0, function () { +// var swarm2 = new Swarm(infoHash, peerId2) + +// var time1 = 0 +// swarm1.on('wire', function (wire) { +// if (time1 === 0) { +// t.ok(wire, 'Peer joined via listening port') +// t.equal(swarm1.wires.length, 1) + +// // at some point in future, end wire +// setTimeout(function () { +// wire.destroy() +// }, 100) + +// // ...and prevent reconnect +// swarm1._drain = function () {} +// } else if (time1 === 1) { +// t.ok(wire, 'Remote peer reconnected') +// t.equal(swarm1.wires.length, 1) +// } else { +// throw new Error('too many wire events (1)') +// } +// time1 += 1 +// }) + +// var time2 = 0 +// swarm2.on('wire', function (wire) { +// if (time2 === 0) { +// t.ok(wire, 'Joined swarm, got wire') +// t.equal(swarm2.wires.length, 1) + +// wire.on('end', function () { +// t.pass('Wire ended by remote peer') +// t.equal(swarm1.wires.length, 0) +// }) +// } else if (time2 === 1) { +// t.ok(wire, 'Reconnected to remote peer') +// t.equal(swarm2.wires.length, 1) + +// swarm1.destroy() +// swarm2.destroy() +// } else { +// throw new Error('too many wire events (2)') +// } +// time2 += 1 +// }) + +// swarm2.addPeer('127.0.0.1:' + swarm1.address().port) +// }) +// }) diff --git a/test/node/swarm-timeout.js b/test/node/swarm-timeout.js index 4341465..50c9822 100644 --- a/test/node/swarm-timeout.js +++ b/test/node/swarm-timeout.js @@ -1,50 +1,50 @@ -var hat = require('hat') -var Swarm = require('../../lib/swarm') -var test = require('tape') - -var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' -var peerId1 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') -var peerId2 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') - -test('timeout if no handshake in 25 seconds', function (t) { - t.plan(4) - - var swarm1 = new Swarm(infoHash, peerId1) - - var _addIncomingPeer = swarm1._addIncomingPeer - swarm1._addIncomingPeer = function (peer) { - // Nuke the handshake function on swarm1's peer to test swarm2's - // handshake timeout code - peer.wire.handshake = function () {} - _addIncomingPeer.call(swarm1, peer) - } - - swarm1.listen(0, function () { - var swarm2 = new Swarm(infoHash, peerId2) - - var numWires = 0 - swarm1.on('wire', function (wire) { - numWires += 1 - if (numWires === 1) { - t.ok(wire, 'Got wire via listening port') - t.equal(swarm1.wires.length, 1) - - // swarm2 should never get a wire since swarm1 refuses to send it a - // handshake - t.equal(swarm2.wires.length, 0) - } else if (numWires === 2) { - t.pass('swarm2 reconnected after timeout') - swarm1.destroy() - swarm2.destroy() - } else { - t.fail('got wire after destroy') - } - }) - - swarm2.on('wire', function (wire) { - t.fail('Should not get a wire because peer did not handshake') - }) - - swarm2.addPeer('127.0.0.1:' + swarm1.address().port) - }) -}) +// var hat = require('hat') +// var Swarm = require('../../lib/swarm') +// var test = require('tape') + +// var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' +// var peerId1 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') +// var peerId2 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') + +// test('timeout if no handshake in 25 seconds', function (t) { +// t.plan(4) + +// var swarm1 = new Swarm(infoHash, peerId1) + +// var _addIncomingPeer = swarm1._addIncomingPeer +// swarm1._addIncomingPeer = function (peer) { +// // Nuke the handshake function on swarm1's peer to test swarm2's +// // handshake timeout code +// peer.wire.handshake = function () {} +// _addIncomingPeer.call(swarm1, peer) +// } + +// swarm1.listen(0, function () { +// var swarm2 = new Swarm(infoHash, peerId2) + +// var numWires = 0 +// swarm1.on('wire', function (wire) { +// numWires += 1 +// if (numWires === 1) { +// t.ok(wire, 'Got wire via listening port') +// t.equal(swarm1.wires.length, 1) + +// // swarm2 should never get a wire since swarm1 refuses to send it a +// // handshake +// t.equal(swarm2.wires.length, 0) +// } else if (numWires === 2) { +// t.pass('swarm2 reconnected after timeout') +// swarm1.destroy() +// swarm2.destroy() +// } else { +// t.fail('got wire after destroy') +// } +// }) + +// swarm2.on('wire', function (wire) { +// t.fail('Should not get a wire because peer did not handshake') +// }) + +// swarm2.addPeer('127.0.0.1:' + swarm1.address().port) +// }) +// }) 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() + }) }) diff --git a/test/swarm.js b/test/swarm.js index 9662d4d..023455b 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -1,82 +1,82 @@ -var hat = require('hat') -var Swarm = require('../lib/swarm') -var test = require('tape') +// var hat = require('hat') +// var Swarm = require('../lib/swarm') +// var test = require('tape') -var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' -var infoHash2 = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa37' -var peerId = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') -var peerId2 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') +// var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' +// var infoHash2 = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa37' +// var peerId = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') +// var peerId2 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex') -test('create swarm, check invariants', function (t) { - var swarm = new Swarm(infoHash, peerId) +// test('create swarm, check invariants', function (t) { +// var swarm = new Swarm(infoHash, peerId) - t.equal(swarm.infoHash.toString('hex'), infoHash) - t.equal(swarm.peerId.toString('hex'), peerId) - t.equal(swarm.downloaded, 0) - t.equal(swarm.uploaded, 0) - t.ok(Array.isArray(swarm.wires)) - t.equal(swarm.wires.length, 0) - t.end() -}) +// t.equal(swarm.infoHash.toString('hex'), infoHash) +// t.equal(swarm.peerId.toString('hex'), peerId) +// t.equal(swarm.downloaded, 0) +// t.equal(swarm.uploaded, 0) +// t.ok(Array.isArray(swarm.wires)) +// t.equal(swarm.wires.length, 0) +// t.end() +// }) -test('swarm listen(0) selects free port', function (t) { - t.plan(2) +// test('swarm listen(0) selects free port', function (t) { +// t.plan(2) - var swarm = new Swarm(infoHash, peerId) - swarm.listen(0) - swarm.on('listening', function () { - var port = swarm.address().port - t.equal(typeof port, 'number', 'port is a number') - if (process.browser) { - t.equal(port, 0, 'port number is 0') - } else { - t.ok(port > 0 && port < 65535, 'valid port number') - } - swarm.destroy() - }) -}) +// var swarm = new Swarm(infoHash, peerId) +// swarm.listen(0) +// swarm.on('listening', function () { +// var port = swarm.address().port +// t.equal(typeof port, 'number', 'port is a number') +// if (process.browser) { +// t.equal(port, 0, 'port number is 0') +// } else { +// t.ok(port > 0 && port < 65535, 'valid port number') +// } +// swarm.destroy() +// }) +// }) -test('two swarms listen on same port (implicit)', function (t) { - t.plan(5) +// test('two swarms listen on same port (implicit)', function (t) { +// t.plan(5) - // When no port is specified and listen is called twice, they should get assigned the same port. +// // When no port is specified and listen is called twice, they should get assigned the same port. - var swarm1 = new Swarm(infoHash, peerId) - var swarm2 = new Swarm(infoHash2, peerId2) +// var swarm1 = new Swarm(infoHash, peerId) +// var swarm2 = new Swarm(infoHash2, peerId2) - var swarm1Port - var swarm2Port +// var swarm1Port +// var swarm2Port - function maybeDone () { - if (swarm1.listening && swarm2.listening) { - t.equal(swarm1Port, swarm2Port, 'swarms were given same port') +// function maybeDone () { +// if (swarm1.listening && swarm2.listening) { +// t.equal(swarm1Port, swarm2Port, 'swarms were given same port') - t.equal(typeof swarm1Port, 'number', 'port is a number') - if (process.browser) { - t.equal(swarm1Port, 0, 'port number is 0') - } else { - t.ok(swarm1Port > 0 && swarm1Port < 65535, 'valid port number') - } +// t.equal(typeof swarm1Port, 'number', 'port is a number') +// if (process.browser) { +// t.equal(swarm1Port, 0, 'port number is 0') +// } else { +// t.ok(swarm1Port > 0 && swarm1Port < 65535, 'valid port number') +// } - t.equal(typeof swarm2Port, 'number', 'port is a number') - if (process.browser) { - t.equal(swarm2Port, 0, 'port number is 0') - } else { - t.ok(swarm2Port > 0 && swarm2Port < 65535, 'valid port number') - } +// t.equal(typeof swarm2Port, 'number', 'port is a number') +// if (process.browser) { +// t.equal(swarm2Port, 0, 'port number is 0') +// } else { +// t.ok(swarm2Port > 0 && swarm2Port < 65535, 'valid port number') +// } - swarm1.destroy() - swarm2.destroy() - } - } +// swarm1.destroy() +// swarm2.destroy() +// } +// } - swarm1.listen(0, function () { - swarm1Port = swarm1.address().port - maybeDone() - }) +// swarm1.listen(0, function () { +// swarm1Port = swarm1.address().port +// maybeDone() +// }) - swarm2.listen(0, function (port2) { - swarm2Port = swarm2.address().port - maybeDone() - }) -}) +// swarm2.listen(0, function (port2) { +// swarm2Port = swarm2.address().port +// maybeDone() +// }) +// }) |