test: add large torrent tests for ws tracker

This commit is contained in:
Feross Aboukhadijeh 2016-02-29 17:11:02 -08:00
parent e15125ca80
commit 012b31553b
3 changed files with 47 additions and 58 deletions

View File

@ -1,49 +1,29 @@
var common = require('./common')
var Client = require('../')
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var path = require('path')
var Server = require('../').Server
var test = require('tape')
var torrent = fs.readFileSync(path.join(__dirname, 'torrents/sintel-5gb.torrent'))
var parsedTorrent = parseTorrent(torrent)
var peerId = new Buffer('01234567890123456789')
test('large torrent: client.start()', function (t) {
t.plan(5)
function testLargeTorrent (t, serverType) {
t.plan(4)
var server = new Server({ http: false, ws: false })
common.createServer(t, serverType, function (server, announceUrl) {
parsedTorrent.announce = [ announceUrl ]
var client = new Client(peerId, 6881, parsedTorrent, { wrtc: {} })
server.on('error', function (err) {
t.fail(err.message)
})
server.on('warning', function (err) {
t.fail(err.message)
})
server.listen(0, function () {
var port = server.udp.address().port
// remove all tracker servers except a single UDP one, for now
parsedTorrent.announce = [ 'udp://127.0.0.1:' + port ]
var client = new Client(peerId, 6881, parsedTorrent)
client.on('error', function (err) {
t.error(err)
})
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
client.once('update', function (data) {
t.equal(data.announce, 'udp://127.0.0.1:' + port)
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
})
client.start()
client.once('peer', function () {
t.pass('there is at least one peer')
client.stop()
@ -53,5 +33,19 @@ test('large torrent: client.start()', function (t) {
client.destroy()
})
})
client.start()
})
}
test('http: large torrent: client.start()', function (t) {
testLargeTorrent(t, 'http')
})
test('udp: large torrent: client.start()', function (t) {
testLargeTorrent(t, 'udp')
})
test('ws: large torrent: client.start()', function (t) {
testLargeTorrent(t, 'ws')
})

View File

@ -12,25 +12,13 @@ var peerId2 = new Buffer('12345678901234567890')
var peerId3 = new Buffer('23456789012345678901')
var port = 6881
function mockWebSocketTracker (client) {
client._trackers[0]._generateOffers = function (numwant, cb) {
var offers = []
for (var i = 0; i < numwant; i++) {
offers.push({ fake_offer: 'fake_offer_' + i })
}
process.nextTick(function () {
cb(offers)
})
}
}
function testClientStart (t, serverType) {
t.plan(4)
common.createServer(t, serverType, function (server, announceUrl) {
parsedTorrent.announce = [ announceUrl ]
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
if (serverType === 'ws') mockWebSocketTracker(client)
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
@ -70,7 +58,7 @@ function testClientStop (t, serverType) {
parsedTorrent.announce = [ announceUrl ]
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
if (serverType === 'ws') mockWebSocketTracker(client)
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
@ -110,7 +98,7 @@ function testClientUpdate (t, serverType) {
parsedTorrent.announce = [ announceUrl ]
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
if (serverType === 'ws') mockWebSocketTracker(client)
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
@ -157,7 +145,7 @@ function testClientScrape (t, serverType) {
parsedTorrent.announce = [ announceUrl ]
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
if (serverType === 'ws') mockWebSocketTracker(client)
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
@ -198,7 +186,7 @@ function testClientAnnounceWithParams (t, serverType) {
t.equal(params.testParam, 'this is a test')
})
if (serverType === 'ws') mockWebSocketTracker(client)
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
@ -248,7 +236,7 @@ function testClientGetAnnounceOpts (t, serverType) {
t.equal(params.testParam, 'this is a test')
})
if (serverType === 'ws') mockWebSocketTracker(client)
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
@ -284,7 +272,7 @@ function testClientAnnounceWithNumWant (t, serverType) {
parsedTorrent.announce = [ announceUrl ]
var client1 = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
if (serverType === 'ws') mockWebSocketTracker(client1)
if (serverType === 'ws') common.mockWebsocketTracker(client1)
client1.on('error', function (err) { t.error(err) })
client1.on('warning', function (err) { t.error(err) })
@ -292,7 +280,7 @@ function testClientAnnounceWithNumWant (t, serverType) {
client1.once('update', function () {
var client2 = new Client(peerId2, port + 1, parsedTorrent, { wrtc: {} })
if (serverType === 'ws') mockWebSocketTracker(client2)
if (serverType === 'ws') common.mockWebsocketTracker(client2)
client2.on('error', function (err) { t.error(err) })
client2.on('warning', function (err) { t.error(err) })
@ -300,7 +288,7 @@ function testClientAnnounceWithNumWant (t, serverType) {
client2.once('update', function () {
var client3 = new Client(peerId3, port + 2, parsedTorrent, { wrtc: {} })
if (serverType === 'ws') mockWebSocketTracker(client3)
if (serverType === 'ws') common.mockWebsocketTracker(client3)
client3.on('error', function (err) { t.error(err) })
client3.on('warning', function (err) { t.error(err) })

View File

@ -7,13 +7,8 @@ exports.createServer = function (t, serverType, cb) {
ws: serverType === 'ws'
})
server.on('error', function (err) {
t.error(err)
})
server.on('warning', function (err) {
t.error(err)
})
server.on('error', function (err) { t.error(err) })
server.on('warning', function (err) { t.error(err) })
server.listen(0, function () {
var port = server[serverType].address().port
@ -29,3 +24,15 @@ exports.createServer = function (t, serverType, cb) {
cb(server, announceUrl)
})
}
exports.mockWebsocketTracker = function (client) {
client._trackers[0]._generateOffers = function (numwant, cb) {
var offers = []
for (var i = 0; i < numwant; i++) {
offers.push({ fake_offer: 'fake_offer_' + i })
}
process.nextTick(function () {
cb(offers)
})
}
}