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

Test improvements: use fixtures #540

Merged
merged 8 commits into from Dec 27, 2015
@@ -617,13 +617,15 @@ function gracefulExit () {

clivas.line('\n{green:webtorrent is gracefully exiting...}')

if (client) {
if (argv['on-exit']) cp.exec(argv['on-exit']).unref()
client.destroy(function (err) {
if (err) return fatalError(err)
// Quit after 1 second. This shouldn't be necessary, node never quits even though
// there's nothing in the event loop when `wrtc` (webtorrent-hybrid) is used :(
setTimeout(function () { process.exit(0) }, 1000).unref()
})
}
if (!client) return

if (argv['on-exit']) cp.exec(argv['on-exit']).unref()

client.destroy(function (err) {
if (err) return fatalError(err)

// Quit after 1 second. This is only necessary for `webtorrent-hybrid` since
// the `wrtc` package makes node never quit :(
setTimeout(function () { process.exit(0) }, 1000).unref()
})
}
@@ -12,6 +12,7 @@ function Server (torrent, opts) {
var server = http.createServer(opts)

var sockets = []
var closed = false

server.on('connection', function (socket) {
socket.setTimeout(36000000)
@@ -22,11 +23,20 @@ function Server (torrent, opts) {
})
})

var _close = server.close
server.close = function (cb) {
closed = true
_close.call(server, cb)
}

server.destroy = function (cb) {
sockets.forEach(function (socket) {
socket.destroy()
})
server.close(cb)

// Only call `server.close` if user has not called it already
if (closed) process.nextTick(cb)
else server.close(cb)
}

server.on('request', function (req, res) {
@@ -73,7 +73,7 @@
"brfs": "^1.2.0",
"browserify": "^12.0.1",
"finalhandler": "^0.4.0",
"run-auto": "^1.0.0",
"run-series": "^1.0.2",
"serve-static": "^1.9.3",
"simple-get": "^1.0.0",
"standard": "^5.1.0",
@@ -83,15 +83,15 @@
},
"homepage": "http://webtorrent.io",
"keywords": [
"torrent",
"bittorrent",
"bittorrent client",
"streaming",
"download",
"mad science",
"streaming",
"torrent",
"webrtc",
"webrtc data",
"webtorrent",
"mad science"
"webtorrent"
],
"license": "MIT",
"main": "index.js",
@@ -116,7 +116,7 @@ test('client.add: parsed torrent, from `parse-torrent`', function (t) {
})

test('client.add: parsed torrent, with string type announce property', function (t) {
t.plan(6)
t.plan(7)

var client = new WebTorrent({ dht: false, tracker: false })

@@ -131,7 +131,44 @@ test('client.add: parsed torrent, with string type announce property', function

torrent.on('infoHash', function () {
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.leaves.magnetURI + '&tr=' + encodeURIComponent('http://tracker.local:80'))

var expectedMagnetURI = common.leaves.magnetURI +
'&tr=' + encodeURIComponent('http://tracker.local:80')
t.equal(torrent.magnetURI, expectedMagnetURI)

// `torrent.announce` must always be an array
t.deepEqual(torrent.announce, [ 'http://tracker.local:80' ])

client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') })
t.equal(client.torrents.length, 0)

client.destroy(function (err) { t.error(err, 'client destroyed') })
})
})

test('client.add: parsed torrent, with array type announce property', 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) })

var parsedTorrent = extend(common.leaves.parsedTorrent)
parsedTorrent.announce = [ 'http://tracker.local:80', 'http://tracker.local:81' ]

var torrent = client.add(parsedTorrent)
t.equal(client.torrents.length, 1)

