diff --git a/README.md b/README.md index 7c21bc1d..3e40b773 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,8 @@ sw.addStreamMuxer(streamMuxer, [options]) ### Dial to another peer ```JavaScript -sw.dial(PeerInfo, options, protocol) -sw.dial(PeerInfo, options) +sw.dial(PeerInfo, options, protocol, callback) +sw.dial(PeerInfo, options, callback) ``` dial uses the best transport (whatever works first, in the future we can have some criteria), and jump starts the connection until the point we have to negotiate the protocol. If a muxer is available, then drop the muxer onto that connection. Good to warm up connections or to check for connectivity. If we have already a muxer for that peerInfo, than do nothing. diff --git a/package.json b/package.json index 7a2df286..f889081a 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "test": "./node_modules/.bin/lab tests/*-test.js", "coverage": "./node_modules/.bin/lab -t 88 tests/*-test.js", - "lint": "./node_modules/.bin/standard" + "lint": "./node_modules/.bin/standard", + "validate": "npm ls" }, "repository": { "type": "git", @@ -51,4 +52,4 @@ "protocol-buffers-stream": "^1.2.0", "spdy-stream-muxer": "^0.6.0" } -} +} \ No newline at end of file diff --git a/src/swarm.js b/src/swarm.js index 29ef20d4..54c25426 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -136,6 +136,11 @@ function Swarm (peerInfo) { }) }) + if (!multiaddrs.length) { + callback(new Error("The swarm doesn't support any of the peer transports")) + return + } + var conn async.eachSeries(multiaddrs, function (multiaddr, next) { @@ -185,6 +190,7 @@ function Swarm (peerInfo) { // if protocol is selected, multistream that protocol if (!conn) { callback(new Error('Unable to open a connection')) + return } if (self.muxers['spdy']) { diff --git a/tests/swarm-test.js b/tests/swarm-test.js index cf37da23..07a20147 100644 --- a/tests/swarm-test.js +++ b/tests/swarm-test.js @@ -16,19 +16,20 @@ var Swarm = require('../src') var tcp = require('libp2p-tcp') var Spdy = require('libp2p-spdy') -/* TODO -experiment('Basics', function () { - test('enforces creation with new', function (done) {done() }) -}) -*/ - // because of Travis-CI process.on('uncaughtException', function (err) { console.log('Caught exception: ' + err) }) -experiment('Without a peer', function () { - test('it throws an exception', function (done) { +experiment('Basics', function () { + test('enforces creation with new', function (done) { + expect(function () { + Swarm() + }).to.throw() + done() + }) + + test('it throws an exception without peerSelf', function (done) { expect(function () { var sw = new Swarm() sw.close() @@ -37,6 +38,21 @@ experiment('Without a peer', function () { }) }) +experiment('When dialing', function () { + experiment('if the swarm does add any of the peer transports', function () { + test('it returns an error', function (done) { + var peerOne = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/8090')]) + var peerTwo = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/8091')]) + var swarm = new Swarm(peerOne) + + swarm.dial(peerTwo, {}, function (err) { + expect(err).to.exist() + done() + }) + }) + }) +}) + experiment('Without a Stream Muxer', function () { experiment('and one swarm over tcp', function () { test('add the transport', function (done) { @@ -218,7 +234,6 @@ experiment('With a SPDY Stream Muxer', function () { } test('dial a conn on a protocol', function (done) { - sw2.handleProtocol('/sparkles/1.0.0', function (conn) { // formallity so that the conn starts flowing conn.on('data', function (chunk) {})