From f7f85dce0aa67fd50efe858c273e1daa2bf21779 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 7 Feb 2018 08:22:03 +0000 Subject: [PATCH] test: update tests to new API --- src/index.js | 4 +- test/circuit-relay.node.js | 64 ++++++++++---------------------- test/stream-muxing.node.js | 19 +++------- test/transports.browser.js | 75 +++++++++++--------------------------- test/transports.node.js | 45 +++++------------------ test/utils/try-echo.js | 21 +++++++++++ 6 files changed, 78 insertions(+), 150 deletions(-) create mode 100644 test/utils/try-echo.js diff --git a/src/index.js b/src/index.js index b6663ae7..b6974988 100644 --- a/src/index.js +++ b/src/index.js @@ -215,11 +215,11 @@ class Node extends EventEmitter { this._getPeerInfo(peer, (err, peerInfo) => { if (err) { return callback(err) } - this.switch.dial(peerInfo, (err, conn) => { + this.switch.dial(peerInfo, (err) => { if (err) { return callback(err) } this.peerBook.put(peerInfo) - callback(null, conn) + callback() }) }) } diff --git a/test/circuit-relay.node.js b/test/circuit-relay.node.js index d3a54887..9fa45d64 100644 --- a/test/circuit-relay.node.js +++ b/test/circuit-relay.node.js @@ -1,17 +1,16 @@ /* eslint-env mocha */ 'use strict' -const pull = require('pull-stream') const waterfall = require('async/waterfall') const series = require('async/series') const parallel = require('async/parallel') const utils = require('./utils/node') const Circuit = require('libp2p-circuit') const multiaddr = require('multiaddr') +const tryEcho = require('./utils/try-echo') const chai = require('chai') chai.use(require('dirty-chai')) - const expect = chai.expect const sinon = require('sinon') @@ -153,25 +152,16 @@ describe('circuit relay', function () { 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) => { + it('dial from WS1 to TCP1 over any R', (done) => { + nodeWS1.dialProtocol(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() expect(conn).to.exist() - - pull( - pull.values(['hello']), - conn, - pull.collect((err, result) => { - expect(err).to.not.exist() - expect(result[0].toString()).to.equal('hello') - done() - }) - ) + tryEcho(conn, done) }) }) - it('should not dial - no R from WS2 to TCP1', (done) => { - nodeWS2.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { + it('fail to dial - no R from WS2 to TCP1', (done) => { + nodeWS2.dialProtocol(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.exist() expect(conn).to.not.exist() done() @@ -182,43 +172,29 @@ describe('circuit relay', function () { 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) => { + it('dial from WS1 to TCP1 over R1', (done) => { + nodeWS1.dialProtocol(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() expect(conn).to.exist() - pull( - pull.values(['hello']), - conn, - 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() - expect(addr).to.equal(`/ipfs/${nodeTCP1.peerInfo.id.toB58String()}`) - done() - }) - ) + tryEcho(conn, () => { + const addr = multiaddr(handlerSpies[0].args[2][0].dstPeer.addrs[0]).toString() + expect(addr).to.equal(`/ipfs/${nodeTCP1.peerInfo.id.toB58String()}`) + done() + }) }) }) - it('should dial from WS1 to TCP2 over R2', (done) => { - nodeWS1.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { + it('dial from WS1 to TCP2 over R2', (done) => { + nodeWS1.dialProtocol(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() expect(conn).to.exist() - pull( - pull.values(['hello']), - conn, - 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() - expect(addr).to.equal(`/ipfs/${nodeTCP2.peerInfo.id.toB58String()}`) - done() - }) - ) + tryEcho(conn, () => { + const addr = multiaddr(handlerSpies[1].args[2][0].dstPeer.addrs[0]).toString() + expect(addr).to.equal(`/ipfs/${nodeTCP2.peerInfo.id.toB58String()}`) + done() + }) }) }) }) diff --git a/test/stream-muxing.node.js b/test/stream-muxing.node.js index 42227f2b..9ef91a80 100644 --- a/test/stream-muxing.node.js +++ b/test/stream-muxing.node.js @@ -6,24 +6,15 @@ 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') +const tryEcho = require('./utils/try-echo') const createNode = utils.createNode const echo = utils.echo function test (nodeA, nodeB, callback) { - nodeA.dial(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => { + nodeA.dialProtocol(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')]) - callback() - }) - ) + tryEcho(conn, callback) }) } @@ -136,7 +127,7 @@ describe('stream muxing', () => { }) it('spdy + multiplex switched order', function (done) { - this.timeout(5000) + this.timeout(5 * 1000) let nodeA let nodeB @@ -170,7 +161,7 @@ describe('stream muxing', () => { }) it('one without the other fails to establish a muxedConn', 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 index 71d83e53..89aaf8dd 100644 --- a/test/transports.browser.js +++ b/test/transports.browser.js @@ -12,6 +12,7 @@ const parallel = require('async/parallel') const goodbye = require('pull-goodbye') const serializer = require('pull-serializer') const w = require('webrtcsupport') +const tryEcho = require('./utils/try-echo') const Node = require('./utils/bundle.browser') const rawPeer = require('./fixtures/test-peer.json') @@ -63,7 +64,7 @@ describe('transports', () => { // General connectivity tests - it('libp2p.dial using Multiaddr nodeA to nodeB', (done) => { + it('.dial using Multiaddr', (done) => { nodeA.dial(peerB.multiaddrs.toArray()[0], (err) => { expect(err).to.not.exist() @@ -77,26 +78,18 @@ describe('transports', () => { }) }) - it('libp2p.dial using Multiaddr on Protocol nodeA to nodeB', (done) => { - nodeA.dial(peerB.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => { + it('.dialProtocol using Multiaddr', (done) => { + nodeA.dialProtocol(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() - }) - ) + tryEcho(conn, done) }) }) - it('libp2p.hangUp using Multiaddr nodeA to nodeB', (done) => { + it('.hangUp using Multiaddr', (done) => { nodeA.hangUp(peerB.multiaddrs.toArray()[0], (err) => { expect(err).to.not.exist() @@ -111,7 +104,7 @@ describe('transports', () => { }) }) - it('libp2p.dial using PeerInfo nodeA to nodeB', (done) => { + it('.dial using PeerInfo', (done) => { nodeA.dial(peerB, (err) => { expect(err).to.not.exist() @@ -125,26 +118,18 @@ describe('transports', () => { }) }) - it('libp2p.dial using PeerInfo on Protocol nodeA to nodeB', (done) => { - nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => { + it('.dialProtocol using PeerInfo', (done) => { + nodeA.dialProtocol(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() - }) - ) + tryEcho(conn, done) }) }) - it('libp2p.hangUp using PeerInfo nodeA to nodeB', (done) => { + it('.hangUp using PeerInfo', (done) => { nodeA.hangUp(peerB, (err) => { expect(err).to.not.exist() setTimeout(check, 500) @@ -161,7 +146,7 @@ describe('transports', () => { describe('stress', () => { it('one big write', (done) => { - nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => { + nodeA.dialProtocol(peerB, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() const rawMessage = Buffer.alloc(100000) rawMessage.fill('a') @@ -180,7 +165,7 @@ describe('transports', () => { }) it('many writes', (done) => { - nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => { + nodeA.dialProtocol(peerB, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() const s = serializer(goodbye({ @@ -235,39 +220,30 @@ describe('transports', () => { done() }) - it('listen on the two libp2p nodes', (done) => { + it('start two libp2p nodes', (done) => { parallel([ (cb) => node1.start(cb), (cb) => node2.start(cb) ], done) }) - it('handle a protocol on the first node', () => { + it('.handle echo on 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) => { + it('.dialProtocol from the second node to the first node', (done) => { + node1.dialProtocol(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() - }) - ) + tryEcho(conn, done) } }) }) @@ -355,28 +331,19 @@ describe('transports', () => { 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) => { + it('.dialProtocol from the second node to the first node', (done) => { + node1.dialProtocol(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() - }) - ) + tryEcho(conn, done) } }) }) diff --git a/test/transports.node.js b/test/transports.node.js index 02d2f508..4c105bbe 100644 --- a/test/transports.node.js +++ b/test/transports.node.js @@ -13,6 +13,7 @@ const rendezvous = require('libp2p-websocket-star-rendezvous') const WSStar = require('libp2p-websocket-star') const WRTCStar = require('libp2p-webrtc-star') const wrtc = require('wrtc') +const tryEcho = require('./utils/try-echo') const createNode = utils.createNode const echo = utils.echo @@ -73,18 +74,10 @@ describe('transports', () => { }) it('nodeA.dial nodeB using PeerInfo', (done) => { - nodeA.dial(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => { + nodeA.dialProtocol(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() - }) - ) + tryEcho(conn, done) }) }) @@ -113,8 +106,8 @@ describe('transports', () => { }) }) - it('nodeA.dial nodeB using multiaddr', (done) => { - nodeA.dial(nodeB.peerInfo.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => { + it('nodeA.dialProtocol nodeB using multiaddr', (done) => { + nodeA.dialProtocol(nodeB.peerInfo.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => { // Some time for Identify to finish setTimeout(check, 500) @@ -135,17 +128,7 @@ describe('transports', () => { expect(Object.keys(nodeA.switch.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() - }) - ) - }) + ], () => tryEcho(conn, done)) } }) }) @@ -176,8 +159,8 @@ describe('transports', () => { }) }) - it('nodeA.dial nodeB using PeerId', (done) => { - nodeA.dial(nodeB.peerInfo.id, '/echo/1.0.0', (err, conn) => { + it('nodeA.dialProtocol nodeB using PeerId', (done) => { + nodeA.dialProtocol(nodeB.peerInfo.id, '/echo/1.0.0', (err, conn) => { // Some time for Identify to finish setTimeout(check, 500) @@ -196,17 +179,7 @@ describe('transports', () => { expect(Object.keys(nodeA.switch.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.eql([Buffer.from('hey')]) - done() - }) - ) - }) + ], () => tryEcho(conn, done)) } }) }) diff --git a/test/utils/try-echo.js b/test/utils/try-echo.js new file mode 100644 index 00000000..b8874b74 --- /dev/null +++ b/test/utils/try-echo.js @@ -0,0 +1,21 @@ +'use strict' + +const pull = require('pull-stream') +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) + +module.exports = (conn, callback) => { + const values = [Buffer.from('echo')] + + pull( + pull.values(values), + conn, + pull.collect((err, _values) => { + expect(err).to.not.exist() + expect(_values).to.eql(values) + callback() + }) + ) +}