torrent.on('infoHash', function () {
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)

var expectedMagnetURI = common.leaves.magnetURI +
'&tr=' + encodeURIComponent('http://tracker.local:80') +
'&tr=' + encodeURIComponent('http://tracker.local:81')
t.equal(torrent.magnetURI, expectedMagnetURI)

t.deepEqual(torrent.announce, [ 'http://tracker.local:80', 'http://tracker.local:81' ])

client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') })
t.equal(client.torrents.length, 0)
@@ -1,30 +1,27 @@
var auto = require('run-auto')
var common = require('./common')
var DHT = require('bittorrent-dht/server')
var networkAddress = require('network-address')
var series = require('run-series')
var test = require('tape')
var WebTorrent = require('../')

test('blocklist blocks peers discovered via DHT', function (t) {
t.plan(8)
t.plan(9)

var dhtServer = new DHT({ bootstrap: false })
var dhtServer, client1, client2

dhtServer.on('error', function (err) { t.fail(err) })
dhtServer.on('warning', function (err) { t.fail(err) })

auto({
dhtPort: function (cb) {
dhtServer.listen(function () {
var port = dhtServer.address().port
cb(null, port)
})
series([
function (cb) {
dhtServer = new DHT({ bootstrap: false })
dhtServer.on('error', function (err) { t.fail(err) })
dhtServer.on('warning', function (err) { t.fail(err) })
dhtServer.listen(cb)
},

client1: ['dhtPort', function (cb, r) {
var client1 = new WebTorrent({
function (cb) {
client1 = new WebTorrent({
tracker: false,
dht: { bootstrap: '127.0.0.1:' + r.dhtPort }
dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port }
})
client1.on('error', function (err) { t.fail(err) })
client1.on('warning', function (err) { t.fail(err) })
@@ -54,14 +51,14 @@ test('blocklist blocks peers discovered via DHT', function (t) {
var torrentReady = false
var announced = false
function maybeDone () {
if (torrentReady && announced) cb(null, client1)
if (torrentReady && announced) cb(null)
}
}],
},

client2: ['client1', function (cb, r) {
var client2 = new WebTorrent({
function (cb) {
client2 = new WebTorrent({
tracker: false,
dht: { bootstrap: '127.0.0.1:' + r.dhtPort },
dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port },
blocklist: [ '127.0.0.1', networkAddress.ipv4() ]
})
client2.on('error', function (err) { t.fail(err) })
@@ -75,24 +72,23 @@ test('blocklist blocks peers discovered via DHT', function (t) {

torrent2.on('dhtAnnounce', function () {
t.pass('client2 announced to dht')
cb(null, client2)
cb(null)
})

torrent2.on('peer', function (addr) {
t.fail('client2 should not find any peers')
})
}]

}, function (err, r) {
if (err) throw err
}

], function (err) {
t.error(err)
dhtServer.destroy(function (err) {
t.error(err, 'dht server destroyed')
})
r.client1.destroy(function (err) {
client1.destroy(function (err) {
t.error(err, 'client1 destroyed')
})
r.client2.destroy(function (err) {
client2.destroy(function (err) {
t.error(err, 'client2 destroyed')
})
})
@@ -1,84 +1,90 @@
var auto = require('run-auto')
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var path = require('path')
var common = require('./common')
var extend = require('xtend')
var series = require('run-series')
var test = require('tape')
var TrackerServer = require('bittorrent-tracker/server')
var WebTorrent = require('../')

var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent'))
var leavesParsed = parseTorrent(leavesTorrent)

test('blocklist blocks peers discovered via tracker', function (t) {
t.plan(8)
t.plan(9)

var parsedTorrent = extend(common.leaves.parsedTorrent)
var tracker, client1, client2

auto({
tracker: function (cb) {
var tracker = new TrackerServer({ udp: false, ws: false })
series([
function (cb) {
tracker = new TrackerServer({ udp: false, ws: false })

tracker.listen(function () {
var port = tracker.http.address().port
var announceUrl = 'http://127.0.0.1:' + port + '/announce'

// Overwrite announce with our local tracker
leavesParsed.announce = [ announceUrl ]
parsedTorrent.announce = announceUrl

cb(null, tracker)
cb(null)
})

tracker.on('start', function () {
t.pass('client connected to tracker') // 2x, once for each client
tracker.once('start', function () {
t.pass('client1 connected to tracker')

tracker.once('start', function () {
t.pass('client2 connected to tracker')
})
})
},

client1: ['tracker', function (cb) {
var client1 = new WebTorrent({ dht: false })
function (cb) {
client1 = new WebTorrent({ dht: false })
client1.on('error', function (err) { t.fail(err) })
client1.on('warning', function (err) { t.fail(err) })

var torrent1 = client1.add(leavesParsed)
var torrent1 = client1.add(parsedTorrent)

torrent1.on('peer', function () {
t.pass('client1 found itself')
cb(null, client1)
cb(null)
})

torrent1.on('blockedPeer', function () {
t.fail('client1 should not block any peers')
})
}],
},

client2: ['client1', function (cb) {
var client2 = new WebTorrent({
function (cb) {
client2 = new WebTorrent({
dht: false,
blocklist: [ '127.0.0.1' ]
})
client2.on('error', function (err) { t.fail(err) })
client2.on('warning', function (err) { t.fail(err) })

var torrent2 = client2.add(leavesParsed)
var torrent2 = client2.add(parsedTorrent)

torrent2.once('blockedPeer', function () {
t.pass('client2 blocked first peer')

torrent2.on('blockedPeer', function () {
t.pass('client2 blocked connection') // 2x, once for each client
cb(null, client2)
torrent2.once('blockedPeer', function () {
t.pass('client2 blocked second peer')
cb(null)
})
})

torrent2.on('peer', function () {
t.fail('client2 should not find any peers')
})
}]

}, function (err, r) {
if (err) throw err
}

r.tracker.close(function () {
], function (err) {
t.error(err)
tracker.close(function () {
t.pass('tracker closed')
})
r.client1.destroy(function () {
t.pass('client1 destroyed')
client1.destroy(function (err) {
t.error(err, 'client1 destroyed')
})
r.client2.destroy(function () {
t.pass('client2 destroyed')
client2.destroy(function (err) {
t.error(err, 'client2 destroyed')
})
})
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.