Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: Many fixes; all leaks fixed #762

Merged
merged 12 commits into from Apr 22, 2016

fix duplicate client.add handling and re-enable test

  • Loading branch information
feross committed Apr 21, 2016
commit e311e0b7cb26437118e565674bffdfc0723492f7
@@ -208,6 +208,16 @@ WebTorrent.prototype.add = function (torrentId, opts, ontorrent) {
var torrent = new Torrent(torrentId, self, opts)
self.torrents.push(torrent)

torrent.once('infoHash', function () {
for (var i = 0, len = self.torrents.length; i < len; i++) {
var t = self.torrents[i]
if (t.infoHash === torrent.infoHash && t !== torrent) {
torrent.removeListener('ready', onReady)
torrent._destroy(new Error('Cannot add duplicate torrent ' + torrent.infoHash))
return
}
}
})
torrent.once('ready', onReady)

function onReady () {
@@ -1,104 +1,176 @@
// 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)
// })
// })
// })
// })
var fixtures = require('webtorrent-fixtures')
var test = require('tape')
var WebTorrent = require('../')

test('client.seed followed by duplicate client.add (sync)', 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)
})
})
})
})

test('client.seed followed by duplicate client.add (async)', 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(fixtures.leaves.torrentPath)

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)
})
})
})
})

test('client.seed followed by two duplicate client.add calls (sync)', 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)
})
})
})
})
})

test('client.seed followed by two duplicate client.add calls (async)', 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(fixtures.leaves.torrentPath)

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(fixtures.leaves.torrentPath)

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)
})
})
})
})
})

test('successive sync client.add, client.remove, client.add, client.remove (sync)', 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)
})
})
})
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.