bittorrent-tracker/test/udp-client.js

78 lines
2.0 KiB
JavaScript
Raw Normal View History

2014-03-28 04:02:11 +00:00
var Client = require('../').Client
var fs = require('fs')
var parseTorrent = require('parse-torrent')
2014-05-24 04:17:31 +00:00
var portfinder = require('portfinder')
var Server = require('../').Server
2014-03-28 04:02:11 +00:00
var test = require('tape')
var torrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var parsedTorrent = parseTorrent(torrent)
var peerId = new Buffer('01234567890123456789')
var port = 6881
test('udp: client.start/update/stop()', function (t) {
2014-05-24 04:17:31 +00:00
t.plan(12)
2014-03-28 04:02:11 +00:00
2014-05-24 04:17:31 +00:00
var server = new Server({ udp: false })
2014-03-28 04:02:11 +00:00
2014-05-24 04:17:31 +00:00
server.on('error', function (err) {
t.fail(err.message)
2014-03-28 04:02:11 +00:00
})
2014-05-24 04:17:31 +00:00
server.on('warning', function (err) {
t.fail(err.message)
2014-03-28 04:02:11 +00:00
})
2014-05-24 04:17:31 +00:00
var announceUrl
portfinder.getPort(function (err, port) {
t.error(err, 'found free port')
// remove all tracker servers except a single UDP one, for now
announceUrl = 'http://127.0.0.1:' + port + '/announce'
parsedTorrent.announce = [ announceUrl ]
server.listen(port)
var client = new Client(peerId, port, parsedTorrent)
client.on('error', function (err) {
t.error(err)
})
2014-03-28 04:02:11 +00:00
client.once('update', function (data) {
t.equal(data.announce, announceUrl)
2014-03-28 04:02:11 +00:00
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
2014-05-24 04:17:31 +00:00
})
client.once('peer', function (addr) {
t.pass('there is at least one peer') // TODO: this shouldn't rely on an external server!
2014-03-28 04:02:11 +00:00
client.once('update', function (data) {
2014-05-24 04:17:31 +00:00
// receive one final update after calling stop
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
2014-05-24 04:17:31 +00:00
client.once('update', function (data) {
// received an update!
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
server.close(function () {
t.pass('server close')
})
})
client.stop()
})
2014-05-24 04:17:31 +00:00
client.update()
2014-03-28 04:02:11 +00:00
})
2014-05-24 04:17:31 +00:00
client.start()
2014-03-28 04:02:11 +00:00
})
})