diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2019-09-08 03:03:06 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-08 03:03:06 +0300 |
commit | 84fb338b9b4bc1dc6d376be34a21828da26e8739 (patch) | |
tree | 995e4555cd21c6f8a7a77ff509e60ae083bbd049 /test | |
parent | 69348363cfdb5d5a1f8e242643c3c686b38bd2b6 (diff) | |
parent | 9fa006813eeda248102f75ce0055939f33483f76 (diff) |
Add tests to check the order of torrent events (#1739)
Add tests to check the order of torrent events
Diffstat (limited to 'test')
-rw-r--r-- | test/torrent-events.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/test/torrent-events.js b/test/torrent-events.js new file mode 100644 index 0000000..febf1c4 --- /dev/null +++ b/test/torrent-events.js @@ -0,0 +1,83 @@ +const fixtures = require('webtorrent-fixtures') +const test = require('tape') +const WebTorrent = require('../') +const MemoryChunkStore = require('memory-chunk-store') + +test('client.add: emit torrent events in order', function (t) { + t.plan(6) + + var client1 = new WebTorrent({ dht: false, tracker: false }) + var client2 = new WebTorrent({ dht: false, tracker: false }) + + client1.on('error', function (err) { t.fail(err) }) + client1.on('warning', function (err) { t.fail(err) }) + + client2.on('error', function (err) { t.fail(err) }) + client2.on('warning', function (err) { t.fail(err) }) + + // Start seeding + client2.seed(fixtures.leaves.content, { + name: 'Leaves of Grass by Walt Whitman.epub', + announce: [] + }) + + client2.on('listening', function () { + // Start downloading + var torrent = client1.add(fixtures.leaves.parsedTorrent.infoHash, { store: MemoryChunkStore }) + + // Manually connect peers + torrent.addPeer('127.0.0.1:' + client2.address().port) + + var order = 0 + + torrent.on('infoHash', function () { + t.equal(++order, 1) + }) + + torrent.on('metadata', function () { + t.equal(++order, 2) + }) + + torrent.on('ready', function () { + t.equal(++order, 3) + }) + + torrent.on('done', function () { + t.equal(++order, 4) + + client1.destroy(function (err) { t.error(err, 'client 1 destroyed') }) + client2.destroy(function (err) { t.error(err, 'client 2 destroyed') }) + }) + }) +}) + +test('client.seed: emit torrent events in order', 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) }) + + var torrent = client.seed(fixtures.leaves.content) + + var order = 0 + + torrent.on('infoHash', function () { + t.equal(++order, 1) + }) + + torrent.on('metadata', function () { + t.equal(++order, 2) + }) + + torrent.on('ready', function () { + t.equal(++order, 3) + }) + + torrent.on('done', function () { + t.equal(++order, 4) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) |