use warmed up connection + test

This commit is contained in:
David Dias 2015-09-22 17:27:37 +01:00
parent 8e8d8e9093
commit 59b00f6886
2 changed files with 54 additions and 3 deletions

View File

@ -79,10 +79,10 @@ function Swarm (peerInfo) {
// if it is and no protocol was selected, do nothing and call and empty callback // if it is and no protocol was selected, do nothing and call and empty callback
if (self.conns[peerInfo.id.toB58String()]) { if (self.conns[peerInfo.id.toB58String()]) {
console.log('Had conn warmed up')
if (protocol) { if (protocol) {
multistreamHandshake(self.conns[peerInfo.id.toB58String()]) multistreamHandshake(self.conns[peerInfo.id.toB58String()])
self.conns[peerInfo.id.toB58String()] = undefined self.conns[peerInfo.id.toB58String()] = undefined
delete self.conns[peerInfo.id.toB58String()]
return return
} else { } else {
return callback() return callback()
@ -208,7 +208,6 @@ function Swarm (peerInfo) {
// internals // internals
function listen (conn) { function listen (conn) {
console.log('Received new connection')
// TODO apply upgrades // TODO apply upgrades
// TODO then add StreamMuxer if available (and point streams from muxer to userProtocolMuxer) // TODO then add StreamMuxer if available (and point streams from muxer to userProtocolMuxer)

View File

@ -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('add an upgrade', function (done) { done() })
test('dial a conn on top of a 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() }) test('dial a conn on a protocol on top of a upgrade', function (done) { done() })