From 614ec03d10611e4f47375c2a68a819f57cb6144f Mon Sep 17 00:00:00 2001 From: mkg20001 Date: Wed, 6 Sep 2017 15:46:56 +0200 Subject: [PATCH 1/7] test: add libp2p-websocket-star tests --- .aegir.js | 26 +- package.json | 2 + test/browser-bundle/browser-bundle.js | 9 +- test/browser-bundle/websocket-star-only.js | 121 +++++++++ test/browser.js | 1 + test/node.js | 1 + .../tcp+websockets+websocket-star.js | 247 ++++++++++++++++++ 7 files changed, 398 insertions(+), 9 deletions(-) create mode 100644 test/browser-bundle/websocket-star-only.js create mode 100644 test/nodejs-bundle/tcp+websockets+websocket-star.js diff --git a/.aegir.js b/.aegir.js index e7fff53c0e..d70888eeae 100644 --- a/.aegir.js +++ b/.aegir.js @@ -6,7 +6,9 @@ const PeerId = require('peer-id') const pull = require('pull-stream') const sigServer = require('libp2p-webrtc-star/src/sig-server') +const WSsigServer = require('libp2p-websocket-star-rendezvous') let server +let server2 let node const rawPeer = require('./test/browser-bundle/peer.json') @@ -15,7 +17,9 @@ const before = (done) => { let count = 0 const ready = () => ++count === 2 ? done() : null - sigServer.start({ port: 15555 }, (err, _server) => { + sigServer.start({ + port: 15555 + }, (err, _server) => { if (err) { throw err } @@ -23,6 +27,18 @@ const before = (done) => { ready() }) + WSsigServer.start({ + port: 14444, + refreshPeerListIntervalMS: 1000, + strictMultiaddr: false + }, (err, _server) => { + if (err) { + throw err + } + server2 = _server + ready() + }) + PeerId.createFromJSON(rawPeer, (err, peerId) => { if (err) { return done(err) @@ -38,12 +54,7 @@ const before = (done) => { } const after = (done) => { - setTimeout(() => node.stop((err) => { - if (err) { - return done(err) - } - server.stop(done) - }), 2000) + setTimeout(() => require('async/each')([node, server, server2], (s, n) => s.stop(n), done), 2000) } module.exports = { @@ -52,4 +63,3 @@ module.exports = { post: after } } - diff --git a/package.json b/package.json index 61ebf09067..42f10852b9 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,8 @@ "libp2p-tcp": "~0.11.1", "libp2p-webrtc-star": "~0.13.2", "libp2p-websockets": "~0.10.4", + "libp2p-websocket-star": "~0.5.0", + "libp2p-websocket-star-rendezvous": "~0.2.0", "lodash.times": "^4.3.2", "pre-commit": "^1.2.2", "pull-goodbye": "0.0.2", diff --git a/test/browser-bundle/browser-bundle.js b/test/browser-bundle/browser-bundle.js index 9d80ad1948..70d79dbd7a 100644 --- a/test/browser-bundle/browser-bundle.js +++ b/test/browser-bundle/browser-bundle.js @@ -2,6 +2,7 @@ const WS = require('libp2p-websockets') const WebRTCStar = require('libp2p-webrtc-star') +const WebSocketStar = require('libp2p-websocket-star') const spdy = require('libp2p-spdy') const multiplex = require('libp2p-multiplex') const secio = require('libp2p-secio') @@ -36,11 +37,13 @@ class Node extends libp2p { constructor (peerInfo, peerBook, options) { options = options || {} const webRTCStar = new WebRTCStar() + const wsStar = new WebSocketStar({ id: peerInfo.id }) const modules = { transport: [ new WS(), - webRTCStar + webRTCStar, + wsStar ], connection: { muxer: getMuxers(options.muxer), @@ -55,6 +58,10 @@ class Node extends libp2p { modules.discovery.push(webRTCStar.discovery) } + if (options.wsStar) { + modules.discovery.push(wsStar.discovery) + } + if (options.bootstrap) { const r = new Railing(options.bootstrap) modules.discovery.push(r) diff --git a/test/browser-bundle/websocket-star-only.js b/test/browser-bundle/websocket-star-only.js new file mode 100644 index 0000000000..950fe49173 --- /dev/null +++ b/test/browser-bundle/websocket-star-only.js @@ -0,0 +1,121 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +chai.use(require('dirty-chai')) +const expect = chai.expect +const PeerInfo = require('peer-info') +const PeerId = require('peer-id') +const parallel = require('async/parallel') +const pull = require('pull-stream') + +const Node = require('./browser-bundle') + +describe('libp2p-ipfs-browser (websocket-star only)', () => { + let peer1 + let peer2 + let node1 + let node2 + + it('create two peerInfo with websocket-star addrs', (done) => { + parallel([ + (cb) => PeerId.create({ bits: 1024 }, cb), + (cb) => PeerId.create({ bits: 1024 }, cb) + ], (err, ids) => { + expect(err).to.not.exist() + + peer1 = new PeerInfo(ids[0]) + const ma1 = '/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/' + peer1.multiaddrs.add(ma1) + + peer2 = new PeerInfo(ids[1]) + const ma2 = '/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/' + peer2.multiaddrs.add(ma2) + + done() + }) + }) + + it('create two libp2p nodes with those peers', (done) => { + node1 = new Node(peer1, null, { wsStar: true }) + node2 = new Node(peer2, null, { wsStar: true }) + done() + }) + + it('listen on the two libp2p nodes', (done) => { + parallel([ + (cb) => node1.start(cb), + (cb) => node2.start(cb) + ], done) + }) + + it('handle a protocol on the first node', () => { + node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) + }) + + it('dial from the second node to the first node', (done) => { + node1.dial(peer2, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + const text = 'hello' + const peers1 = node1.peerBook.getAll() + expect(Object.keys(peers1)).to.have.length(1) + + const peers2 = node2.peerBook.getAll() + expect(Object.keys(peers2)).to.have.length(1) + + pull( + pull.values([Buffer.from(text)]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist() + expect(data[0].toString()).to.equal(text) + done() + }) + ) + } + }) + }) + + it('node1 hangUp node2', (done) => { + node1.hangUp(peer2, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + const peers = node1.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0) + done() + } + }) + }) + + it('create a third node and check that discovery works', (done) => { + let counter = 0 + + function check () { + if (++counter === 3) { + expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1) + expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1) + done() + } + } + + PeerId.create((err, id3) => { + expect(err).to.not.exist() + + const peer3 = new PeerInfo(id3) + const ma3 = '/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/ipfs/' + id3.toB58String() + peer3.multiaddrs.add(ma3) + + node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check)) + node2.on('peer:discovery', (peerInfo) => node2.dial(peerInfo, check)) + + const node3 = new Node(peer3, null, { wsStar: true }) + node3.start(check) + }) + }) +}) diff --git a/test/browser.js b/test/browser.js index 6bbaf816a9..7f4fd5221b 100644 --- a/test/browser.js +++ b/test/browser.js @@ -5,3 +5,4 @@ const w = require('webrtcsupport') require('./base') require('./browser-bundle/websockets-only') if (w.support) { require('./browser-bundle/webrtc-star-only') } +require('./browser-bundle/websocket-star-only') diff --git a/test/node.js b/test/node.js index 2c579ab89c..02164ac201 100644 --- a/test/node.js +++ b/test/node.js @@ -4,6 +4,7 @@ require('./base') require('./nodejs-bundle/tcp') require('./nodejs-bundle/tcp+websockets') require('./nodejs-bundle/tcp+websockets+webrtc-star') +require('./nodejs-bundle/tcp+websockets+websocket-star') require('./nodejs-bundle/stream-muxing') require('./nodejs-bundle/discovery') require('./nodejs-bundle/peer-routing') diff --git a/test/nodejs-bundle/tcp+websockets+websocket-star.js b/test/nodejs-bundle/tcp+websockets+websocket-star.js new file mode 100644 index 0000000000..4cc322f567 --- /dev/null +++ b/test/nodejs-bundle/tcp+websockets+websocket-star.js @@ -0,0 +1,247 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +chai.use(require('dirty-chai')) +const expect = chai.expect +const parallel = require('async/parallel') +const signalling = require('libp2p-websocket-star-signal') +const WStar = require('libp2p-websocket-star') +const utils = require('./utils') +const createNode = utils.createNode +const echo = utils.echo + +describe('TCP + WebSockets + WebSocketStar', () => { + let nodeAll + let nodeTCP + let nodeWS + let nodeWStar + + let ss + + before((done) => { + parallel([ + (cb) => { + signalling.start({ port: 24642 }, (err, server) => { + expect(err).to.not.exist() + ss = server + cb() + }) + }, + (cb) => { + const wstar = new WStar({}) + createNode([ + '/ip4/0.0.0.0/tcp/0', + '/ip4/127.0.0.1/tcp/25011/ws', + '/ip4/127.0.0.1/tcp/24642/ws/p2p-websocket-star' + ], { + modules: { + transport: [wstar], + discovery: [wstar.discovery] + } + }, (err, node) => { + expect(err).to.not.exist() + nodeAll = node + wstar.lazySetId(node.peerInfo.id) + node.handle('/echo/1.0.0', echo) + node.start(cb) + }) + }, + (cb) => createNode([ + '/ip4/0.0.0.0/tcp/0' + ], (err, node) => { + expect(err).to.not.exist() + nodeTCP = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }), + (cb) => createNode([ + '/ip4/127.0.0.1/tcp/25022/ws' + ], (err, node) => { + expect(err).to.not.exist() + nodeWS = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }), + + (cb) => { + const wstar = new WStar({}) + + createNode([ + '/ip4/127.0.0.1/tcp/24642/ws/p2p-websocket-star' + ], { + modules: { + transport: [wstar], + discovery: [wstar.discovery] + } + }, (err, node) => { + expect(err).to.not.exist() + nodeWStar = node + wstar.lazySetId(node.peerInfo.id) + node.handle('/echo/1.0.0', echo) + node.start(cb) + }) + } + ], done) + }) + + after((done) => { + parallel([ + (cb) => nodeAll.stop(cb), + (cb) => nodeTCP.stop(cb), + (cb) => nodeWS.stop(cb), + (cb) => nodeWStar.stop(cb), + (cb) => ss.stop(done) + ], done) + }) + + it('nodeAll.dial nodeTCP using PeerInfo', (done) => { + nodeAll.dial(nodeTCP.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeAll.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) + cb() + }, + (cb) => { + const peers = nodeTCP.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(1) + cb() + } + ], done) + } + }) + }) + + it('nodeAll.hangUp nodeTCP using PeerInfo', (done) => { + nodeAll.hangUp(nodeTCP.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeAll.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(0) + cb() + }, + (cb) => { + const peers = nodeTCP.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(0) + cb() + } + ], done) + } + }) + }) + + it('nodeAll.dial nodeWS using PeerInfo', (done) => { + nodeAll.dial(nodeWS.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeAll.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(2) + expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) + cb() + }, + (cb) => { + const peers = nodeWS.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(1) + cb() + } + ], done) + } + }) + }) + + it('nodeAll.hangUp nodeWS using PeerInfo', (done) => { + nodeAll.hangUp(nodeWS.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeAll.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(2) + expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(0) + cb() + }, + (cb) => { + const peers = nodeWS.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(0) + cb() + } + ], done) + } + }) + }) + + it('nodeAll.dial nodeWStar using PeerInfo', (done) => { + nodeAll.dial(nodeWStar.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeAll.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(3) + expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) + cb() + }, + (cb) => { + const peers = nodeWStar.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) + cb() + } + ], done) + } + }) + }) + + it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => { + nodeAll.hangUp(nodeWStar.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeAll.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(3) + expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(0) + cb() + }, + (cb) => { + const peers = nodeWStar.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeWStar.swarm.muxedConns)).to.have.length(0) + cb() + } + ], done) + } + }) + }) +}) From 0e2793f257c36b4946d50bfd5ef4bc563ef01ff8 Mon Sep 17 00:00:00 2001 From: mkg20001 Date: Sat, 28 Oct 2017 14:59:10 +0200 Subject: [PATCH 2/7] refactor: websocket-star-{signal => rendezvous} --- .aegir.js | 4 ++-- test/nodejs-bundle/tcp+websockets+websocket-star.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.aegir.js b/.aegir.js index d70888eeae..fb20291ea7 100644 --- a/.aegir.js +++ b/.aegir.js @@ -6,7 +6,7 @@ const PeerId = require('peer-id') const pull = require('pull-stream') const sigServer = require('libp2p-webrtc-star/src/sig-server') -const WSsigServer = require('libp2p-websocket-star-rendezvous') +const wsRendezvous = require('libp2p-websocket-star-rendezvous') let server let server2 @@ -27,7 +27,7 @@ const before = (done) => { ready() }) - WSsigServer.start({ + wsRendezvous.start({ port: 14444, refreshPeerListIntervalMS: 1000, strictMultiaddr: false diff --git a/test/nodejs-bundle/tcp+websockets+websocket-star.js b/test/nodejs-bundle/tcp+websockets+websocket-star.js index 4cc322f567..842313b9bb 100644 --- a/test/nodejs-bundle/tcp+websockets+websocket-star.js +++ b/test/nodejs-bundle/tcp+websockets+websocket-star.js @@ -5,7 +5,7 @@ const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect const parallel = require('async/parallel') -const signalling = require('libp2p-websocket-star-signal') +const rendezvous = require('libp2p-websocket-star-rendezvous') const WStar = require('libp2p-websocket-star') const utils = require('./utils') const createNode = utils.createNode @@ -22,7 +22,7 @@ describe('TCP + WebSockets + WebSocketStar', () => { before((done) => { parallel([ (cb) => { - signalling.start({ port: 24642 }, (err, server) => { + rendezvous.start({ port: 24642 }, (err, server) => { expect(err).to.not.exist() ss = server cb() From daf5b6918f408dd451e6f9979f9fc560bff68beb Mon Sep 17 00:00:00 2001 From: mkg20001 Date: Tue, 31 Oct 2017 15:08:04 +0100 Subject: [PATCH 3/7] misc: Apply requested changes --- .aegir.js | 3 +- .../tcp+websockets+webrtc-star.js | 125 +++--------------- .../tcp+websockets+websocket-star.js | 125 +++--------------- 3 files changed, 34 insertions(+), 219 deletions(-) diff --git a/.aegir.js b/.aegir.js index fb20291ea7..59fb65a8ab 100644 --- a/.aegir.js +++ b/.aegir.js @@ -4,6 +4,7 @@ const Node = require('./test/nodejs-bundle/nodejs-bundle.js') const PeerInfo = require('peer-info') const PeerId = require('peer-id') const pull = require('pull-stream') +const {parallel} = require('async') const sigServer = require('libp2p-webrtc-star/src/sig-server') const wsRendezvous = require('libp2p-websocket-star-rendezvous') @@ -54,7 +55,7 @@ const before = (done) => { } const after = (done) => { - setTimeout(() => require('async/each')([node, server, server2], (s, n) => s.stop(n), done), 2000) + setTimeout(() => parallel([node, server, server2].map(s => cb => s.stop(cb)), done), 2000) } module.exports = { diff --git a/test/nodejs-bundle/tcp+websockets+webrtc-star.js b/test/nodejs-bundle/tcp+websockets+webrtc-star.js index e4b1e183f5..b0943613ec 100644 --- a/test/nodejs-bundle/tcp+websockets+webrtc-star.js +++ b/test/nodejs-bundle/tcp+websockets+webrtc-star.js @@ -91,57 +91,32 @@ describe('TCP + WebSockets + WebRTCStar', () => { (cb) => nodeTCP.stop(cb), (cb) => nodeWS.stop(cb), (cb) => nodeWStar.stop(cb), - (cb) => ss.stop(done) + (cb) => ss.stop(cb) ], done) }) + function check (otherNode, done, muxed, peers) { + let i = 1; + [nodeAll, otherNode].forEach((node) => { + expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1) + expect(Object.keys(node.swarm.muxedConns)).to.have.length(muxed) + }) + done() + } + it('nodeAll.dial nodeTCP using PeerInfo', (done) => { nodeAll.dial(nodeTCP.peerInfo, (err) => { expect(err).to.not.exist() // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeTCP.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(1) - cb() - } - ], done) - } + setTimeout(check, 500, nodeTCP, done, 1, 1) }) }) it('nodeAll.hangUp nodeTCP using PeerInfo', (done) => { nodeAll.hangUp(nodeTCP.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(0) - cb() - }, - (cb) => { - const peers = nodeTCP.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } + setTimeout(check, 500, nodeTCP, done, 0, 1) }) }) @@ -150,48 +125,14 @@ describe('TCP + WebSockets + WebRTCStar', () => { expect(err).to.not.exist() // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(2) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(1) - cb() - } - ], done) - } + setTimeout(check, 500, nodeWS, done, 1, 2) }) }) it('nodeAll.hangUp nodeWS using PeerInfo', (done) => { nodeAll.hangUp(nodeWS.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(2) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(0) - cb() - }, - (cb) => { - const peers = nodeWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } + setTimeout(check, 500, nodeWS, done, 0, 2) }) }) @@ -201,48 +142,14 @@ describe('TCP + WebSockets + WebRTCStar', () => { expect(err).to.not.exist() // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(3) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeWStar.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) - cb() - } - ], done) - } + setTimeout(check, 500, nodeWStar, done, 1, 3) }) }) it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => { nodeAll.hangUp(nodeWStar.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(3) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(0) - cb() - }, - (cb) => { - const peers = nodeWStar.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeWStar.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } + setTimeout(check, 500, nodeWStar, done, 0, 3) }) }) }) diff --git a/test/nodejs-bundle/tcp+websockets+websocket-star.js b/test/nodejs-bundle/tcp+websockets+websocket-star.js index 842313b9bb..c2ab00243d 100644 --- a/test/nodejs-bundle/tcp+websockets+websocket-star.js +++ b/test/nodejs-bundle/tcp+websockets+websocket-star.js @@ -91,57 +91,32 @@ describe('TCP + WebSockets + WebSocketStar', () => { (cb) => nodeTCP.stop(cb), (cb) => nodeWS.stop(cb), (cb) => nodeWStar.stop(cb), - (cb) => ss.stop(done) + (cb) => ss.stop(cb) ], done) }) + function check (otherNode, done, muxed, peers) { + let i = 1; + [nodeAll, otherNode].forEach((node) => { + expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1) + expect(Object.keys(node.swarm.muxedConns)).to.have.length(muxed) + }) + done() + } + it('nodeAll.dial nodeTCP using PeerInfo', (done) => { nodeAll.dial(nodeTCP.peerInfo, (err) => { expect(err).to.not.exist() // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeTCP.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(1) - cb() - } - ], done) - } + setTimeout(check, 500, nodeTCP, done, 1, 1) }) }) it('nodeAll.hangUp nodeTCP using PeerInfo', (done) => { nodeAll.hangUp(nodeTCP.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(0) - cb() - }, - (cb) => { - const peers = nodeTCP.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } + setTimeout(check, 500, nodeTCP, done, 0, 1) }) }) @@ -150,48 +125,14 @@ describe('TCP + WebSockets + WebSocketStar', () => { expect(err).to.not.exist() // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(2) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(1) - cb() - } - ], done) - } + setTimeout(check, 500, nodeWS, done, 1, 2) }) }) it('nodeAll.hangUp nodeWS using PeerInfo', (done) => { nodeAll.hangUp(nodeWS.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(2) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(0) - cb() - }, - (cb) => { - const peers = nodeWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } + setTimeout(check, 500, nodeWS, done, 0, 2) }) }) @@ -200,48 +141,14 @@ describe('TCP + WebSockets + WebSocketStar', () => { expect(err).to.not.exist() // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(3) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeWStar.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(1) - cb() - } - ], done) - } + setTimeout(check, 500, nodeWStar, done, 1, 3) }) }) it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => { nodeAll.hangUp(nodeWStar.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeAll.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(3) - expect(Object.keys(nodeAll.swarm.muxedConns)).to.have.length(0) - cb() - }, - (cb) => { - const peers = nodeWStar.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeWStar.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } + setTimeout(check, 500, nodeWStar, done, 0, 3) }) }) }) From fe776a51491298cbdd258f8b14ecda202b52014a Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 27 Nov 2017 07:36:00 +0000 Subject: [PATCH 4/7] chore: update deps --- package.json | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 42f10852b9..9f44100833 100644 --- a/package.json +++ b/package.json @@ -37,23 +37,23 @@ }, "homepage": "https://github.com/libp2p/js-libp2p", "dependencies": { - "async": "^2.5.0", + "async": "^2.6.0", "libp2p-ping": "~0.6.0", - "libp2p-swarm": "~0.33.0", + "libp2p-swarm": "~0.33.2", "mafmt": "^3.0.2", "multiaddr": "^3.0.1", "peer-book": "~0.5.1", "peer-id": "~0.10.2", - "peer-info": "~0.11.0" + "peer-info": "~0.11.1" }, "devDependencies": { - "aegir": "^12.1.1", + "aegir": "^12.2.0", "chai": "^4.1.2", "cids": "~0.5.2", "dirty-chai": "^2.0.1", "electron-webrtc": "~0.3.0", - "libp2p-circuit": "~0.1.3", - "libp2p-kad-dht": "~0.5.1", + "libp2p-circuit": "~0.1.4", + "libp2p-kad-dht": "~0.6.0", "libp2p-mdns": "~0.9.1", "libp2p-multiplex": "~0.5.0", "libp2p-railing": "~0.7.1", @@ -62,16 +62,16 @@ "libp2p-tcp": "~0.11.1", "libp2p-webrtc-star": "~0.13.2", "libp2p-websockets": "~0.10.4", - "libp2p-websocket-star": "~0.5.0", - "libp2p-websocket-star-rendezvous": "~0.2.0", + "libp2p-websocket-star": "~0.5.1", + "libp2p-websocket-star-rendezvous": "~0.2.1", "lodash.times": "^4.3.2", "pre-commit": "^1.2.2", "pull-goodbye": "0.0.2", "pull-serializer": "~0.3.2", "pull-stream": "^3.6.1", "safe-buffer": "^5.1.1", - "sinon": "^4.0.2", - "wrtc": "0.0.62" + "sinon": "^4.1.2", + "wrtc": "0.0.63" }, "contributors": [ "Chris Bratlien ", From 47fdbddfd30fe8a05a34f17c1f3fd47a2ab23d2a Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 27 Nov 2017 08:27:09 +0000 Subject: [PATCH 5/7] refactor: considerable amount of refactor, clean up test structure --- .aegir.js | 86 +-- package.json | 12 +- test/browser-bundle/webrtc-star-only.js | 121 ---- test/browser-bundle/websocket-star-only.js | 121 ---- test/browser-bundle/websockets-only.js | 201 ------ test/browser.js | 6 +- .../circuit.js => circuit-relay.node.js} | 50 +- ...ent-routing.js => content-routing.node.js} | 6 +- .../test-data/test-id.json | 0 .../peer.json => fixtures/test-peer.json} | 0 test/node.js | 15 +- test/nodejs-bundle/spawn-libp2p-node.js | 30 - .../tcp+websockets+webrtc-star.js | 155 ---- .../tcp+websockets+websocket-star.js | 154 ---- test/nodejs-bundle/tcp+websockets.js | 167 ----- test/nodejs-bundle/tcp.js | 234 ------ .../discovery.js => peer-discovery.node.js} | 11 +- .../peer-routing.js => peer-routing.node.js} | 7 +- ...stream-muxing.js => stream-muxing.node.js} | 4 +- test/transports.browser.js | 424 +++++++++++ test/transports.node.js | 677 ++++++++++++++++++ .../turbolence.js => turbolence.node.js} | 3 +- .../bundle.browser.js} | 2 +- .../nodejs-bundle.js => utils/bundle.node.js} | 0 .../{nodejs-bundle/utils.js => utils/node.js} | 2 +- 25 files changed, 1203 insertions(+), 1285 deletions(-) delete mode 100644 test/browser-bundle/webrtc-star-only.js delete mode 100644 test/browser-bundle/websocket-star-only.js delete mode 100644 test/browser-bundle/websockets-only.js rename test/{nodejs-bundle/circuit.js => circuit-relay.node.js} (83%) rename test/{nodejs-bundle/content-routing.js => content-routing.node.js} (97%) rename test/{nodejs-bundle => fixtures}/test-data/test-id.json (100%) rename test/{browser-bundle/peer.json => fixtures/test-peer.json} (100%) delete mode 100755 test/nodejs-bundle/spawn-libp2p-node.js delete mode 100644 test/nodejs-bundle/tcp+websockets+webrtc-star.js delete mode 100644 test/nodejs-bundle/tcp+websockets+websocket-star.js delete mode 100644 test/nodejs-bundle/tcp+websockets.js delete mode 100644 test/nodejs-bundle/tcp.js rename test/{nodejs-bundle/discovery.js => peer-discovery.node.js} (93%) rename test/{nodejs-bundle/peer-routing.js => peer-routing.node.js} (97%) rename test/{nodejs-bundle/stream-muxing.js => stream-muxing.node.js} (98%) create mode 100644 test/transports.browser.js create mode 100644 test/transports.node.js rename test/{nodejs-bundle/turbolence.js => turbolence.node.js} (96%) rename test/{browser-bundle/browser-bundle.js => utils/bundle.browser.js} (96%) rename test/{nodejs-bundle/nodejs-bundle.js => utils/bundle.node.js} (100%) rename test/{nodejs-bundle/utils.js => utils/node.js} (95%) diff --git a/.aegir.js b/.aegir.js index 59fb65a8ab..a43dcb6471 100644 --- a/.aegir.js +++ b/.aegir.js @@ -1,61 +1,65 @@ 'use strict' -const Node = require('./test/nodejs-bundle/nodejs-bundle.js') const PeerInfo = require('peer-info') const PeerId = require('peer-id') const pull = require('pull-stream') -const {parallel} = require('async') +const parallel = require('async/parallel') +const rawPeer = require('./test/fixtures/test-peer.json') +const Node = require('./test/utils/bundle.node.js') const sigServer = require('libp2p-webrtc-star/src/sig-server') -const wsRendezvous = require('libp2p-websocket-star-rendezvous') -let server -let server2 +const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous') +let wrtcRendezvous +let wsRendezvous let node -const rawPeer = require('./test/browser-bundle/peer.json') const before = (done) => { - let count = 0 - const ready = () => ++count === 2 ? done() : null + parallel([ + (cb) => { + sigServer.start({ port: 15555 }, (err, server) => { + if (err) { return cb(err) } + wrtcRendezvous = server + cb() + }) + }, + (cb) => { + WebSocketStarRendezvous.start({ + port: 14444, + refreshPeerListIntervalMS: 1000, + strictMultiaddr: false, + disableCryptoChallenge: true + }, (err, _server) => { + if (err) { + return cb(err) + } + wsRendezvous = _server + cb() + }) + }, + (cb) => { + PeerId.createFromJSON(rawPeer, (err, peerId) => { + if (err) { + return done(err) + } + const peer = new PeerInfo(peerId) - sigServer.start({ - port: 15555 - }, (err, _server) => { - if (err) { - throw err - } - server = _server - ready() - }) - - wsRendezvous.start({ - port: 14444, - refreshPeerListIntervalMS: 1000, - strictMultiaddr: false - }, (err, _server) => { - if (err) { - throw err - } - server2 = _server - ready() - }) + peer.multiaddrs.add('/ip4/127.0.0.1/tcp/9200/ws') - PeerId.createFromJSON(rawPeer, (err, peerId) => { - if (err) { - return done(err) + node = new Node(peer) + node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) + node.start(cb) + }) } - const peer = new PeerInfo(peerId) - - peer.multiaddrs.add('/ip4/127.0.0.1/tcp/9200/ws') - - node = new Node(peer) - node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) - node.start(() => ready()) - }) + ], done) } const after = (done) => { - setTimeout(() => parallel([node, server, server2].map(s => cb => s.stop(cb)), done), 2000) + setTimeout(() => parallel( + [node, wrtcRendezvous, wsRendezvous].map((s) => { + return (cb) => s.stop(cb) + }) + , done), 2000) } module.exports = { diff --git a/package.json b/package.json index 9f44100833..1cdd51bfc1 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,12 @@ "scripts": { "lint": "aegir lint", "build": "aegir build", - "test": "aegir test --target node --target browser --no-parallel", - "test:node": "aegir test --target node --no-parallel", - "test:browser": "aegir test --target browser --no-parallel", - "release": "aegir test release --target node --target browser --no-parallel", - "release-minor": "aegir release --type minor --target node --target browser --no-parallel", - "release-major": "aegir release --type major --target node --target browser --no-parallel", + "test": "aegir test -t node -t browser", + "test:node": "aegir test -t node", + "test:browser": "aegir test -t browser", + "release": "aegir release -t node -t browser", + "release-minor": "aegir release --type minor -t node -t browser", + "release-major": "aegir release --type major -t node -t browser", "coverage": "aegir coverage", "coverage-publish": "aegir coverage --provider coveralls" }, diff --git a/test/browser-bundle/webrtc-star-only.js b/test/browser-bundle/webrtc-star-only.js deleted file mode 100644 index 6697507f03..0000000000 --- a/test/browser-bundle/webrtc-star-only.js +++ /dev/null @@ -1,121 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect -const PeerInfo = require('peer-info') -const PeerId = require('peer-id') -const parallel = require('async/parallel') -const pull = require('pull-stream') - -const Node = require('./browser-bundle') - -describe('libp2p-ipfs-browser (webrtc only)', () => { - let peer1 - let peer2 - let node1 - let node2 - - it('create two peerInfo with webrtc-star addrs', (done) => { - parallel([ - (cb) => PeerId.create({ bits: 1024 }, cb), - (cb) => PeerId.create({ bits: 1024 }, cb) - ], (err, ids) => { - expect(err).to.not.exist() - - peer1 = new PeerInfo(ids[0]) - const ma1 = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/ipfs/' + ids[0].toB58String() - peer1.multiaddrs.add(ma1) - - peer2 = new PeerInfo(ids[1]) - const ma2 = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/ipfs/' + ids[1].toB58String() - peer2.multiaddrs.add(ma2) - - done() - }) - }) - - it('create two libp2p nodes with those peers', (done) => { - node1 = new Node(peer1, null, { webRTCStar: true }) - node2 = new Node(peer2, null, { webRTCStar: true }) - done() - }) - - it('listen on the two libp2p nodes', (done) => { - parallel([ - (cb) => node1.start(cb), - (cb) => node2.start(cb) - ], done) - }) - - it('handle a protocol on the first node', () => { - node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) - }) - - it('dial from the second node to the first node', (done) => { - node1.dial(peer2, '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - const text = 'hello' - const peers1 = node1.peerBook.getAll() - expect(Object.keys(peers1)).to.have.length(1) - - const peers2 = node2.peerBook.getAll() - expect(Object.keys(peers2)).to.have.length(1) - - pull( - pull.values([Buffer.from(text)]), - conn, - pull.collect((err, data) => { - expect(err).to.not.exist() - expect(data[0].toString()).to.equal(text) - done() - }) - ) - } - }) - }) - - it('node1 hangUp node2', (done) => { - node1.hangUp(peer2, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - const peers = node1.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0) - done() - } - }) - }) - - it('create a third node and check that discovery works', (done) => { - let counter = 0 - - function check () { - if (++counter === 3) { - expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1) - expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1) - done() - } - } - - PeerId.create((err, id3) => { - expect(err).to.not.exist() - - const peer3 = new PeerInfo(id3) - const ma3 = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/ipfs/' + id3.toB58String() - peer3.multiaddrs.add(ma3) - - node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check)) - node2.on('peer:discovery', (peerInfo) => node2.dial(peerInfo, check)) - - const node3 = new Node(peer3, null, { webRTCStar: true }) - node3.start(check) - }) - }) -}) diff --git a/test/browser-bundle/websocket-star-only.js b/test/browser-bundle/websocket-star-only.js deleted file mode 100644 index 950fe49173..0000000000 --- a/test/browser-bundle/websocket-star-only.js +++ /dev/null @@ -1,121 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect -const PeerInfo = require('peer-info') -const PeerId = require('peer-id') -const parallel = require('async/parallel') -const pull = require('pull-stream') - -const Node = require('./browser-bundle') - -describe('libp2p-ipfs-browser (websocket-star only)', () => { - let peer1 - let peer2 - let node1 - let node2 - - it('create two peerInfo with websocket-star addrs', (done) => { - parallel([ - (cb) => PeerId.create({ bits: 1024 }, cb), - (cb) => PeerId.create({ bits: 1024 }, cb) - ], (err, ids) => { - expect(err).to.not.exist() - - peer1 = new PeerInfo(ids[0]) - const ma1 = '/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/' - peer1.multiaddrs.add(ma1) - - peer2 = new PeerInfo(ids[1]) - const ma2 = '/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/' - peer2.multiaddrs.add(ma2) - - done() - }) - }) - - it('create two libp2p nodes with those peers', (done) => { - node1 = new Node(peer1, null, { wsStar: true }) - node2 = new Node(peer2, null, { wsStar: true }) - done() - }) - - it('listen on the two libp2p nodes', (done) => { - parallel([ - (cb) => node1.start(cb), - (cb) => node2.start(cb) - ], done) - }) - - it('handle a protocol on the first node', () => { - node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) - }) - - it('dial from the second node to the first node', (done) => { - node1.dial(peer2, '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - const text = 'hello' - const peers1 = node1.peerBook.getAll() - expect(Object.keys(peers1)).to.have.length(1) - - const peers2 = node2.peerBook.getAll() - expect(Object.keys(peers2)).to.have.length(1) - - pull( - pull.values([Buffer.from(text)]), - conn, - pull.collect((err, data) => { - expect(err).to.not.exist() - expect(data[0].toString()).to.equal(text) - done() - }) - ) - } - }) - }) - - it('node1 hangUp node2', (done) => { - node1.hangUp(peer2, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - const peers = node1.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0) - done() - } - }) - }) - - it('create a third node and check that discovery works', (done) => { - let counter = 0 - - function check () { - if (++counter === 3) { - expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1) - expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1) - done() - } - } - - PeerId.create((err, id3) => { - expect(err).to.not.exist() - - const peer3 = new PeerInfo(id3) - const ma3 = '/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/ipfs/' + id3.toB58String() - peer3.multiaddrs.add(ma3) - - node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check)) - node2.on('peer:discovery', (peerInfo) => node2.dial(peerInfo, check)) - - const node3 = new Node(peer3, null, { wsStar: true }) - node3.start(check) - }) - }) -}) diff --git a/test/browser-bundle/websockets-only.js b/test/browser-bundle/websockets-only.js deleted file mode 100644 index 34b7ea3aaa..0000000000 --- a/test/browser-bundle/websockets-only.js +++ /dev/null @@ -1,201 +0,0 @@ -/* eslint max-nested-callbacks: ["error", 8] */ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect -const PeerInfo = require('peer-info') -const PeerId = require('peer-id') -const pull = require('pull-stream') -const goodbye = require('pull-goodbye') -const serializer = require('pull-serializer') -const Buffer = require('safe-buffer').Buffer - -const Node = require('./browser-bundle') -const rawPeer = require('./peer.json') - -describe('libp2p-ipfs-browser (websockets only)', () => { - let peerB - let nodeA - - before((done) => { - const ma = '/ip4/127.0.0.1/tcp/9200/ws/ipfs/' + rawPeer.id - - PeerId.createFromPrivKey(rawPeer.privKey, (err, id) => { - if (err) { - return done(err) - } - - peerB = new PeerInfo(id) - peerB.multiaddrs.add(ma) - done() - }) - }) - - after((done) => nodeA.stop(done)) - - it('create libp2pNode', (done) => { - PeerInfo.create((err, peerInfo) => { - expect(err).to.not.exist() - peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') - - nodeA = new Node(peerInfo) - done() - }) - }) - - it('create libp2pNode with multiplex only', (done) => { - PeerInfo.create((err, peerInfo) => { - expect(err).to.not.exist() - - const b = new Node(peerInfo, null, { muxer: ['multiplex'] }) - expect(b.modules.connection.muxer).to.eql([require('libp2p-multiplex')]) - done() - }) - }) - - it('start libp2pNode', (done) => { - nodeA.start(done) - }) - - // General connectivity tests - - it('libp2p.dial using Multiaddr nodeA to nodeB', (done) => { - nodeA.dial(peerB.multiaddrs.toArray()[0], (err) => { - expect(err).to.not.exist() - - setTimeout(check, 500) // Some time for Identify to finish - - function check () { - const peers = nodeA.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - done() - } - }) - }) - - it('libp2p.dial using Multiaddr on Protocol nodeA to nodeB', (done) => { - nodeA.dial(peerB.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - - const peers = nodeA.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - pull( - pull.values([Buffer.from('hey')]), - conn, - pull.collect((err, data) => { - expect(err).to.not.exist() - expect(data).to.eql([Buffer.from('hey')]) - done() - }) - ) - }) - }) - - it('libp2p.hangUp using Multiaddr nodeA to nodeB', (done) => { - nodeA.hangUp(peerB.multiaddrs.toArray()[0], (err) => { - expect(err).to.not.exist() - - setTimeout(check, 500) - - function check () { - const peers = nodeA.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) - done() - } - }) - }) - - it('libp2p.dial using PeerInfo nodeA to nodeB', (done) => { - nodeA.dial(peerB, (err) => { - expect(err).to.not.exist() - - setTimeout(check, 500) // Some time for Identify to finish - - function check () { - const peers = nodeA.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - done() - } - }) - }) - - it('libp2p.dial using PeerInfo on Protocol nodeA to nodeB', (done) => { - nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - const peers = nodeA.peerBook.getAll() - expect(err).to.not.exist() - expect(Object.keys(peers)).to.have.length(1) - - pull( - pull.values([Buffer.from('hey')]), - conn, - pull.collect((err, data) => { - expect(err).to.not.exist() - expect(data).to.eql([Buffer.from('hey')]) - done() - }) - ) - }) - }) - - it('libp2p.hangUp using PeerInfo nodeA to nodeB', (done) => { - nodeA.hangUp(peerB, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - const peers = nodeA.peerBook.getAll() - expect(err).to.not.exist() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) - done() - } - }) - }) - - describe('stress', () => { - it('one big write', (done) => { - nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - const rawMessage = Buffer.alloc(100000) - rawMessage.fill('a') - - const s = serializer(goodbye({ - source: pull.values([rawMessage]), - sink: pull.collect((err, results) => { - expect(err).to.not.exist() - expect(results).to.have.length(1) - expect(Buffer.from(results[0])).to.have.length(rawMessage.length) - done() - }) - })) - pull(s, conn, s) - }) - }) - - it('many writes', (done) => { - nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - - const s = serializer(goodbye({ - source: pull( - pull.infinite(), - pull.take(1000), - pull.map((val) => Buffer.from(val.toString())) - ), - sink: pull.collect((err, result) => { - expect(err).to.not.exist() - expect(result).to.have.length(1000) - done() - }) - })) - - pull(s, conn, s) - }) - }) - }) -}) diff --git a/test/browser.js b/test/browser.js index 7f4fd5221b..41357c9e57 100644 --- a/test/browser.js +++ b/test/browser.js @@ -1,8 +1,4 @@ 'use strict' -const w = require('webrtcsupport') - require('./base') -require('./browser-bundle/websockets-only') -if (w.support) { require('./browser-bundle/webrtc-star-only') } -require('./browser-bundle/websocket-star-only') +require('./transports.browser') diff --git a/test/nodejs-bundle/circuit.js b/test/circuit-relay.node.js similarity index 83% rename from test/nodejs-bundle/circuit.js rename to test/circuit-relay.node.js index c90ed91f36..09e0af530c 100644 --- a/test/nodejs-bundle/circuit.js +++ b/test/circuit-relay.node.js @@ -5,7 +5,7 @@ const pull = require('pull-stream') const waterfall = require('async/waterfall') const series = require('async/series') const parallel = require('async/parallel') -const utils = require('./utils') +const utils = require('./utils/node') const Circuit = require('libp2p-circuit') const multiaddr = require('multiaddr') @@ -15,7 +15,7 @@ chai.use(require('dirty-chai')) const expect = chai.expect const sinon = require('sinon') -describe(`circuit`, function () { +describe('circuit relay', function () { let handlerSpies = [] let relayNode1 let relayNode2 @@ -51,8 +51,8 @@ describe(`circuit`, function () { waterfall([ // set up passive relay (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/0/ws`, - `/ip4/0.0.0.0/tcp/0` + '/ip4/0.0.0.0/tcp/0/ws', + '/ip4/0.0.0.0/tcp/0' ], { relay: { enabled: true, @@ -67,8 +67,8 @@ describe(`circuit`, function () { }), // setup active relay (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/0/ws`, - `/ip4/0.0.0.0/tcp/0` + '/ip4/0.0.0.0/tcp/0/ws', + '/ip4/0.0.0.0/tcp/0' ], { relay: { enabled: true, @@ -83,7 +83,7 @@ describe(`circuit`, function () { }), // setup node with WS (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/0/ws` + '/ip4/0.0.0.0/tcp/0/ws' ], { relay: { enabled: true @@ -94,7 +94,7 @@ describe(`circuit`, function () { }), // setup node with WS (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/0/ws` + '/ip4/0.0.0.0/tcp/0/ws' ], { relay: { enabled: true @@ -105,7 +105,7 @@ describe(`circuit`, function () { }), // set up node with TCP and listening on relay1 (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/0`, + '/ip4/0.0.0.0/tcp/0', `/ipfs/${relayNode1.peerInfo.id.toB58String()}/p2p-circuit` ], { relay: { @@ -117,7 +117,7 @@ describe(`circuit`, function () { }), // set up node with TCP and listening on relay2 over TCP transport (cb) => setupNode([ - `/ip4/0.0.0.0/tcp/0`, + '/ip4/0.0.0.0/tcp/0', `/ip4/0.0.0.0/tcp/0/ipfs/${relayNode2.peerInfo.id.toB58String()}/p2p-circuit` ], { relay: { @@ -150,9 +150,10 @@ describe(`circuit`, function () { ], done) }) - describe(`any relay`, function () { - this.timeout(20000) - it('should dial from WS1 to TCP1 over any R', function (done) { + describe('any relay', function () { + this.timeout(20 * 1000) + + it('should dial from WS1 to TCP1 over any R', (done) => { nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() expect(conn).to.exist() @@ -160,8 +161,8 @@ describe(`circuit`, function () { pull( pull.values(['hello']), conn, - pull.collect((e, result) => { - expect(e).to.not.exist() + pull.collect((err, result) => { + expect(err).to.not.exist() expect(result[0].toString()).to.equal('hello') done() }) @@ -169,7 +170,7 @@ describe(`circuit`, function () { }) }) - it(`should not dial - no R from WS2 to TCP1`, function (done) { + it('should not dial - no R from WS2 to TCP1', (done) => { nodeWS2.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.exist() expect(conn).to.not.exist() @@ -178,9 +179,10 @@ describe(`circuit`, function () { }) }) - describe(`explicit relay`, function () { - this.timeout(20000) - it('should dial from WS1 to TCP1 over R1', function (done) { + describe('explicit relay', function () { + this.timeout(20 * 1000) + + it('should dial from WS1 to TCP1 over R1', (done) => { nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() expect(conn).to.exist() @@ -188,8 +190,8 @@ describe(`circuit`, function () { pull( pull.values(['hello']), conn, - pull.collect((e, result) => { - expect(e).to.not.exist() + pull.collect((err, result) => { + expect(err).to.not.exist() expect(result[0].toString()).to.equal('hello') const addr = multiaddr(handlerSpies[0].args[2][0].dstPeer.addrs[0]).toString() @@ -200,7 +202,7 @@ describe(`circuit`, function () { }) }) - it(`should dial from WS1 to TCP2 over R2`, function (done) { + it('should dial from WS1 to TCP2 over R2', (done) => { nodeWS1.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() expect(conn).to.exist() @@ -208,8 +210,8 @@ describe(`circuit`, function () { pull( pull.values(['hello']), conn, - pull.collect((e, result) => { - expect(e).to.not.exist() + pull.collect((err, result) => { + expect(err).to.not.exist() expect(result[0].toString()).to.equal('hello') const addr = multiaddr(handlerSpies[1].args[2][0].dstPeer.addrs[0]).toString() diff --git a/test/nodejs-bundle/content-routing.js b/test/content-routing.node.js similarity index 97% rename from test/nodejs-bundle/content-routing.js rename to test/content-routing.node.js index bc47148e50..389dc0e59f 100644 --- a/test/nodejs-bundle/content-routing.js +++ b/test/content-routing.node.js @@ -7,10 +7,10 @@ const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect const parallel = require('async/parallel') -const utils = require('./utils') -const createNode = utils.createNode const _times = require('lodash.times') const CID = require('cids') +const utils = require('./utils/node') +const createNode = utils.createNode describe('.contentRouting', () => { let nodeA @@ -20,7 +20,7 @@ describe('.contentRouting', () => { let nodeE before(function (done) { - this.timeout(5000) + this.timeout(5 * 1000) const tasks = _times(5, () => (cb) => { createNode('/ip4/0.0.0.0/tcp/0', { mdns: false, diff --git a/test/nodejs-bundle/test-data/test-id.json b/test/fixtures/test-data/test-id.json similarity index 100% rename from test/nodejs-bundle/test-data/test-id.json rename to test/fixtures/test-data/test-id.json diff --git a/test/browser-bundle/peer.json b/test/fixtures/test-peer.json similarity index 100% rename from test/browser-bundle/peer.json rename to test/fixtures/test-peer.json diff --git a/test/node.js b/test/node.js index 02164ac201..6bde025de1 100644 --- a/test/node.js +++ b/test/node.js @@ -1,12 +1,9 @@ 'use strict' require('./base') -require('./nodejs-bundle/tcp') -require('./nodejs-bundle/tcp+websockets') -require('./nodejs-bundle/tcp+websockets+webrtc-star') -require('./nodejs-bundle/tcp+websockets+websocket-star') -require('./nodejs-bundle/stream-muxing') -require('./nodejs-bundle/discovery') -require('./nodejs-bundle/peer-routing') -require('./nodejs-bundle/content-routing') -require('./nodejs-bundle/circuit') +require('./transports.node') +require('./stream-muxing.node') +require('./peer-discovery.node') +require('./peer-routing.node') +require('./content-routing.node') +require('./circuit-relay.node') diff --git a/test/nodejs-bundle/spawn-libp2p-node.js b/test/nodejs-bundle/spawn-libp2p-node.js deleted file mode 100755 index cd412074cb..0000000000 --- a/test/nodejs-bundle/spawn-libp2p-node.js +++ /dev/null @@ -1,30 +0,0 @@ -#! /usr/bin/env node - -'use strict' - -const Node = require('./nodejs-bundle') -const PeerInfo = require('peer-info') -const PeerId = require('peer-id') -const pull = require('pull-stream') - -const idBak = require('./test-data/test-id.json') - -PeerId.createFromJSON(idBak, (err, peerId) => { - if (err) { - throw err - } - - const peerInfo = new PeerInfo(peerId) - - peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/12345') - - const node = new Node(peerInfo) - - node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) - - node.start((err) => { - if (err) { throw err } - - console.log('Spawned node started, env:', process.env.LIBP2P_MUXER) - }) -}) diff --git a/test/nodejs-bundle/tcp+websockets+webrtc-star.js b/test/nodejs-bundle/tcp+websockets+webrtc-star.js deleted file mode 100644 index b0943613ec..0000000000 --- a/test/nodejs-bundle/tcp+websockets+webrtc-star.js +++ /dev/null @@ -1,155 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect -const parallel = require('async/parallel') -const signalling = require('libp2p-webrtc-star/src/sig-server') -const WStar = require('libp2p-webrtc-star') -const wrtc = require('wrtc') -const utils = require('./utils') -const createNode = utils.createNode -const echo = utils.echo - -describe('TCP + WebSockets + WebRTCStar', () => { - let nodeAll - let nodeTCP - let nodeWS - let nodeWStar - - let ss - - before(function (done) { - this.timeout(5000) - parallel([ - (cb) => { - signalling.start({ port: 24642 }, (err, server) => { - expect(err).to.not.exist() - ss = server - cb() - }) - }, - (cb) => { - const wstar = new WStar({wrtc: wrtc}) - createNode([ - '/ip4/0.0.0.0/tcp/0', - '/ip4/127.0.0.1/tcp/25011/ws', - '/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star' - ], { - modules: { - transport: [wstar], - discovery: [wstar.discovery] - } - }, (err, node) => { - expect(err).to.not.exist() - nodeAll = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }) - }, - (cb) => createNode([ - '/ip4/0.0.0.0/tcp/0' - ], (err, node) => { - expect(err).to.not.exist() - nodeTCP = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }), - (cb) => createNode([ - '/ip4/127.0.0.1/tcp/25022/ws' - ], (err, node) => { - expect(err).to.not.exist() - nodeWS = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }), - - (cb) => { - const wstar = new WStar({wrtc: wrtc}) - - createNode([ - '/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star' - ], { - modules: { - transport: [wstar], - discovery: [wstar.discovery] - } - }, (err, node) => { - expect(err).to.not.exist() - nodeWStar = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }) - } - ], done) - }) - - after((done) => { - parallel([ - (cb) => nodeAll.stop(cb), - (cb) => nodeTCP.stop(cb), - (cb) => nodeWS.stop(cb), - (cb) => nodeWStar.stop(cb), - (cb) => ss.stop(cb) - ], done) - }) - - function check (otherNode, done, muxed, peers) { - let i = 1; - [nodeAll, otherNode].forEach((node) => { - expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1) - expect(Object.keys(node.swarm.muxedConns)).to.have.length(muxed) - }) - done() - } - - it('nodeAll.dial nodeTCP using PeerInfo', (done) => { - nodeAll.dial(nodeTCP.peerInfo, (err) => { - expect(err).to.not.exist() - - // Some time for Identify to finish - setTimeout(check, 500, nodeTCP, done, 1, 1) - }) - }) - - it('nodeAll.hangUp nodeTCP using PeerInfo', (done) => { - nodeAll.hangUp(nodeTCP.peerInfo, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500, nodeTCP, done, 0, 1) - }) - }) - - it('nodeAll.dial nodeWS using PeerInfo', (done) => { - nodeAll.dial(nodeWS.peerInfo, (err) => { - expect(err).to.not.exist() - - // Some time for Identify to finish - setTimeout(check, 500, nodeWS, done, 1, 2) - }) - }) - - it('nodeAll.hangUp nodeWS using PeerInfo', (done) => { - nodeAll.hangUp(nodeWS.peerInfo, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500, nodeWS, done, 0, 2) - }) - }) - - it('nodeAll.dial nodeWStar using PeerInfo', function (done) { - this.timeout(10000) - nodeAll.dial(nodeWStar.peerInfo, (err) => { - expect(err).to.not.exist() - - // Some time for Identify to finish - setTimeout(check, 500, nodeWStar, done, 1, 3) - }) - }) - - it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => { - nodeAll.hangUp(nodeWStar.peerInfo, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500, nodeWStar, done, 0, 3) - }) - }) -}) diff --git a/test/nodejs-bundle/tcp+websockets+websocket-star.js b/test/nodejs-bundle/tcp+websockets+websocket-star.js deleted file mode 100644 index c2ab00243d..0000000000 --- a/test/nodejs-bundle/tcp+websockets+websocket-star.js +++ /dev/null @@ -1,154 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect -const parallel = require('async/parallel') -const rendezvous = require('libp2p-websocket-star-rendezvous') -const WStar = require('libp2p-websocket-star') -const utils = require('./utils') -const createNode = utils.createNode -const echo = utils.echo - -describe('TCP + WebSockets + WebSocketStar', () => { - let nodeAll - let nodeTCP - let nodeWS - let nodeWStar - - let ss - - before((done) => { - parallel([ - (cb) => { - rendezvous.start({ port: 24642 }, (err, server) => { - expect(err).to.not.exist() - ss = server - cb() - }) - }, - (cb) => { - const wstar = new WStar({}) - createNode([ - '/ip4/0.0.0.0/tcp/0', - '/ip4/127.0.0.1/tcp/25011/ws', - '/ip4/127.0.0.1/tcp/24642/ws/p2p-websocket-star' - ], { - modules: { - transport: [wstar], - discovery: [wstar.discovery] - } - }, (err, node) => { - expect(err).to.not.exist() - nodeAll = node - wstar.lazySetId(node.peerInfo.id) - node.handle('/echo/1.0.0', echo) - node.start(cb) - }) - }, - (cb) => createNode([ - '/ip4/0.0.0.0/tcp/0' - ], (err, node) => { - expect(err).to.not.exist() - nodeTCP = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }), - (cb) => createNode([ - '/ip4/127.0.0.1/tcp/25022/ws' - ], (err, node) => { - expect(err).to.not.exist() - nodeWS = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }), - - (cb) => { - const wstar = new WStar({}) - - createNode([ - '/ip4/127.0.0.1/tcp/24642/ws/p2p-websocket-star' - ], { - modules: { - transport: [wstar], - discovery: [wstar.discovery] - } - }, (err, node) => { - expect(err).to.not.exist() - nodeWStar = node - wstar.lazySetId(node.peerInfo.id) - node.handle('/echo/1.0.0', echo) - node.start(cb) - }) - } - ], done) - }) - - after((done) => { - parallel([ - (cb) => nodeAll.stop(cb), - (cb) => nodeTCP.stop(cb), - (cb) => nodeWS.stop(cb), - (cb) => nodeWStar.stop(cb), - (cb) => ss.stop(cb) - ], done) - }) - - function check (otherNode, done, muxed, peers) { - let i = 1; - [nodeAll, otherNode].forEach((node) => { - expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1) - expect(Object.keys(node.swarm.muxedConns)).to.have.length(muxed) - }) - done() - } - - it('nodeAll.dial nodeTCP using PeerInfo', (done) => { - nodeAll.dial(nodeTCP.peerInfo, (err) => { - expect(err).to.not.exist() - - // Some time for Identify to finish - setTimeout(check, 500, nodeTCP, done, 1, 1) - }) - }) - - it('nodeAll.hangUp nodeTCP using PeerInfo', (done) => { - nodeAll.hangUp(nodeTCP.peerInfo, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500, nodeTCP, done, 0, 1) - }) - }) - - it('nodeAll.dial nodeWS using PeerInfo', (done) => { - nodeAll.dial(nodeWS.peerInfo, (err) => { - expect(err).to.not.exist() - - // Some time for Identify to finish - setTimeout(check, 500, nodeWS, done, 1, 2) - }) - }) - - it('nodeAll.hangUp nodeWS using PeerInfo', (done) => { - nodeAll.hangUp(nodeWS.peerInfo, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500, nodeWS, done, 0, 2) - }) - }) - - it('nodeAll.dial nodeWStar using PeerInfo', (done) => { - nodeAll.dial(nodeWStar.peerInfo, (err) => { - expect(err).to.not.exist() - - // Some time for Identify to finish - setTimeout(check, 500, nodeWStar, done, 1, 3) - }) - }) - - it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => { - nodeAll.hangUp(nodeWStar.peerInfo, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500, nodeWStar, done, 0, 3) - }) - }) -}) diff --git a/test/nodejs-bundle/tcp+websockets.js b/test/nodejs-bundle/tcp+websockets.js deleted file mode 100644 index 609eedea27..0000000000 --- a/test/nodejs-bundle/tcp+websockets.js +++ /dev/null @@ -1,167 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect -const parallel = require('async/parallel') -// const multiaddr = require('multiaddr') -// const pull = require('pull-stream') -const utils = require('./utils') -const createNode = utils.createNode -const echo = utils.echo - -describe('TCP + WebSockets', () => { - let nodeTCP - let nodeTCPnWS - let nodeWS - - before((done) => { - parallel([ - (cb) => createNode([ - '/ip4/0.0.0.0/tcp/0' - ], (err, node) => { - expect(err).to.not.exist() - nodeTCP = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }), - (cb) => createNode([ - '/ip4/0.0.0.0/tcp/0', - '/ip4/127.0.0.1/tcp/25011/ws' - ], (err, node) => { - expect(err).to.not.exist() - nodeTCPnWS = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }), - (cb) => createNode([ - '/ip4/127.0.0.1/tcp/25022/ws' - ], (err, node) => { - expect(err).to.not.exist() - nodeWS = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }) - ], done) - }) - - after((done) => { - parallel([ - (cb) => nodeTCP.stop(cb), - (cb) => nodeTCPnWS.stop(cb), - (cb) => nodeWS.stop(cb) - ], done) - }) - - it('nodeTCP.dial nodeTCPnWS using PeerInfo', (done) => { - nodeTCP.dial(nodeTCPnWS.peerInfo, (err) => { - expect(err).to.not.exist() - - // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeTCP.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeTCPnWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(1) - cb() - } - ], done) - } - }) - }) - - it('nodeTCP.hangUp nodeTCPnWS using PeerInfo', (done) => { - nodeTCP.hangUp(nodeTCPnWS.peerInfo, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeTCP.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(0) - - cb() - }, - (cb) => { - const peers = nodeTCPnWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } - }) - }) - - it('nodeTCPnWS.dial nodeWS using PeerInfo', (done) => { - nodeTCPnWS.dial(nodeWS.peerInfo, (err) => { - expect(err).to.not.exist() - - // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeTCPnWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(2) - expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(1) - cb() - } - ], done) - } - }) - }) - - it('nodeTCPnWS.hangUp nodeWS using PeerInfo', (done) => { - nodeTCPnWS.hangUp(nodeWS.peerInfo, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeTCPnWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(2) - expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(0) - - cb() - }, - (cb) => { - const peers = nodeWS.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } - }) - }) - - // Until https://github.com/libp2p/js-libp2p/issues/46 is resolved - // Everynode will be able to dial in WebSockets - it.skip('nodeTCP.dial nodeWS using PeerInfo is unsuccesful', (done) => { - nodeTCP.dial(nodeWS.peerInfo, (err) => { - expect(err).to.exist() - done() - }) - }) -}) diff --git a/test/nodejs-bundle/tcp.js b/test/nodejs-bundle/tcp.js deleted file mode 100644 index a8b4cb4aae..0000000000 --- a/test/nodejs-bundle/tcp.js +++ /dev/null @@ -1,234 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect -const parallel = require('async/parallel') -const series = require('async/series') -const pull = require('pull-stream') -const utils = require('./utils') -const createNode = utils.createNode -const echo = utils.echo - -describe('TCP only', () => { - let nodeA - let nodeB - - before((done) => { - parallel([ - (cb) => createNode('/ip4/0.0.0.0/tcp/0', (err, node) => { - expect(err).to.not.exist() - nodeA = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }), - (cb) => createNode('/ip4/0.0.0.0/tcp/0', (err, node) => { - expect(err).to.not.exist() - nodeB = node - node.handle('/echo/1.0.0', echo) - node.start(cb) - }) - ], done) - }) - - after((done) => { - parallel([ - (cb) => nodeA.stop(cb), - (cb) => nodeB.stop(cb) - ], done) - }) - - it('nodeA.dial nodeB using PeerInfo without proto (warmup)', (done) => { - nodeA.dial(nodeB.peerInfo, (err) => { - expect(err).to.not.exist() - - // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeA.peerBook.getAll() - expect(err).to.not.exist() - expect(Object.keys(peers)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeB.peerBook.getAll() - expect(err).to.not.exist() - expect(Object.keys(peers)).to.have.length(1) - cb() - } - ], done) - } - }) - }) - - it('nodeA.dial nodeB using PeerInfo', (done) => { - nodeA.dial(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => { - expect(err).to.not.exist() - - pull( - pull.values([Buffer.from('hey')]), - conn, - pull.collect((err, data) => { - expect(err).to.not.exist() - expect(data).to.be.eql([Buffer.from('hey')]) - done() - }) - ) - }) - }) - - it('nodeA.hangUp nodeB using PeerInfo (first)', (done) => { - nodeA.hangUp(nodeB.peerInfo, (err) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeA.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) - cb() - }, - (cb) => { - const peers = nodeB.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - expect(Object.keys(nodeB.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } - }) - }) - - it('nodeA.dial nodeB using multiaddr', (done) => { - nodeA.dial(nodeB.peerInfo.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => { - // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - expect(err).to.not.exist() - series([ - (cb) => { - const peers = nodeA.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeB.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1) - cb() - } - ], () => { - pull( - pull.values([Buffer.from('hey')]), - conn, - pull.collect((err, data) => { - expect(err).to.not.exist() - expect(data).to.be.eql([Buffer.from('hey')]) - done() - }) - ) - }) - } - }) - }) - - it('nodeA.hangUp nodeB using multiaddr (second)', (done) => { - nodeA.hangUp(nodeB.peerInfo.multiaddrs.toArray()[0], (err) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeA.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) - cb() - }, - (cb) => { - const peers = nodeB.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - expect(Object.keys(nodeB.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } - }) - }) - - it('nodeA.dial nodeB using PeerId', (done) => { - nodeA.dial(nodeB.peerInfo.id, '/echo/1.0.0', (err, conn) => { - // Some time for Identify to finish - setTimeout(check, 500) - - function check () { - expect(err).to.not.exist() - series([ - (cb) => { - const peers = nodeA.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1) - cb() - }, - (cb) => { - const peers = nodeB.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1) - cb() - } - ], () => { - pull( - pull.values([Buffer.from('hey')]), - conn, - pull.collect((err, data) => { - expect(err).to.not.exist() - expect(data).to.be.eql([Buffer.from('hey')]) - done() - }) - ) - }) - } - }) - }) - - it('nodeA.hangUp nodeB using PeerId (third)', (done) => { - nodeA.hangUp(nodeB.peerInfo.multiaddrs.toArray()[0], (err) => { - expect(err).to.not.exist() - setTimeout(check, 500) - - function check () { - parallel([ - (cb) => { - const peers = nodeA.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) - cb() - }, - (cb) => { - const peers = nodeB.peerBook.getAll() - expect(Object.keys(peers)).to.have.length(1) - - expect(Object.keys(nodeB.swarm.muxedConns)).to.have.length(0) - cb() - } - ], done) - } - }) - }) -}) diff --git a/test/nodejs-bundle/discovery.js b/test/peer-discovery.node.js similarity index 93% rename from test/nodejs-bundle/discovery.js rename to test/peer-discovery.node.js index bdaf5ad858..d82b4e4c95 100644 --- a/test/nodejs-bundle/discovery.js +++ b/test/peer-discovery.node.js @@ -6,11 +6,11 @@ chai.use(require('dirty-chai')) const expect = chai.expect const signalling = require('libp2p-webrtc-star/src/sig-server') const parallel = require('async/parallel') -const utils = require('./utils') +const utils = require('./utils/node') const createNode = utils.createNode const echo = utils.echo -describe('discovery', () => { +describe('peer discovery', () => { let nodeA let nodeB let port = 24642 @@ -61,7 +61,8 @@ describe('discovery', () => { setup({ mdns: true }) it('find a peer', function (done) { - this.timeout(15000) + this.timeout(15 * 1000) + nodeA.once('peer:discovery', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) .to.eql(peerInfo.id.toB58String()) @@ -75,7 +76,7 @@ describe('discovery', () => { setup({ webRTCStar: true }) it('find a peer', function (done) { - this.timeout(15000) + this.timeout(15 * 1000) nodeA.once('peer:discovery', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) .to.eql(peerInfo.id.toB58String()) @@ -91,7 +92,7 @@ describe('discovery', () => { }) it('find a peer', function (done) { - this.timeout(15000) + this.timeout(15 * 1000) nodeA.once('peer:discovery', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) .to.eql(peerInfo.id.toB58String()) diff --git a/test/nodejs-bundle/peer-routing.js b/test/peer-routing.node.js similarity index 97% rename from test/nodejs-bundle/peer-routing.js rename to test/peer-routing.node.js index fb2e5611bd..ec34eb7f24 100644 --- a/test/nodejs-bundle/peer-routing.js +++ b/test/peer-routing.node.js @@ -7,9 +7,9 @@ const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect const parallel = require('async/parallel') -const utils = require('./utils') -const createNode = utils.createNode const _times = require('lodash.times') +const utils = require('./utils/node') +const createNode = utils.createNode describe('.peerRouting', () => { let nodeA @@ -19,7 +19,8 @@ describe('.peerRouting', () => { let nodeE before(function (done) { - this.timeout(5000) + this.timeout(5 * 1000) + const tasks = _times(5, () => (cb) => { createNode('/ip4/0.0.0.0/tcp/0', { mdns: false, diff --git a/test/nodejs-bundle/stream-muxing.js b/test/stream-muxing.node.js similarity index 98% rename from test/nodejs-bundle/stream-muxing.js rename to test/stream-muxing.node.js index bf9eb5aeb4..1c00722d8c 100644 --- a/test/nodejs-bundle/stream-muxing.js +++ b/test/stream-muxing.node.js @@ -7,7 +7,7 @@ const expect = chai.expect const parallel = require('async/parallel') const series = require('async/series') const pull = require('pull-stream') -const utils = require('./utils') +const utils = require('./utils/node') const createNode = utils.createNode const echo = utils.echo @@ -36,7 +36,7 @@ function teardown (nodeA, nodeB, callback) { describe('stream muxing', () => { it('spdy only', function (done) { - this.timeout(5000) + this.timeout(5 * 1000) let nodeA let nodeB diff --git a/test/transports.browser.js b/test/transports.browser.js new file mode 100644 index 0000000000..01808df20f --- /dev/null +++ b/test/transports.browser.js @@ -0,0 +1,424 @@ +/* eslint max-nested-callbacks: ["error", 8] */ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +chai.use(require('dirty-chai')) +const expect = chai.expect +const PeerInfo = require('peer-info') +const PeerId = require('peer-id') +const pull = require('pull-stream') +const parallel = require('async/parallel') +const goodbye = require('pull-goodbye') +const serializer = require('pull-serializer') +const w = require('webrtcsupport') + +const Node = require('./utils/bundle.browser') +const rawPeer = require('./fixtures/test-peer.json') + +describe('transports', () => { + describe('websockets', () => { + let peerB + let nodeA + + before((done) => { + const ma = '/ip4/127.0.0.1/tcp/9200/ws/ipfs/' + rawPeer.id + + PeerId.createFromPrivKey(rawPeer.privKey, (err, id) => { + if (err) { + return done(err) + } + + peerB = new PeerInfo(id) + peerB.multiaddrs.add(ma) + done() + }) + }) + + after((done) => nodeA.stop(done)) + + it('create libp2pNode', (done) => { + PeerInfo.create((err, peerInfo) => { + expect(err).to.not.exist() + peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') + + nodeA = new Node(peerInfo) + done() + }) + }) + + it('create libp2pNode with multiplex only', (done) => { + PeerInfo.create((err, peerInfo) => { + expect(err).to.not.exist() + + const b = new Node(peerInfo, null, { muxer: ['multiplex'] }) + expect(b.modules.connection.muxer).to.eql([require('libp2p-multiplex')]) + done() + }) + }) + + it('start libp2pNode', (done) => { + nodeA.start(done) + }) + + // General connectivity tests + + it('libp2p.dial using Multiaddr nodeA to nodeB', (done) => { + nodeA.dial(peerB.multiaddrs.toArray()[0], (err) => { + expect(err).to.not.exist() + + setTimeout(check, 500) // Some time for Identify to finish + + function check () { + const peers = nodeA.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + done() + } + }) + }) + + it('libp2p.dial using Multiaddr on Protocol nodeA to nodeB', (done) => { + nodeA.dial(peerB.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + + const peers = nodeA.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + + pull( + pull.values([Buffer.from('hey')]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist() + expect(data).to.eql([Buffer.from('hey')]) + done() + }) + ) + }) + }) + + it('libp2p.hangUp using Multiaddr nodeA to nodeB', (done) => { + nodeA.hangUp(peerB.multiaddrs.toArray()[0], (err) => { + expect(err).to.not.exist() + + setTimeout(check, 500) + + function check () { + const peers = nodeA.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) + done() + } + }) + }) + + it('libp2p.dial using PeerInfo nodeA to nodeB', (done) => { + nodeA.dial(peerB, (err) => { + expect(err).to.not.exist() + + setTimeout(check, 500) // Some time for Identify to finish + + function check () { + const peers = nodeA.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + done() + } + }) + }) + + it('libp2p.dial using PeerInfo on Protocol nodeA to nodeB', (done) => { + nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + const peers = nodeA.peerBook.getAll() + expect(err).to.not.exist() + expect(Object.keys(peers)).to.have.length(1) + + pull( + pull.values([Buffer.from('hey')]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist() + expect(data).to.eql([Buffer.from('hey')]) + done() + }) + ) + }) + }) + + it('libp2p.hangUp using PeerInfo nodeA to nodeB', (done) => { + nodeA.hangUp(peerB, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + const peers = nodeA.peerBook.getAll() + expect(err).to.not.exist() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) + done() + } + }) + }) + + describe('stress', () => { + it('one big write', (done) => { + nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + const rawMessage = Buffer.alloc(100000) + rawMessage.fill('a') + + const s = serializer(goodbye({ + source: pull.values([rawMessage]), + sink: pull.collect((err, results) => { + expect(err).to.not.exist() + expect(results).to.have.length(1) + expect(Buffer.from(results[0])).to.have.length(rawMessage.length) + done() + }) + })) + pull(s, conn, s) + }) + }) + + it('many writes', (done) => { + nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + + const s = serializer(goodbye({ + source: pull( + pull.infinite(), + pull.take(1000), + pull.map((val) => Buffer.from(val.toString())) + ), + sink: pull.collect((err, result) => { + expect(err).to.not.exist() + expect(result).to.have.length(1000) + done() + }) + })) + + pull(s, conn, s) + }) + }) + }) + }) + + describe('webrtc-star', () => { + if (!w.support) { return console.log('NO WEBRTC SUPPORT') } + + let peer1 + let peer2 + let node1 + let node2 + + it('create two peerInfo with webrtc-star addrs', (done) => { + parallel([ + (cb) => PeerId.create({ bits: 1024 }, cb), + (cb) => PeerId.create({ bits: 1024 }, cb) + ], (err, ids) => { + expect(err).to.not.exist() + + peer1 = new PeerInfo(ids[0]) + const ma1 = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/ipfs/' + ids[0].toB58String() + peer1.multiaddrs.add(ma1) + + peer2 = new PeerInfo(ids[1]) + const ma2 = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/ipfs/' + ids[1].toB58String() + peer2.multiaddrs.add(ma2) + + done() + }) + }) + + it('create two libp2p nodes with those peers', (done) => { + node1 = new Node(peer1, null, { webRTCStar: true }) + node2 = new Node(peer2, null, { webRTCStar: true }) + done() + }) + + it('listen on the two libp2p nodes', (done) => { + parallel([ + (cb) => node1.start(cb), + (cb) => node2.start(cb) + ], done) + }) + + it('handle a protocol on the first node', () => { + node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) + }) + + it('dial from the second node to the first node', (done) => { + node1.dial(peer2, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + const text = 'hello' + const peers1 = node1.peerBook.getAll() + expect(Object.keys(peers1)).to.have.length(1) + + const peers2 = node2.peerBook.getAll() + expect(Object.keys(peers2)).to.have.length(1) + + pull( + pull.values([Buffer.from(text)]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist() + expect(data[0].toString()).to.equal(text) + done() + }) + ) + } + }) + }) + + it('node1 hangUp node2', (done) => { + node1.hangUp(peer2, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + const peers = node1.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0) + done() + } + }) + }) + + it('create a third node and check that discovery works', (done) => { + let counter = 0 + + function check () { + if (++counter === 3) { + expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1) + expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1) + done() + } + } + + PeerId.create((err, id3) => { + expect(err).to.not.exist() + + const peer3 = new PeerInfo(id3) + const ma3 = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/ipfs/' + id3.toB58String() + peer3.multiaddrs.add(ma3) + + node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check)) + node2.on('peer:discovery', (peerInfo) => node2.dial(peerInfo, check)) + + const node3 = new Node(peer3, null, { webRTCStar: true }) + node3.start(check) + }) + }) + }) + + describe('websocket-star', () => { + let peer1 + let peer2 + let node1 + let node2 + + it('create two peerInfo with websocket-star addrs', (done) => { + parallel([ + (cb) => PeerId.create({ bits: 1024 }, cb), + (cb) => PeerId.create({ bits: 1024 }, cb) + ], (err, ids) => { + expect(err).to.not.exist() + + peer1 = new PeerInfo(ids[0]) + const ma1 = '/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/' + peer1.multiaddrs.add(ma1) + + peer2 = new PeerInfo(ids[1]) + const ma2 = '/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/' + peer2.multiaddrs.add(ma2) + + done() + }) + }) + + it('create two libp2p nodes with those peers', (done) => { + node1 = new Node(peer1, null, { wsStar: true }) + node2 = new Node(peer2, null, { wsStar: true }) + done() + }) + + it('listen on the two libp2p nodes', (done) => { + parallel([ + (cb) => node1.start(cb), + (cb) => node2.start(cb) + ], done) + }) + + it('handle a protocol on the first node', () => { + node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) + }) + + it('dial from the second node to the first node', (done) => { + node1.dial(peer2, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + const text = 'hello' + const peers1 = node1.peerBook.getAll() + expect(Object.keys(peers1)).to.have.length(1) + + const peers2 = node2.peerBook.getAll() + expect(Object.keys(peers2)).to.have.length(1) + + pull( + pull.values([Buffer.from(text)]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist() + expect(data[0].toString()).to.equal(text) + done() + }) + ) + } + }) + }) + + it('node1 hangUp node2', (done) => { + node1.hangUp(peer2, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + const peers = node1.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0) + done() + } + }) + }) + + it('create a third node and check that discovery works', (done) => { + let counter = 0 + + function check () { + if (++counter === 3) { + expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1) + expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1) + done() + } + } + + PeerId.create((err, id3) => { + expect(err).to.not.exist() + + const peer3 = new PeerInfo(id3) + const ma3 = '/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/ipfs/' + id3.toB58String() + peer3.multiaddrs.add(ma3) + + node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check)) + node2.on('peer:discovery', (peerInfo) => node2.dial(peerInfo, check)) + + const node3 = new Node(peer3, null, { wsStar: true }) + node3.start(check) + }) + }) + }) +}) diff --git a/test/transports.node.js b/test/transports.node.js new file mode 100644 index 0000000000..2a4989a04f --- /dev/null +++ b/test/transports.node.js @@ -0,0 +1,677 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +chai.use(require('dirty-chai')) +const expect = chai.expect +const parallel = require('async/parallel') +const series = require('async/series') +const pull = require('pull-stream') +const utils = require('./utils/node.js') +const signalling = require('libp2p-webrtc-star/src/sig-server') +const rendezvous = require('libp2p-websocket-star-rendezvous') +const WSStar = require('libp2p-websocket-star') +const WRTCStar = require('libp2p-webrtc-star') +const wrtc = require('wrtc') + +const createNode = utils.createNode +const echo = utils.echo + +describe('transports', () => { + describe('TCP only', () => { + let nodeA + let nodeB + + before((done) => { + parallel([ + (cb) => createNode('/ip4/0.0.0.0/tcp/0', (err, node) => { + expect(err).to.not.exist() + nodeA = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }), + (cb) => createNode('/ip4/0.0.0.0/tcp/0', (err, node) => { + expect(err).to.not.exist() + nodeB = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }) + ], done) + }) + + after((done) => { + parallel([ + (cb) => nodeA.stop(cb), + (cb) => nodeB.stop(cb) + ], done) + }) + + it('nodeA.dial nodeB using PeerInfo without proto (warmup)', (done) => { + nodeA.dial(nodeB.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeA.peerBook.getAll() + expect(err).to.not.exist() + expect(Object.keys(peers)).to.have.length(1) + cb() + }, + (cb) => { + const peers = nodeB.peerBook.getAll() + expect(err).to.not.exist() + expect(Object.keys(peers)).to.have.length(1) + cb() + } + ], done) + } + }) + }) + + it('nodeA.dial nodeB using PeerInfo', (done) => { + nodeA.dial(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist() + + pull( + pull.values([Buffer.from('hey')]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist() + expect(data).to.be.eql([Buffer.from('hey')]) + done() + }) + ) + }) + }) + + it('nodeA.hangUp nodeB using PeerInfo (first)', (done) => { + nodeA.hangUp(nodeB.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeA.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) + cb() + }, + (cb) => { + const peers = nodeB.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + + expect(Object.keys(nodeB.swarm.muxedConns)).to.have.length(0) + cb() + } + ], done) + } + }) + }) + + it('nodeA.dial nodeB using multiaddr', (done) => { + nodeA.dial(nodeB.peerInfo.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => { + // Some time for Identify to finish + setTimeout(check, 500) + + function check () { + expect(err).to.not.exist() + series([ + (cb) => { + const peers = nodeA.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + + expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1) + cb() + }, + (cb) => { + const peers = nodeB.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + + expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1) + cb() + } + ], () => { + pull( + pull.values([Buffer.from('hey')]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist() + expect(data).to.be.eql([Buffer.from('hey')]) + done() + }) + ) + }) + } + }) + }) + + it('nodeA.hangUp nodeB using multiaddr (second)', (done) => { + nodeA.hangUp(nodeB.peerInfo.multiaddrs.toArray()[0], (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeA.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + + expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) + cb() + }, + (cb) => { + const peers = nodeB.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + + expect(Object.keys(nodeB.swarm.muxedConns)).to.have.length(0) + cb() + } + ], done) + } + }) + }) + + it('nodeA.dial nodeB using PeerId', (done) => { + nodeA.dial(nodeB.peerInfo.id, '/echo/1.0.0', (err, conn) => { + // Some time for Identify to finish + setTimeout(check, 500) + + function check () { + expect(err).to.not.exist() + series([ + (cb) => { + const peers = nodeA.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1) + cb() + }, + (cb) => { + const peers = nodeB.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1) + cb() + } + ], () => { + pull( + pull.values([Buffer.from('hey')]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist() + expect(data).to.be.eql([Buffer.from('hey')]) + done() + }) + ) + }) + } + }) + }) + + it('nodeA.hangUp nodeB using PeerId (third)', (done) => { + nodeA.hangUp(nodeB.peerInfo.multiaddrs.toArray()[0], (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeA.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0) + cb() + }, + (cb) => { + const peers = nodeB.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeB.swarm.muxedConns)).to.have.length(0) + cb() + } + ], done) + } + }) + }) + }) + + describe('TCP + WebSockets', () => { + let nodeTCP + let nodeTCPnWS + let nodeWS + + before((done) => { + parallel([ + (cb) => createNode([ + '/ip4/0.0.0.0/tcp/0' + ], (err, node) => { + expect(err).to.not.exist() + nodeTCP = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }), + (cb) => createNode([ + '/ip4/0.0.0.0/tcp/0', + '/ip4/127.0.0.1/tcp/25011/ws' + ], (err, node) => { + expect(err).to.not.exist() + nodeTCPnWS = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }), + (cb) => createNode([ + '/ip4/127.0.0.1/tcp/25022/ws' + ], (err, node) => { + expect(err).to.not.exist() + nodeWS = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }) + ], done) + }) + + after((done) => { + parallel([ + (cb) => nodeTCP.stop(cb), + (cb) => nodeTCPnWS.stop(cb), + (cb) => nodeWS.stop(cb) + ], done) + }) + + it('nodeTCP.dial nodeTCPnWS using PeerInfo', (done) => { + nodeTCP.dial(nodeTCPnWS.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeTCP.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(1) + cb() + }, + (cb) => { + const peers = nodeTCPnWS.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(1) + cb() + } + ], done) + } + }) + }) + + it('nodeTCP.hangUp nodeTCPnWS using PeerInfo', (done) => { + nodeTCP.hangUp(nodeTCPnWS.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeTCP.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(0) + + cb() + }, + (cb) => { + const peers = nodeTCPnWS.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(0) + cb() + } + ], done) + } + }) + }) + + it('nodeTCPnWS.dial nodeWS using PeerInfo', (done) => { + nodeTCPnWS.dial(nodeWS.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeTCPnWS.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(2) + expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(1) + cb() + }, + (cb) => { + const peers = nodeWS.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(1) + cb() + } + ], done) + } + }) + }) + + it('nodeTCPnWS.hangUp nodeWS using PeerInfo', (done) => { + nodeTCPnWS.hangUp(nodeWS.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500) + + function check () { + parallel([ + (cb) => { + const peers = nodeTCPnWS.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(2) + expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(0) + + cb() + }, + (cb) => { + const peers = nodeWS.peerBook.getAll() + expect(Object.keys(peers)).to.have.length(1) + expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(0) + cb() + } + ], done) + } + }) + }) + + // Until https://github.com/libp2p/js-libp2p/issues/46 is resolved + // Everynode will be able to dial in WebSockets + it.skip('nodeTCP.dial nodeWS using PeerInfo is unsuccesful', (done) => { + nodeTCP.dial(nodeWS.peerInfo, (err) => { + expect(err).to.exist() + done() + }) + }) + }) + + describe('TCP + WebSockets + WebRTCStar', () => { + let nodeAll + let nodeTCP + let nodeWS + let nodeWStar + + let ss + + before(function (done) { + this.timeout(5 * 1000) + parallel([ + (cb) => { + signalling.start({ port: 24642 }, (err, server) => { + expect(err).to.not.exist() + ss = server + cb() + }) + }, + (cb) => { + const wstar = new WRTCStar({wrtc: wrtc}) + createNode([ + '/ip4/0.0.0.0/tcp/0', + '/ip4/127.0.0.1/tcp/25011/ws', + '/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star' + ], { + modules: { + transport: [wstar], + discovery: [wstar.discovery] + } + }, (err, node) => { + expect(err).to.not.exist() + nodeAll = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }) + }, + (cb) => createNode([ + '/ip4/0.0.0.0/tcp/0' + ], (err, node) => { + expect(err).to.not.exist() + nodeTCP = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }), + (cb) => createNode([ + '/ip4/127.0.0.1/tcp/25022/ws' + ], (err, node) => { + expect(err).to.not.exist() + nodeWS = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }), + + (cb) => { + const wstar = new WRTCStar({wrtc: wrtc}) + + createNode([ + '/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star' + ], { + modules: { + transport: [wstar], + discovery: [wstar.discovery] + } + }, (err, node) => { + expect(err).to.not.exist() + nodeWStar = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }) + } + ], done) + }) + + after((done) => { + parallel([ + (cb) => nodeAll.stop(cb), + (cb) => nodeTCP.stop(cb), + (cb) => nodeWS.stop(cb), + (cb) => nodeWStar.stop(cb), + (cb) => ss.stop(cb) + ], done) + }) + + function check (otherNode, done, muxed, peers) { + let i = 1; + [nodeAll, otherNode].forEach((node) => { + expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1) + expect(Object.keys(node.swarm.muxedConns)).to.have.length(muxed) + }) + done() + } + + it('nodeAll.dial nodeTCP using PeerInfo', (done) => { + nodeAll.dial(nodeTCP.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500, nodeTCP, done, 1, 1) + }) + }) + + it('nodeAll.hangUp nodeTCP using PeerInfo', (done) => { + nodeAll.hangUp(nodeTCP.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500, nodeTCP, done, 0, 1) + }) + }) + + it('nodeAll.dial nodeWS using PeerInfo', (done) => { + nodeAll.dial(nodeWS.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500, nodeWS, done, 1, 2) + }) + }) + + it('nodeAll.hangUp nodeWS using PeerInfo', (done) => { + nodeAll.hangUp(nodeWS.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500, nodeWS, done, 0, 2) + }) + }) + + it('nodeAll.dial nodeWStar using PeerInfo', function (done) { + this.timeout(10000) + nodeAll.dial(nodeWStar.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500, nodeWStar, done, 1, 3) + }) + }) + + it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => { + nodeAll.hangUp(nodeWStar.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500, nodeWStar, done, 0, 3) + }) + }) + }) + + describe('TCP + WebSockets + WebSocketStar', () => { + let nodeAll + let nodeTCP + let nodeWS + let nodeWStar + + let ss + + before((done) => { + parallel([ + (cb) => { + rendezvous.start({ port: 24642 }, (err, server) => { + expect(err).to.not.exist() + ss = server + cb() + }) + }, + (cb) => { + const wstar = new WSStar() + createNode([ + '/ip4/0.0.0.0/tcp/0', + '/ip4/127.0.0.1/tcp/25011/ws', + '/ip4/127.0.0.1/tcp/24642/ws/p2p-websocket-star' + ], { + modules: { + transport: [wstar], + discovery: [wstar.discovery] + } + }, (err, node) => { + expect(err).to.not.exist() + nodeAll = node + wstar.lazySetId(node.peerInfo.id) + node.handle('/echo/1.0.0', echo) + node.start(cb) + }) + }, + (cb) => createNode([ + '/ip4/0.0.0.0/tcp/0' + ], (err, node) => { + expect(err).to.not.exist() + nodeTCP = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }), + (cb) => createNode([ + '/ip4/127.0.0.1/tcp/25022/ws' + ], (err, node) => { + expect(err).to.not.exist() + nodeWS = node + node.handle('/echo/1.0.0', echo) + node.start(cb) + }), + + (cb) => { + const wstar = new WSStar({}) + + createNode([ + '/ip4/127.0.0.1/tcp/24642/ws/p2p-websocket-star' + ], { + modules: { + transport: [wstar], + discovery: [wstar.discovery] + } + }, (err, node) => { + expect(err).to.not.exist() + nodeWStar = node + wstar.lazySetId(node.peerInfo.id) + node.handle('/echo/1.0.0', echo) + node.start(cb) + }) + } + ], done) + }) + + after((done) => { + parallel([ + (cb) => nodeAll.stop(cb), + (cb) => nodeTCP.stop(cb), + (cb) => nodeWS.stop(cb), + (cb) => nodeWStar.stop(cb), + (cb) => ss.stop(cb) + ], done) + }) + + function check (otherNode, done, muxed, peers) { + let i = 1; + [nodeAll, otherNode].forEach((node) => { + expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1) + expect(Object.keys(node.swarm.muxedConns)).to.have.length(muxed) + }) + done() + } + + it('nodeAll.dial nodeTCP using PeerInfo', (done) => { + nodeAll.dial(nodeTCP.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500, nodeTCP, done, 1, 1) + }) + }) + + it('nodeAll.hangUp nodeTCP using PeerInfo', (done) => { + nodeAll.hangUp(nodeTCP.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500, nodeTCP, done, 0, 1) + }) + }) + + it('nodeAll.dial nodeWS using PeerInfo', (done) => { + nodeAll.dial(nodeWS.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500, nodeWS, done, 1, 2) + }) + }) + + it('nodeAll.hangUp nodeWS using PeerInfo', (done) => { + nodeAll.hangUp(nodeWS.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500, nodeWS, done, 0, 2) + }) + }) + + it('nodeAll.dial nodeWStar using PeerInfo', (done) => { + nodeAll.dial(nodeWStar.peerInfo, (err) => { + expect(err).to.not.exist() + + // Some time for Identify to finish + setTimeout(check, 500, nodeWStar, done, 1, 3) + }) + }) + + it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => { + nodeAll.hangUp(nodeWStar.peerInfo, (err) => { + expect(err).to.not.exist() + setTimeout(check, 500, nodeWStar, done, 0, 3) + }) + }) + }) +}) diff --git a/test/nodejs-bundle/turbolence.js b/test/turbolence.node.js similarity index 96% rename from test/nodejs-bundle/turbolence.js rename to test/turbolence.node.js index 527e73ef47..8c4dee4c50 100644 --- a/test/nodejs-bundle/turbolence.js +++ b/test/turbolence.node.js @@ -7,9 +7,8 @@ const expect = chai.expect const multiaddr = require('multiaddr') const spawn = require('child_process').spawn const path = require('path') -// const map = require('async/map') const pull = require('pull-stream') -const utils = require('./utils') +const utils = require('./utils/node') const createNode = utils.createNode const echo = utils.echo diff --git a/test/browser-bundle/browser-bundle.js b/test/utils/bundle.browser.js similarity index 96% rename from test/browser-bundle/browser-bundle.js rename to test/utils/bundle.browser.js index 70d79dbd7a..5e85aef983 100644 --- a/test/browser-bundle/browser-bundle.js +++ b/test/utils/bundle.browser.js @@ -37,7 +37,7 @@ class Node extends libp2p { constructor (peerInfo, peerBook, options) { options = options || {} const webRTCStar = new WebRTCStar() - const wsStar = new WebSocketStar({ id: peerInfo.id }) + const wsStar = new WebSocketStar() const modules = { transport: [ diff --git a/test/nodejs-bundle/nodejs-bundle.js b/test/utils/bundle.node.js similarity index 100% rename from test/nodejs-bundle/nodejs-bundle.js rename to test/utils/bundle.node.js diff --git a/test/nodejs-bundle/utils.js b/test/utils/node.js similarity index 95% rename from test/nodejs-bundle/utils.js rename to test/utils/node.js index 6a89ed7861..4343207a71 100644 --- a/test/nodejs-bundle/utils.js +++ b/test/utils/node.js @@ -3,7 +3,7 @@ const chai = require('chai') chai.use(require('dirty-chai')) -const Node = require('./nodejs-bundle') +const Node = require('./bundle.node') const PeerInfo = require('peer-info') const PeerId = require('peer-id') const waterfall = require('async/waterfall') From baa560a71f94823991d8072608eff49b3077c896 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 27 Nov 2017 08:48:29 +0000 Subject: [PATCH 6/7] use right flag --- .aegir.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.aegir.js b/.aegir.js index a43dcb6471..5422a010df 100644 --- a/.aegir.js +++ b/.aegir.js @@ -28,11 +28,9 @@ const before = (done) => { port: 14444, refreshPeerListIntervalMS: 1000, strictMultiaddr: false, - disableCryptoChallenge: true + cryptoChallenge: false }, (err, _server) => { - if (err) { - return cb(err) - } + if (err) { return cb(err) } wsRendezvous = _server cb() }) From 724fe5b5875aaae8d1c3cf6b2d4ac69f000dab58 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 27 Nov 2017 08:59:34 +0000 Subject: [PATCH 7/7] test: refactor transport tests --- test/transports.node.js | 47 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/test/transports.node.js b/test/transports.node.js index 2a4989a04f..413f27939e 100644 --- a/test/transports.node.js +++ b/test/transports.node.js @@ -202,7 +202,7 @@ describe('transports', () => { conn, pull.collect((err, data) => { expect(err).to.not.exist() - expect(data).to.be.eql([Buffer.from('hey')]) + expect(data).to.eql([Buffer.from('hey')]) done() }) ) @@ -401,6 +401,7 @@ describe('transports', () => { before(function (done) { this.timeout(5 * 1000) + parallel([ (cb) => { signalling.start({ port: 24642 }, (err, server) => { @@ -474,61 +475,61 @@ describe('transports', () => { ], done) }) - function check (otherNode, done, muxed, peers) { + function check (otherNode, muxed, peers, callback) { let i = 1; [nodeAll, otherNode].forEach((node) => { expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1) expect(Object.keys(node.swarm.muxedConns)).to.have.length(muxed) }) - done() + callback() } it('nodeAll.dial nodeTCP using PeerInfo', (done) => { nodeAll.dial(nodeTCP.peerInfo, (err) => { expect(err).to.not.exist() - // Some time for Identify to finish - setTimeout(check, 500, nodeTCP, done, 1, 1) + setTimeout(() => check(nodeTCP, 1, 1, done), 500) }) }) it('nodeAll.hangUp nodeTCP using PeerInfo', (done) => { nodeAll.hangUp(nodeTCP.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500, nodeTCP, done, 0, 1) + // Some time for Identify to finish + setTimeout(() => check(nodeTCP, 0, 1, done), 500) }) }) it('nodeAll.dial nodeWS using PeerInfo', (done) => { nodeAll.dial(nodeWS.peerInfo, (err) => { expect(err).to.not.exist() - // Some time for Identify to finish - setTimeout(check, 500, nodeWS, done, 1, 2) + setTimeout(() => check(nodeWS, 1, 2, done), 500) }) }) it('nodeAll.hangUp nodeWS using PeerInfo', (done) => { nodeAll.hangUp(nodeWS.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500, nodeWS, done, 0, 2) + // Some time for Identify to finish + setTimeout(() => check(nodeWS, 0, 2, done), 500) }) }) it('nodeAll.dial nodeWStar using PeerInfo', function (done) { - this.timeout(10000) + this.timeout(10 * 1000) + nodeAll.dial(nodeWStar.peerInfo, (err) => { expect(err).to.not.exist() - // Some time for Identify to finish - setTimeout(check, 500, nodeWStar, done, 1, 3) + setTimeout(() => check(nodeWStar, 1, 3, done), 500) }) }) it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => { nodeAll.hangUp(nodeWStar.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500, nodeWStar, done, 0, 3) + setTimeout(() => check(nodeWStar, 0, 3, done), 500) }) }) }) @@ -617,7 +618,7 @@ describe('transports', () => { ], done) }) - function check (otherNode, done, muxed, peers) { + function check (otherNode, muxed, peers, done) { let i = 1; [nodeAll, otherNode].forEach((node) => { expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1) @@ -629,48 +630,48 @@ describe('transports', () => { it('nodeAll.dial nodeTCP using PeerInfo', (done) => { nodeAll.dial(nodeTCP.peerInfo, (err) => { expect(err).to.not.exist() - // Some time for Identify to finish - setTimeout(check, 500, nodeTCP, done, 1, 1) + setTimeout(() => check(nodeTCP, 1, 1, done), 500) }) }) it('nodeAll.hangUp nodeTCP using PeerInfo', (done) => { nodeAll.hangUp(nodeTCP.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500, nodeTCP, done, 0, 1) + // Some time for Identify to finish + setTimeout(() => check(nodeTCP, 0, 1, done), 500) }) }) it('nodeAll.dial nodeWS using PeerInfo', (done) => { nodeAll.dial(nodeWS.peerInfo, (err) => { expect(err).to.not.exist() - // Some time for Identify to finish - setTimeout(check, 500, nodeWS, done, 1, 2) + setTimeout(() => check(nodeWS, 1, 2, done), 500) }) }) it('nodeAll.hangUp nodeWS using PeerInfo', (done) => { nodeAll.hangUp(nodeWS.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500, nodeWS, done, 0, 2) + // Some time for Identify to finish + setTimeout(() => check(nodeWS, 0, 2, done), 500) }) }) it('nodeAll.dial nodeWStar using PeerInfo', (done) => { nodeAll.dial(nodeWStar.peerInfo, (err) => { expect(err).to.not.exist() - // Some time for Identify to finish - setTimeout(check, 500, nodeWStar, done, 1, 3) + setTimeout(() => check(nodeWStar, 1, 3, done), 500) }) }) it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => { nodeAll.hangUp(nodeWStar.peerInfo, (err) => { expect(err).to.not.exist() - setTimeout(check, 500, nodeWStar, done, 0, 3) + // Some time for Identify to finish + setTimeout(() => check(nodeWStar, 0, 3, done), 500) }) }) })