From fff128745dae034d7f7533cd835b5e53210022e8 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 9 Oct 2018 11:20:13 +0100 Subject: [PATCH] fix: make ipfs.ping() options optional Without this PR: ```console $ npm test ...*snip* Uncaught TypeError: Cannot read property 'push' of null at /Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-stream/sinks/collect.js:7:9 at /Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-stream/sinks/reduce.js:8:11 at /Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-stream/sinks/drain.js:24:37 at callback (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-pushable/index.js:84:5) at Function.push (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-pushable/index.js:44:7) at libp2pNode.ping (src/core/components/ping-pull-stream.js:79:18) at _getPeerInfo (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/libp2p/src/index.js:339:7) at setImmediate (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/libp2p/src/get-peer-info.js:54:24) at Immediate. (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/async/internal/setImmediate.js:27:16) ``` Might resolve the second issue in #1616 --- src/core/components/ping.js | 5 +++ test/core/ping.spec.js | 64 +++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/core/components/ping.js b/src/core/components/ping.js index c5bfba9134..a92e31c24a 100644 --- a/src/core/components/ping.js +++ b/src/core/components/ping.js @@ -5,6 +5,11 @@ const pull = require('pull-stream/pull') module.exports = function ping (self) { return promisify((peerId, opts, cb) => { + if (typeof opts === 'function') { + cb = opts + opts = {} + } + pull( self.pingPullStream(peerId, opts), pull.collect(cb) diff --git a/test/core/ping.spec.js b/test/core/ping.spec.js index 583ff6c964..37121f7c55 100644 --- a/test/core/ping.spec.js +++ b/test/core/ping.spec.js @@ -13,6 +13,10 @@ const isNode = require('detect-node') const expect = chai.expect chai.use(dirtyChai) const df = DaemonFactory.create({ exec: 'src/cli/bin.js' }) +const dfProc = DaemonFactory.create({ + exec: require('../../'), + type: 'proc' +}) const config = { Bootstrap: [], @@ -24,9 +28,10 @@ const config = { } } -function spawnNode ({ dht = false }, cb) { +function spawnNode ({ dht = false, type = 'js' }, cb) { const args = dht ? ['--enable-dht-experiment'] : [] - df.spawn({ + const factory = type === 'js' ? df : dfProc + factory.spawn({ args, config, initOptions: { bits: 512 } @@ -43,6 +48,61 @@ describe('ping', function () { if (!isNode) return + describe('in-process daemon', function () { + let ipfsdA + let ipfsdB + let bMultiaddr + let ipfsdBId + + // Spawn nodes + before(function (done) { + this.timeout(60 * 1000) + + series([ + spawnNode.bind(null, { dht: false, type: 'proc' }), + spawnNode.bind(null, { dht: false }) + ], (err, ipfsd) => { + expect(err).to.not.exist() + ipfsdA = ipfsd[0] + ipfsdB = ipfsd[1] + done() + }) + }) + + // Get the peer info object + before(async function () { + this.timeout(60 * 1000) + + const peerInfo = await ipfsdB.api.id() + + ipfsdBId = peerInfo.id + bMultiaddr = peerInfo.addresses[0] + }) + + // Connect the nodes + before(async function () { + this.timeout(60 * 1000) + await ipfsdA.api.swarm.connect(bMultiaddr) + }) + + after(async () => { + if (!ipfsdA) return + await ipfsdA.stop() + }) + + after(async () => { + if (!ipfsdB) return + await ipfsdB.stop() + }) + + it('can ping via a promise without options', async () => { + const res = await ipfsdA.api.ping(ipfsdBId) + + expect(res.length).to.be.ok() + expect(res[0].success).to.be.true() + }) + }) + describe('DHT disabled', function () { // Without DHT nodes need to be previously connected let ipfsdA