From 5993a9bfdb2b3e212549e4b411ff45464c2fa66b Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Thu, 26 Apr 2018 10:29:32 -0700 Subject: [PATCH] feat(tests) move some bitswap tests to interface Will require including the version of interface-ipfs-core that they are moved to --- src/cli/commands/bitswap/unwant.js | 8 ++- src/cli/commands/bitswap/wantlist.js | 4 +- src/cli/commands/block/get.js | 7 ++- src/core/components/bitswap.js | 31 ++++++++---- src/http/api/resources/bitswap.js | 28 +++++------ src/http/api/resources/block.js | 8 ++- test/cli/bitswap.js | 2 +- test/core/bitswap.spec.js | 74 ---------------------------- test/core/interface/bitswap.js | 35 +++++++++++++ test/http-api/interface/bitswap.js | 31 ++++++++++++ 10 files changed, 120 insertions(+), 108 deletions(-) create mode 100644 test/core/interface/bitswap.js create mode 100644 test/http-api/interface/bitswap.js diff --git a/src/cli/commands/bitswap/unwant.js b/src/cli/commands/bitswap/unwant.js index 28eb1a7a14..ea676450fa 100644 --- a/src/cli/commands/bitswap/unwant.js +++ b/src/cli/commands/bitswap/unwant.js @@ -15,7 +15,11 @@ module.exports = { } }, handler (argv) { - argv.ipfs.bitswap.unwant(argv.key) - print(`Key ${argv.key} removed from wantlist`) + argv.ipfs.bitswap.unwant(argv.key, (err) => { + if (err) { + throw err + } + print(`Key ${argv.key} removed from wantlist`) + }) } } diff --git a/src/cli/commands/bitswap/wantlist.js b/src/cli/commands/bitswap/wantlist.js index 63692a993a..8ad26a439a 100644 --- a/src/cli/commands/bitswap/wantlist.js +++ b/src/cli/commands/bitswap/wantlist.js @@ -21,8 +21,8 @@ module.exports = { if (err) { throw err } - res.Keys.forEach((cidStr) => { - print(cidStr) + res.Keys.forEach((cid) => { + print(cid['/']) }) }) } diff --git a/src/cli/commands/block/get.js b/src/cli/commands/block/get.js index ed753aeadb..dd1e21d7fc 100644 --- a/src/cli/commands/block/get.js +++ b/src/cli/commands/block/get.js @@ -1,6 +1,7 @@ 'use strict' const CID = require('cids') +const print = require('../../utils').print module.exports = { command: 'get ', @@ -17,7 +18,11 @@ module.exports = { throw err } - process.stdout.write(block.data) + if (block) { + print(block.data) + } else { + print('Block was unwanted before it could be remotely retrieved') + } }) } } diff --git a/src/core/components/bitswap.js b/src/core/components/bitswap.js index 44d2d2d3d9..9f3c7bfbab 100644 --- a/src/core/components/bitswap.js +++ b/src/core/components/bitswap.js @@ -4,21 +4,23 @@ const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR const promisify = require('promisify-es6') const setImmediate = require('async/setImmediate') const Big = require('big.js') +const CID = require('cids') function formatWantlist (list) { - return Array.from(list).map((e) => e[1]) + return Array.from(list).map((e) => ({ '/': e[1].cid.toBaseEncodedString() })) } module.exports = function bitswap (self) { return { - wantlist: () => { + wantlist: promisify((callback) => { if (!self.isOnline()) { - throw new Error(OFFLINE_ERROR) + return setImmediate(() => callback(new Error(OFFLINE_ERROR))) } - const list = self._bitswap.getWantlist() - return formatWantlist(list) - }, + let list = self._bitswap.getWantlist() + list = formatWantlist(list) + callback(null, { Keys: list }) + }), stat: promisify((callback) => { if (!self.isOnline()) { @@ -40,12 +42,21 @@ module.exports = function bitswap (self) { }) }), - unwant: (key) => { + unwant: promisify((keys, callback) => { if (!self.isOnline()) { - throw new Error(OFFLINE_ERROR) + return setImmediate(() => callback(new Error(OFFLINE_ERROR))) } - self._bitswap.unwant(key) - } + if (!Array.isArray(keys)) { + keys = [keys] + } + keys = keys.map((key) => { + if (CID.isCID(key)) { + return key + } + return new CID(key) + }) + callback(null, self._bitswap.unwant(keys)) + }) } } diff --git a/src/http/api/resources/bitswap.js b/src/http/api/resources/bitswap.js index e9e78ca09f..f846d87ba2 100644 --- a/src/http/api/resources/bitswap.js +++ b/src/http/api/resources/bitswap.js @@ -7,16 +7,11 @@ const parseKey = require('./block').parseKey exports = module.exports exports.wantlist = (request, reply) => { - let list - try { - list = request.server.app.ipfs.bitswap.wantlist() - list = list.map((entry) => entry.cid.toBaseEncodedString()) - } catch (err) { - return reply(boom.badRequest(err)) - } - - reply({ - Keys: list + request.server.app.ipfs.bitswap.wantlist((err, list) => { + if (err) { + return reply(boom.badRequest(err)) + } + reply(list) }) } @@ -53,12 +48,11 @@ exports.unwant = { handler: (request, reply) => { const key = request.pre.args.key const ipfs = request.server.app.ipfs - try { - ipfs.bitswap.unwant(key) - } catch (err) { - return reply(boom.badRequest(err)) - } - - reply({ Key: key }) + ipfs.bitswap.unwant(key, (err) => { + if (err) { + return reply(boom.badRequest(err)) + } + reply({ key: key.toBaseEncodedString() }) + }) } } diff --git a/src/http/api/resources/block.js b/src/http/api/resources/block.js index 77553cbcaf..f536fb2a22 100644 --- a/src/http/api/resources/block.js +++ b/src/http/api/resources/block.js @@ -48,7 +48,13 @@ exports.get = { }).code(500) } - return reply(block.data) + if (block) { + return reply(block.data) + } + return reply({ + Message: 'Block was unwanted before it could be remotely retrieved', + Code: 0 + }).code(404) }) } } diff --git a/test/cli/bitswap.js b/test/cli/bitswap.js index 9fb042addc..6cdebe61de 100644 --- a/test/cli/bitswap.js +++ b/test/cli/bitswap.js @@ -13,7 +13,7 @@ describe('bitswap', () => runOn((thing) => { ipfs('block get ' + key) .then(() => {}) .catch(() => {}) - setTimeout(done, 800) + setTimeout(done, 250) }) it('wantlist', function () { diff --git a/test/core/bitswap.spec.js b/test/core/bitswap.spec.js index 55ac6116cf..645581f2c1 100644 --- a/test/core/bitswap.spec.js +++ b/test/core/bitswap.spec.js @@ -18,9 +18,6 @@ const CID = require('cids') const IPFSFactory = require('ipfsd-ctl') -// This gets replaced by '../utils/create-repo-browser.js' in the browser -const createTempRepo = require('../utils/create-repo-nodejs.js') - const IPFS = require('../../src/core') // TODO bitswap tests on windows is failing, missing proper shutdown of daemon @@ -247,75 +244,4 @@ skipOnWindows('bitswap', function () { }) }) }) - - describe('api', () => { - let node - - before(function (done) { - this.timeout(40 * 1000) - - node = new IPFS({ - repo: createTempRepo(), - start: false, - config: { - Addresses: { - Swarm: [] - }, - Discovery: { - MDNS: { - Enabled: false - } - } - } - }) - node.on('ready', () => done()) - }) - - describe('while offline', () => { - it('.wantlist throws if offline', () => { - expect(() => node.bitswap.wantlist()).to.throw(/online/) - }) - - it('.stat gives error while offline', () => { - node.bitswap.stat((err, stats) => { - expect(err).to.exist() - expect(stats).to.not.exist() - }) - }) - - it('.unwant throws if offline', () => { - expect(() => node.bitswap.unwant('my key')).to.throw(/online/) - }) - }) - - describe('while online', () => { - before(function (done) { - this.timeout(80 * 1000) - - node.start(() => done()) - }) - - it('.wantlist returns an array of wanted blocks', () => { - expect(node.bitswap.wantlist()).to.eql([]) - }) - - it('returns the stats', (done) => { - node.bitswap.stat((err, stats) => { - expect(err).to.not.exist() - expect(stats).to.have.keys([ - 'blocksReceived', - 'blocksSent', - 'dataReceived', - 'dataSent', - 'wantlist', - 'peers', - 'provideBufLen', - 'dupDataReceived', - 'dupBlksReceived' - ]) - done() - }) - }) - }) - }) }) diff --git a/test/core/interface/bitswap.js b/test/core/interface/bitswap.js new file mode 100644 index 0000000000..b7927dd5dd --- /dev/null +++ b/test/core/interface/bitswap.js @@ -0,0 +1,35 @@ +/* eslint-env mocha */ +'use strict' + +const test = require('interface-ipfs-core') +const parallel = require('async/parallel') + +const IPFS = require('../../../src') + +const DaemonFactory = require('ipfsd-ctl') +const df = DaemonFactory.create({ type: 'proc', exec: IPFS }) + +const nodes = [] +const common = { + setup: function (callback) { + callback(null, { + spawnNode: (cb) => { + df.spawn({ + initOptions: { bits: 512 } + }, (err, _ipfsd) => { + if (err) { + return cb(err) + } + + nodes.push(_ipfsd) + cb(null, _ipfsd.api) + }) + } + }) + }, + teardown: function (callback) { + parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) + } +} + +test.bitswap(common) diff --git a/test/http-api/interface/bitswap.js b/test/http-api/interface/bitswap.js new file mode 100644 index 0000000000..231a41cfe2 --- /dev/null +++ b/test/http-api/interface/bitswap.js @@ -0,0 +1,31 @@ +/* eslint-env mocha */ +'use strict' + +const test = require('interface-ipfs-core') +const parallel = require('async/parallel') + +const DaemonFactory = require('ipfsd-ctl') +const df = DaemonFactory.create({ exec: 'src/cli/bin.js' }) + +const nodes = [] +const common = { + setup: function (callback) { + callback(null, { + spawnNode: (cb) => { + df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => { + if (err) { + return cb(err) + } + + nodes.push(_ipfsd) + cb(null, _ipfsd.api) + }) + } + }) + }, + teardown: function (callback) { + parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) + } +} + +test.bitswap(common)