diff --git a/src/swarm.js b/src/swarm.js index 763b73b1..2949c11e 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -79,10 +79,10 @@ function Swarm (peerInfo) { // if it is and no protocol was selected, do nothing and call and empty callback if (self.conns[peerInfo.id.toB58String()]) { - console.log('Had conn warmed up') if (protocol) { multistreamHandshake(self.conns[peerInfo.id.toB58String()]) self.conns[peerInfo.id.toB58String()] = undefined + delete self.conns[peerInfo.id.toB58String()] return } else { return callback() @@ -208,7 +208,6 @@ function Swarm (peerInfo) { // internals function listen (conn) { - console.log('Received new connection') // TODO apply upgrades // TODO then add StreamMuxer if available (and point streams from muxer to userProtocolMuxer) diff --git a/tests/swarm-test.js b/tests/swarm-test.js index 658e2a3e..129a716f 100644 --- a/tests/swarm-test.js +++ b/tests/swarm-test.js @@ -126,7 +126,59 @@ experiment('Without a Stream Muxer', function () { } }) - test('dial a protocol on a previous created conn', function (done) { done() }) + test('dial a protocol on a previous created conn', function (done) { + var mh1 = multiaddr('/ip4/127.0.0.1/tcp/8010') + var p1 = new Peer(Id.create(), []) + var sw1 = new Swarm(p1) + sw1.addTransport('tcp', tcp, { multiaddr: mh1 }, {}, {port: 8010}, ready) + + var mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020') + var p2 = new Peer(Id.create(), []) + var sw2 = new Swarm(p2) + sw2.addTransport('tcp', tcp, { multiaddr: mh2 }, {}, {port: 8020}, ready) + + var readyCounter = 0 + + sw2.handleProtocol('/sparkles/1.0.0', function (conn) { + conn.end() + conn.on('end', function () { + var cleaningCounter = 0 + sw1.closeConns(cleaningUp) + sw2.closeConns(cleaningUp) + + sw1.closeListener('tcp', cleaningUp) + sw2.closeListener('tcp', cleaningUp) + + function cleaningUp () { + cleaningCounter++ + if (cleaningCounter < 4) { + return + } + + done() + } + }) + }) + + function ready () { + readyCounter++ + if (readyCounter < 2) { + return + } + + sw1.dial(p2, {}, function (err) { + expect(err).to.equal(undefined) + expect(Object.keys(sw1.conns).length).to.equal(1) + + sw1.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) { + expect(err).to.equal(null) + expect(Object.keys(sw1.conns).length).to.equal(0) + conn.end() + }) + }) + } + }) + test('add an upgrade', function (done) { done() }) test('dial a conn on top of a upgrade', function (done) { done() }) test('dial a conn on a protocol on top of a upgrade', function (done) { done() })