Skip to content

Commit

Permalink
ensure the torrent event is emitted (mafintosh#189)
Browse files Browse the repository at this point in the history
* ensure the torrent event is emitted

* fix tracker test

* remove v4 node as no longer compatible
  • Loading branch information
patmooney authored and mafintosh committed Sep 12, 2018
1 parent 74cae11 commit fe1ffc0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ node_js:
- "node"
- "8"
- "6"
- "4"
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ var torrentStream = function (link, opts, cb) {
rechokeIntervalId = setInterval(onrechoke, RECHOKE_INTERVAL)

process.nextTick(function () {
engine.emit('torrent', torrent)
engine.emit('ready')
refresh()
})
Expand Down
43 changes: 43 additions & 0 deletions test/meta-exchange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var test = require('tap').test
var torrents = require('../')
var fs = require('fs')
var path = require('path')

var fixture = torrents(fs.readFileSync(path.join(__dirname, 'data', 'star.torrent')), {
dht: false,
tracker: false
})
fixture.listen(10000)

var engine = function () {
var e = torrents('magnet:?xt=urn:btih:ef330b39f4801d25b4245212e75a38634bfc856e', {
dht: false,
tracker: false
})

e.connect('127.0.0.1:10000')
return e
}

test('fixture should be ready', function (t) {
t.plan(1)
fixture.on('ready', t.ok.bind(t, true, 'should be ready'))
})

test('torrent should be emitted', function (t) {
t.plan(1)
var e = engine()
e.on('torrent', function (torrent) {
t.equal(torrent.infoHash, 'ef330b39f4801d25b4245212e75a38634bfc856e')
e.destroy(t.ok.bind(t, true, 'should be destroyed'))
})
})

test('remove fixture and all content', function (t) {
t.plan(1)
fixture.destroy(function () {
fixture.remove(function () {
t.ok(!fs.existsSync(fixture.path))
})
})
})
16 changes: 10 additions & 6 deletions test/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,25 @@ test('peer should connect to the swarm using magnet link and trackers', function

test('peer should connect to an alternate tracker', function (t) {
t.plan(5)
var engine = null
var server = new tracker.Server()
server.once('listening', function () {
t.ok(true, 'tracker should be listening')

engine = torrents(torrent, { dht: false, trackers: ['http://127.0.0.1:54321/announce'] })
engine.once('ready', function () {
t.ok(true, 'should be ready')
})
})
server.once('start', function (addr) {
t.equal(addr, '127.0.0.1:6881')
})
server.listen(54321)

var engine = torrents(torrent, { dht: false, trackers: ['http://127.0.0.1:54321/announce'] })
engine.once('ready', function () {
t.ok(true, 'should be ready')
engine.destroy(t.ok.bind(t, true, 'should be destroyed'))
engine.destroy(function () {
engine.remove(t.ok.bind(t, true, 'should be destroyed'))
})
server.close(t.ok.bind(t, true, 'tracker should be closed'))
})
server.listen(54321)
})

test('cleanup', function (t) {
Expand Down

0 comments on commit fe1ffc0

Please sign in to comment.