diff --git a/.gitignore b/.gitignore index dc89152..7c9e945 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ conanbuildinfo* node_modules/ dist/ joystream.log +bin/ diff --git a/conanfile.py b/conanfile.py index ef86629..4fa3726 100644 --- a/conanfile.py +++ b/conanfile.py @@ -5,12 +5,12 @@ class JoyStreamNode(ConanFile): requires = ("Libtorrent/1.1.1@joystream/stable", "LibtorrentNode/0.0.6@joystream/stable", "CoinCore/0.1.2@joystream/stable", - "Common/0.1.2@joystream/stable", + "Common/0.1.3@joystream/stable", "PaymentChannel/0.1.2@joystream/stable", "ProtocolWire/0.1.2@joystream/stable", - "ProtocolStateMachine/0.1.2@joystream/stable", - "ProtocolSession/0.1.4@joystream/stable", - "Extension/0.1.6@joystream/stable", + "ProtocolStateMachine/0.2.0@joystream/stable", + "ProtocolSession/0.2.0@joystream/stable", + "Extension/0.2.0@joystream/stable", "Boost/1.60.0@lasote/stable", "OpenSSL/1.0.2j@lasote/stable") diff --git a/examples/.gitignore b/examples/.gitignore index 2a7f179..f276268 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -1,5 +1,6 @@ -purchase/buyer/ -purchase/seller/ +buyer/downloads/ +seller/downloads/ -dht/buyer/ -dht/seller/ +sintel.mp4 +"stock footage - clouds.m2ts" +joystream.log diff --git a/examples/basic/README.md b/examples/basic/README.md deleted file mode 100644 index e73c10e..0000000 --- a/examples/basic/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Basic example - -In this example we show how to add a torrent and remove it. In order to buy or sell you will need to add a wallet to you application. diff --git a/examples/basic/basic.js b/examples/basic/basic.js deleted file mode 100644 index c947e34..0000000 --- a/examples/basic/basic.js +++ /dev/null @@ -1,55 +0,0 @@ -const Session = require('../../').Session -const TorrentInfo = require('../../').TorrentInfo - -console.log('====== Starting basic.js example ======') - -// start a joystream session -var session = new Session({port: 6881}) - -let addTorrentParams = { - ti: new TorrentInfo(__dirname + '/../../test/sintel.torrent'), - path: __dirname -} - -session.addTorrent(addTorrentParams, (err, torrent) => { - if (!err) { - console.log('====== Torrent Sintel Added ======') - - console.log(torrent) - - torrent.on('pieceFinished', (pieceIndex) => { - console.log('WE HAVE PIECE : ', pieceIndex) - }) - - torrent.on('state_changed', () => { - - var status = torrent.status() - - console.log(status) - - if (status.state === 5 || status.state === 3) { - var havePiece = torrent.handle.havePiece(0) - - var torrentInfo = torrent.handle.torrentFile() - var fileStorage = torrentInfo.files() - - if (havePiece) { - console.log('asking for piece !') - torrent.handle.readPiece(0) - } else { - console.log('We dont have piece') - } - } - }) - - /*session.removeTorrent(torrent.infoHash, (err, result) => { - if (!err) { - console.log('====== Torrent Sintel Removed ======') - } else { - console.error(err) - } - })*/ - } else { - console.error(err) - } -}) diff --git a/examples/buyer/buyer.js b/examples/buyer/buyer.js index 1784b8b..4b2e2a8 100644 --- a/examples/buyer/buyer.js +++ b/examples/buyer/buyer.js @@ -47,6 +47,10 @@ function letsBuy (torrent) { }) }) + torrent.on('allSellersGone', function() { + lookingForSeller = true + }) + // Wait for one suitable seller and start downloading torrent.on('peerPluginStatusUpdates', function (peerStatuses) { if (!lookingForSeller) return diff --git a/examples/dht/README.md b/examples/dht/README.md deleted file mode 100644 index 091fa00..0000000 --- a/examples/dht/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# DHT example - -Show how to update dht. diff --git a/examples/dht/index.js b/examples/dht/index.js deleted file mode 100644 index 29d427e..0000000 --- a/examples/dht/index.js +++ /dev/null @@ -1,78 +0,0 @@ -const Session = require('../../').Session -const TorrentInfo = require('../../').TorrentInfo -const path = require('path') - -const sintelTorrentPath = path.join(__dirname, '/../../test/sintel.torrent') - -/* * * * * * * * * * * - * - * Buyer - * - * * * * * * * * * * */ - -var buyerSession = new Session({ - port: 6882 -}) - -let addTorrentParamsBuyer = { - ti: new TorrentInfo(sintelTorrentPath), - savePath: path.join(__dirname, '/buyer/') -} - - /* * * * * * * * * * * - * - * Seller - * - * * * * * * * * * * */ - -var sellerSession = new Session({ - port: 6881 -}) - -let addTorrentParamsSeller = { - ti: new TorrentInfo(sintelTorrentPath), - savePath: path.join(__dirname, '/seller/') -} - -/* * * * * * * * * * * - * - * DHT test - * - * * * * * * * * * * */ - -// Seller adding torrent and starting sell mode -sellerSession.addTorrent(addTorrentParamsSeller, (err, torrent) => { - if (!err) { - console.log('Torrent added to seller session') - torrent.on('dhtGetPeersReply', (peers) => { - for (var i in peers) { - console.log(peers[i]) - torrent.connectPeer(peers[i]) - } - }) - } else { - console.error(err) - } -}) - -/*buyerSession.addTorrent(addTorrentParamsBuyer, (err, torrent) => { - if (!err) { - console.log('Torrent added to buyer session') - } else { - console.error(err) - } -})*/ - -setInterval(() => { - console.log('DHT announce') - - for (var [secondaryInfoHash] of sellerSession.torrentsBySecondaryHash.entries()) { - sellerSession.dhtAnnounce(secondaryInfoHash) - sellerSession.dhtGetPeers(secondaryInfoHash) - } - - /*for (var [secondaryInfoHash] of buyerSession.torrentsBySecondaryHash.entries()) { - buyerSession.dhtAnnounce(secondaryInfoHash) - buyerSession.dhtGetPeers(secondaryInfoHash) - }*/ -}, 5000) diff --git a/examples/purchase/purchase.js b/examples/purchase/purchase.js deleted file mode 100644 index 04c53f0..0000000 --- a/examples/purchase/purchase.js +++ /dev/null @@ -1,262 +0,0 @@ -'use strict' - -const Session = require('../../').Session -const TorrentInfo = require('../../').TorrentInfo -const path = require('path') -const areTermsMatching = require('../../lib/utils').areTermsMatching -const ConnectionInnerState = require('../../').ConnectionInnerState -const TorrentState = require('../../').TorrentState -const LibtorrentInteraction = require('../../').LibtorrentInteraction - -const sfcTorrentPath = path.join(__dirname, '/../../test/sfc.torrent') -const sintelTorrentPath = path.join(__dirname, '/../../test/sintel.torrent') - -var buyerSession = new Session({ - port: 6881 -}) - -let addTorrentParamsBuyer = { - ti: new TorrentInfo(sintelTorrentPath), - savePath: path.join(__dirname, '/buyer/') -} - -function letsBuy (torrent) { - console.log('torrent in downloading state, going to buy mode') - // 100, 5, 1, 20000 - const buyerTerms = { - maxPrice: 100, - maxLock: 5, - minNumberOfSellers: 1, - maxContractFeePerKb: 20000 - } - - let lookingForSeller = false - - torrent.toBuyMode(buyerTerms, (err) => { - if (err) { - return console.log(err) - } - - torrent.startPlugin(function (err) { - if(err) { - return console.log(err) - } - - console.log('We are in buying mode') - lookingForSeller = true - }) - }) - - // Wait for one suitable seller and start downloading - torrent.on('peerPluginStatusUpdates', function (peerStatuses) { - if (!lookingForSeller) return - - console.log('looking for seller') - - const connection = pickSuitableSeller(peerStatuses, buyerTerms) - - if (!connection) return - - console.log('found a suitable seller', connection) - - // Stop looking for seller - lookingForSeller = false - - const pid = connection.pid - const sellerTerms = connection.announcedModeAndTermsFromPeer.seller.terms - - let setup = makeContractAndDownloadInfoMap(pid, sellerTerms) - - torrent.startDownloading(setup.contract, setup.map, function (err) { - if (err) { - console.log(err) - lookingForSeller = true - return - } - console.log('Started Downloading From Seller') - }) - }) -} - -function makeContractAndDownloadInfoMap (pid, sellerTerms) { - // Fake contract - const contract = Buffer.from('01000000017b1eabe0209b1fe794124575ef807057c77ada2138ae4fa8d6c4de0398a14f3f00000000494830450221008949f0cb400094ad2b5eb399d59d01c14d73d8fe6e96df1a7150deb388ab8935022079656090d7f6bac4c9a94e0aad311a4268e082a725f8aeae0573fb12ff866a5f01ffffffff01f0ca052a010000001976a914cbc20a7664f2f69e5355aa427045bc15e7c6c77288ac00000000', 'hex') - - // Download info map for one seller - const map = new Map() - - map.set(pid, { - index: 0, - value: 100000, - sellerTerms: sellerTerms, - buyerContractSk: Buffer.from('0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20', 'hex'), - buyerFinalPkHash: new Buffer(20) - }) - - return {contract, map} -} - -function pickSuitableSeller (peerStatuses, buyerTerms) { - for (var i in peerStatuses) { - const status = peerStatuses[i] - - if (!status.connection) continue - - //console.log(status.connection.announcedModeAndTermsFromPeer) - - // connection must be in PerparingContract state - if (status.connection.innerState !== ConnectionInnerState.PreparingContract) { - console.log('not in preparing contract status') - continue - } - - try { - // lazy checking for seller, if peer is not a seller this will throw.. - const sellerTerms = status.connection.announcedModeAndTermsFromPeer.seller.terms - console.log(sellerTerms) - if (areTermsMatching(buyerTerms, sellerTerms)) { - return status.connection - } - - } catch (e) { console.log(e) } - } - - return null -} - -buyerSession.addTorrent(addTorrentParamsBuyer, (err, torrent) => { - if (err) { - return console.log(err) - } - - console.log(torrent) - - torrent.setLibtorrentInteraction(LibtorrentInteraction.BlockUploadingAndDownloading) - - // Wait for libtorrent state to be downloading - waitForState(torrent, TorrentState.downloading, function () { - letsBuy(torrent) - }) - - // Wait for libtorrent state to be seeding which means we already - // have it and it doesn't make sense to try to buy it, or we completed downloading it - waitForState(torrent, TorrentState.seeding, function () { - console.log('Torrent downloaded, exiting') - process.exit() - }) -}) - -function waitForState (torrent, targetState, callback) { - function checkState () { - if (torrent.status().state === targetState) { - torrent.removeListener('state_changed', checkState) - callback() - } - } - - if (torrent.status().state === targetState) { - callback() - } else { - torrent.on('state_changed', checkState) - } -} - -var sellerSession = new Session({ - port: 6882 -}) - -let addTorrentParamsSeller = { - ti: new TorrentInfo(sintelTorrentPath), - savePath: path.join(__dirname, '/seller/') -} - -function letsSell (torrent) { - // 50, 1, 10, 15000, 5000 - let sellerTerms = { - minPrice: 50, - minLock: 1, - maxNumberOfSellers: 10, - minContractFeePerKb: 15000, - settlementFee: 5000 - } - - let lookingForBuyer = false - - let contractSk = Buffer.from('030589ee559348bd6a7325994f9c8eff12bd5d73cc683142bd0dd1a17abc99b0', 'hex') - let finalPkHash = new Buffer(20) - - torrent.toSellMode(sellerTerms, (err, result) => { - if (err) { - return console.log(err) - } - - torrent.startPlugin(function (err) { - if(err) { - return console.log(err) - } - - console.log('We are in sell mode') - lookingForBuyer = true - }) - }) - - // Wait for one suitable buyer and start uploading - torrent.on('peerPluginStatusUpdates', function (peerStatuses) { - if (!lookingForBuyer) return - - let connection = pickSuitableBuyer(peerStatuses, sellerTerms) - - if (!connection) return - - lookingForBuyer = false - - console.log('Found Suitable buyer', connection) - - const pid = connection.pid - const buyerTerms = connection.announcedModeAndTermsFromPeer.buyer.terms - - torrent.startUploading(pid, buyerTerms, contractSk, finalPkHash, (err) => { - if (err) { - console.log('Failed to start uploading to buyer', err) - lookingForBuyer = true - } else { - console.log('Started Selling To Buyer', connection) - } - }) - }) -} - -function pickSuitableBuyer (peerStatuses, sellerTerms) { - for (var i in peerStatuses) { - const status = peerStatuses[i] - - if(!status.connection) continue - - // Buyer must have invited us - if(status.connection.innerState !== ConnectionInnerState.Invited) continue - - try { - // lazy checking for buyer - const buyerTerms = status.connection.announcedModeAndTermsFromPeer.buyer.terms - if(areTermsMatching(buyerTerms, sellerTerms)){ - return status.connection - } - } catch (e) {} - } - - return null -} - -sellerSession.addTorrent(addTorrentParamsSeller, (err, torrent) => { - if (err) { - return console.log(err) - } - - console.log(torrent) - - torrent.setLibtorrentInteraction(LibtorrentInteraction.BlockUploadingAndDownloading) - - waitForState(torrent, TorrentState.seeding, function () { - letsSell(torrent) - }) -}) diff --git a/lib/Session.js b/lib/Session.js index b8c2b43..2ffe6ca 100644 --- a/lib/Session.js +++ b/lib/Session.js @@ -402,6 +402,11 @@ class Session extends EventEmitter { this._downloadStarted(alert) break + // AllSellersGone + case JoyStreamAddon.AlertType.AllSellersGone: + this._allSellersGone(alert) + break + default: //console.log(alert.message) break @@ -857,6 +862,10 @@ class Session extends EventEmitter { _downloadStarted (alert) { this.__emitEventOnValidTorrent('downloadStarted', alert) } + + _allSellersGone (alert) { + this.__emitEventOnValidTorrent('allSellersGone', alert) + } } module.exports = Session diff --git a/package.json b/package.json index 481ab82..8c3669c 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,17 @@ { "name": "joystream-node", - "version": "0.1.0", + "version": "0.2.0", "description": "JoyStream payment protocol over BitTorrent", - "keywords": ["libtorrent", "bitcoin", "micropayment"], + "keywords": [ + "libtorrent", + "bitcoin", + "micropayment" + ], "homepage": "https://github.com/JoyStream/joystream-node", "bugs": "https://github.com/JoyStream/joystream-node/issues", - "repository" : { - "type" : "git", - "url" : "https://github.com/JoyStream/joystream-node" + "repository": { + "type": "git", + "url": "https://github.com/JoyStream/joystream-node" }, "main": "index.js", "scripts": { diff --git a/src/Connection.cpp b/src/Connection.cpp index 10bfdb1..4567646 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -23,7 +23,7 @@ namespace connection { typedef std::pair TypeInfo; #define STATE_TO_TYPE_INFO(name) (std::make_pair(std::type_index(typeid(protocol_statemachine::name)), #name)) - static const std::array InnerStateTypeInfo = { + static const std::array InnerStateTypeInfo = { // Initial picker state, in practice we should never be here given // current behaviour of plugin @@ -37,10 +37,8 @@ namespace connection { STATE_TO_TYPE_INFO(Invited), // (active selling state) STATE_TO_TYPE_INFO(WaitingToStart), - STATE_TO_TYPE_INFO(ReadyForPieceRequest), - // ** Servicing Piece Request - STATE_TO_TYPE_INFO(LoadingPiece), - STATE_TO_TYPE_INFO(WaitingForPayment), + // *StartedSelling + STATE_TO_TYPE_INFO(ServicingPieceRequests), // * Buying STATE_TO_TYPE_INFO(ReadyToInviteSeller), @@ -48,9 +46,7 @@ namespace connection { // ** Seller has joined STATE_TO_TYPE_INFO(PreparingContract), // (active buying state) - STATE_TO_TYPE_INFO(ReadyToRequestPiece), - STATE_TO_TYPE_INFO(WaitingForFullPiece), - STATE_TO_TYPE_INFO(ProcessingPiece) + STATE_TO_TYPE_INFO(RequestingPieces) }; NAN_MODULE_INIT(InitInnerStateTypes); diff --git a/src/PluginAlertEncoder.cpp b/src/PluginAlertEncoder.cpp index fa2f3e5..08340f6 100644 --- a/src/PluginAlertEncoder.cpp +++ b/src/PluginAlertEncoder.cpp @@ -61,6 +61,7 @@ namespace PluginAlertEncoder { else ENCODE_PLUGIN_ALERT(SendingPieceToBuyer) else ENCODE_PLUGIN_ALERT(PieceRequestedByBuyer) else ENCODE_PLUGIN_ALERT(AnchorAnnounced) + else ENCODE_PLUGIN_ALERT(AllSellersGone) return v; } @@ -95,6 +96,7 @@ namespace PluginAlertEncoder { SET_JOYSTREAM_PLUGIN_ALERT_TYPE(object, SendingPieceToBuyer) SET_JOYSTREAM_PLUGIN_ALERT_TYPE(object, PieceRequestedByBuyer) SET_JOYSTREAM_PLUGIN_ALERT_TYPE(object, AnchorAnnounced) + SET_JOYSTREAM_PLUGIN_ALERT_TYPE(object, AllSellersGone) SET_VAL(target, "AlertType", object); @@ -310,6 +312,11 @@ namespace PluginAlertEncoder { return v; } + v8::Local encode(joystream::extension::alert::AllSellersGone const * p) { + auto v = libtorrent::node::alert_types::encode(static_cast(p)); + + return v; + } } } } diff --git a/src/PluginAlertEncoder.hpp b/src/PluginAlertEncoder.hpp index aa67921..7cc9d47 100644 --- a/src/PluginAlertEncoder.hpp +++ b/src/PluginAlertEncoder.hpp @@ -41,6 +41,7 @@ namespace alert { struct SendingPieceToBuyer; struct PieceRequestedByBuyer; struct AnchorAnnounced; + struct AllSellersGone; } } namespace node { @@ -75,6 +76,7 @@ namespace PluginAlertEncoder { v8::Local encode(extension::alert::SendingPieceToBuyer const * p); v8::Local encode(extension::alert::PieceRequestedByBuyer const * p); v8::Local encode(extension::alert::AnchorAnnounced const * p); + v8::Local encode(extension::alert::AllSellersGone const * p); } } diff --git a/test/Session-test.js b/test/Session-test.js index b7699dd..11bfbda 100644 --- a/test/Session-test.js +++ b/test/Session-test.js @@ -2,106 +2,111 @@ var lib = require('../') var assert = require('assert') -describe('Session class', function () { - describe('Adding torrent to plugin', function () { - it('Add torrent with torrent info file', function () { - let addTorrentParams = { - ti: new lib.TorrentInfo(__dirname + '/sintel.torrent'), - savePath: __dirname - } - var app = new lib.Session({ - port: 6881 - }) +// Test AddTorrentParams +const TORRENTS = { + file: { + ti: new lib.TorrentInfo(__dirname + '/sintel.torrent'), + savePath: __dirname, + paused: true + }, + + magnet: { + url: 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&dn=sintel.mp4&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.webtorrent.io&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel-1024-surround.mp4', + path: __dirname, + paused: true + }, + + infohash:{ + infoHash: '6a9759bffd5c0af65319979fb7832189f4f3c35d', + name: 'sintel.mp4', + path: __dirname, + paused: true + } +} + +describe('Session', function () { + // default 2s is not enough for most of the operations we are testing + // which make take multiple pop alert intervals to complete. + this.timeout(5000) + + var session; + + before(function () { + session = new lib.Session({ + port: 6881 + }) + }) + + // start with empty session before each testcase + beforeEach(function (done) { + var callbacks = [] + + // remove all torrents + for (var [infoHash] of session.torrents.entries()) { + callbacks.push(new Promise(function (resolve) { + session.removeTorrent(infoHash, resolve) + })) + } + + // Wait for all callacks to be called + Promise.all(callbacks).then(function () { + done() + }) + }) + + describe('Adding torrents', function () { - app.addTorrent(addTorrentParams, (err, torrent) => { + it('Add torrent with torrent info file', function (done) { + session.addTorrent(TORRENTS.file, (err, torrent) => { assert(!err) + done() }) }) - it('Add torrent with magnet url', function () { - let addTorrentParams = { - url: 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&dn=sintel.mp4&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.webtorrent.io&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel-1024-surround.mp4', - path: __dirname - } - var app = new lib.Session({ - port: 6881 - }) - app.addTorrent(addTorrentParams, (err, torrent) => { + it('Add torrent with magnet url', function (done) { + session.addTorrent(TORRENTS.magnet, (err, torrent) => { assert(!err) + assert(torrent.infoHash === '6a9759bffd5c0af65319979fb7832189f4f3c35d') + done() }) }) - it('Add torrent with info_hash', function () { - let addTorrentParams = { - infoHash: '6a9759bffd5c0af65319979fb7832189f4f3c35d', - name: 'sintel.mp4', - path: __dirname - } - var app = new lib.Session({ - port: 6881 - }) - app.addTorrent(addTorrentParams, (err, torrent) => { + + it('Add torrent with info_hash', function (done) { + session.addTorrent(TORRENTS.infohash, (err, torrent) => { assert(!err) + done() }) }) - it('torrentsBySecondaryHash map updated', function () { - let addTorrentParams = { - infoHash: '6a9759bffd5c0af65319979fb7832189f4f3c35d', - name: 'sintel.mp4', - path: __dirname - } - var app = new lib.Session({ - port: 6881 - }) - app.addTorrent(addTorrentParams, (err, torrent) => { - assert(this.torrentsBySecondaryHash.has(torrent.secondaryInfoHash)) + + it('torrentsBySecondaryHash map updated', function (done) { + session.addTorrent(TORRENTS.infohash, (err, torrent) => { + assert(session.torrentsBySecondaryHash.has(torrent.secondaryInfoHash)) + done() }) }) }) - describe('Removing torrent from plugin', function () { - it('Remove torrent', function () { - let addTorrentParams = { - ti: new lib.TorrentInfo(__dirname + '/sintel.torrent'), - savePath: __dirname - } - var app = new lib.Session({ - port: 6881 - }) - app.addTorrent(addTorrentParams, (err, torrent) => { - var infoHash = torrent.handle.infoHash() - app.removeTorrent(infoHash, (err, result) => { + describe('Removing torrent from plugin', function () { + it('Remove torrent', function (done) { + session.addTorrent(TORRENTS.infohash, (err, torrent) => { + session.removeTorrent(torrent.infoHash, (err, result) => { assert(!err) + assert(!session.torrents.has(torrent.infoHash)) + done() }) }) }) - it('Check if torrent removed from app', function () { - let addTorrentParams = { - ti: new lib.TorrentInfo(__dirname + '/sintel.torrent'), - savePath: __dirname - } - var app = new lib.Session({ - port: 6881 - }) - app.addTorrent(addTorrentParams, (err, torrent) => { - var infoHash = torrent.handle.infoHash() - app.removeTorrent(infoHash, (err, result) => { - assert(!app.torrents.get(infoHash)) + it('torrentsBySecondaryHash map updated after removed', function (done) { + session.addTorrent(TORRENTS.infohash, (err, torrent) => { + assert(!err) + session.removeTorrent(torrent.infoHash, (err, result) => { + assert(!err) + assert(!session.torrents.has(torrent.infoHash)) + assert(!session.torrentsBySecondaryHash.has(torrent.secondaryInfoHash)) + done() }) }) }) }) - it('torrentsBySecondaryHash map updated after removed', function () { - let addTorrentParams = { - infoHash: '6a9759bffd5c0af65319979fb7832189f4f3c35d', - name: 'sintel.mp4', - path: __dirname - } - var app = new lib.Session({ - port: 6881 - }) - app.addTorrent(addTorrentParams, (err, torrent) => { - assert(!this.torrentsBySecondaryHash.has(torrent.secondaryInfoHash)) - }) - }) })