diff options
author | Alex <alxmorais8@msn.com> | 2021-07-12 01:37:36 +0300 |
---|---|---|
committer | Alex <alxmorais8@msn.com> | 2021-07-12 01:37:36 +0300 |
commit | 600b9092aa5399e8fbd129c0b8c9e33145bb4ddb (patch) | |
tree | 0c9d3cc0f4cf15507d1b540ce57212aa357ae46d | |
parent | dd7863f024dbfec1cd9e7f3e1cfaaf8104aeaf2e (diff) |
Add docs + add tests
-rw-r--r-- | docs/api.md | 2 | ||||
-rw-r--r-- | lib/interval.js | 37 | ||||
-rw-r--r-- | test/client-deselect.js | 76 |
3 files changed, 65 insertions, 50 deletions
diff --git a/docs/api.md b/docs/api.md index db7441e..092a17a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -378,7 +378,7 @@ Selects a range of pieces to prioritize starting with `start` and ending with `e inclusive) at the given `priority`. `notify` is an optional callback to be called when the selection is updated with new data. -## `torrent.deselect(start, end, priority)` +## `torrent.deselect(start, end)` Deprioritizes a range of previously selected pieces. diff --git a/lib/interval.js b/lib/interval.js deleted file mode 100644 index d0cf69a..0000000 --- a/lib/interval.js +++ /dev/null @@ -1,37 +0,0 @@ -class Interval { - constructor (from, to, data) { - if (typeof from !== 'number' || typeof to !== 'number') { - throw new Error('from and to must be a number') - } - - this.from = from - this.to = to - this.data = data - } - - get from () { - return this.from - } - - get to () { - return this.to - } - - get priority () { - return this.data - } - - setFrom (from) { - if (typeof from !== 'number') throw new Error('from must be a number') - if (from > this.to) throw new Error('from must be lower than to') - this.from = from - } - - setTo (to) { - if (typeof to !== 'number') throw new Error('to must be a number') - if (this.from > to) throw new Error('from must be lower than to') - this.to = to - } -} - -module.exports = Interval diff --git a/test/client-deselect.js b/test/client-deselect.js index af5e290..9a33756 100644 --- a/test/client-deselect.js +++ b/test/client-deselect.js @@ -1,26 +1,78 @@ const fixtures = require('webtorrent-fixtures') +const MemoryChunkStore = require('memory-chunk-store') const test = require('tape') const WebTorrent = require('../') -test('client.seed: torrent file (Buffer)', function (t) { - t.plan(3) +function setupClient (t, onTorrent, onDone) { + const client1 = new WebTorrent({ dht: false, tracker: false, lsd: false }) + client1.on('error', function (err) { t.fail(err) }) + client1.on('warning', function (err) { t.fail(err) }) - const client = new WebTorrent({ dht: false, tracker: false, lsd: false }) + const client2 = new WebTorrent({ dht: false, tracker: false, lsd: false }) + client2.on('error', function (err) { t.fail(err) }) + client2.on('warning', function (err) { t.fail(err) }) - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) + const parsedTorrent = Object.assign({}, fixtures.leaves.parsedTorrent) - client.seed(fixtures.leaves.content, { + client1.seed(fixtures.leaves.content, { name: 'Leaves of Grass by Walt Whitman.epub', announce: [] - }, function (torrent) { - client.remove(torrent, function (err) { t.error(err, 'torrent removed') }) - t.equal(client.torrents.length, 0) + }, () => { + client2.add(parsedTorrent, { store: MemoryChunkStore }, (torrent) => { + onTorrent(torrent) + + torrent.addPeer(`localhost:${client1.torrentPort}`) + + torrent.once('done', () => { + onDone(torrent) + + client1.destroy(function (err) { t.error(err, 'client1 destroyed') }) + client2.destroy(function (err) { t.error(err, 'client2 destroyed') }) + }) + }) + }) +} + +test('client.select: whole torrent', function (t) { + t.plan(3) + + setupClient(t, (torrent) => { + torrent.select(0, torrent.pieces.length - 1) + }, (torrent) => { + t.equal(torrent.pieces.filter((a) => a === null).length, torrent.pieces.length) + }) +}) - // client.add() +test('client.select: partial torrent', function (t) { + t.plan(3) + + setupClient(t, (torrent) => { + lastPiece = Math.floor((torrent.pieces.length - 1) / 2) + torrent.deselect(0, torrent.pieces.length) + torrent.select(0, lastPiece) + }, (torrent) => { + t.equal(torrent.pieces.filter((a) => a === null).length, (lastPiece + 1)) + }) +}) + +test('client.deselect: whole torrent', function (t) { + t.plan(3) - // client.deselect() + setupClient(t, (torrent) => { + torrent.deselect(0, torrent.pieces.length - 1) + }, (torrent) => { + t.equal(torrent.pieces.filter((a) => a === null).length, 0) + }) +}) + +test('client.deselect: partial torrent', function (t) { + t.plan(3) - client.destroy(function (err) { t.error(err, 'client destroyed') }) + let lastPiece + setupClient(t, (torrent) => { + lastPiece = Math.floor((torrent.pieces.length - 1) / 2) + torrent.deselect(0, lastPiece) + }, (torrent) => { + t.equal(torrent.pieces.filter((a) => a === null).length, (torrent.pieces.length - 1 - lastPiece)) }) }) |