connection reuse test

This commit is contained in:
David Dias 2015-07-10 12:28:40 -07:00
parent a80a5fc32b
commit a5b2524873
5 changed files with 54 additions and 49 deletions

View File

@ -13,4 +13,17 @@ Ref link (still a WiP) - https://github.com/diasdavid/specs/blob/protocol-spec/p
# Usage
## API calls
.openStream
.registerHandle
## Events emmited
.on('error')
.on('connection')
.on('connection-unknown')
.on('stream')
.on('stream-unknown')

View File

@ -14,11 +14,11 @@ i.on('thenews', function (news) {
})
b.on('error', function (err) {
if (err) return
console.log(err)
})
b.listen()
b.registerHandle('/ipfs/sparkles/1.2.3', function (stream) {
b.registerHandle('/ipfs/sparkles/1.2.3', function (err, stream) {
console.log('woop got a stream')
})

View File

@ -26,26 +26,19 @@ function Swarm () {
// set the listener
self.listen = function (port, ready) {
if (!ready) {
ready = function noop () {}
}
if (!ready) { ready = function noop () {} }
if (typeof port === 'function') {
ready = port
} else if (port) {
self.port = port
}
} else if (port) { self.port = port }
self.listener = tcp.createServer(function (socket) {
socket.on('error', errorEmit)
errorUp(self, socket)
var ms = new Select()
ms.handle(socket)
ms.addHandler('/spdy/3.1.0', function (ds) {
log.info('Negotiated spdy with incoming socket')
var conn = spdy.connection.create(ds, {
protocol: 'spdy',
isServer: true
})
var conn = spdy.connection.create(ds, { protocol: 'spdy', isServer: true })
conn.start(3.1)
@ -53,14 +46,13 @@ function Swarm () {
// attach multistream handlers to incoming streams
conn.on('stream', registerHandles)
conn.on('error', errorEmit)
errorUp(self, conn)
// IDENTIFY DOES THAT FOR US
// conn.on('close', function () { delete self.connections[conn.peerId] })
})
}).listen(self.port, ready)
self.listener.on('error', errorEmit)
errorUp(self, self.listener)
}
// interface
@ -76,7 +68,7 @@ function Swarm () {
var tmp = tcp.connect(multiaddr.toOptions(), function () {
socket = tmp
socket.on('error', errorEmit)
errorUp(self, socket)
next()
})
@ -109,7 +101,7 @@ function Swarm () {
self.connections[peer.id.toB58String()] = conn
conn.on('close', function () { delete self.connections[peer.id.toB58String()] })
conn.on('error', errorEmit)
errorUp(self, conn)
createStream(peer, protocol, cb)
})
@ -121,7 +113,7 @@ function Swarm () {
var conn = self.connections[peer.id.toB58String()]
conn.request({path: '/', method: 'GET'}, function (err, stream) {
if (err) { return cb(err) }
stream.on('error', errorEmit)
errorUp(self, stream)
// negotiate desired protocol
var msi = new Interactive()
@ -135,11 +127,11 @@ function Swarm () {
}
}
self.registerHandle = function (protocol, handleFunc) {
self.registerHandler = function (protocol, handlerFunc) {
if (self.handles[protocol]) {
throw new Error('Handle for protocol already exists', protocol)
return handlerFunc(new Error('Handle for protocol already exists', protocol))
}
self.handles.push({ protocol: protocol, func: handleFunc })
self.handles.push({ protocol: protocol, func: handlerFunc })
log.info('Registered handler for protocol:', protocol)
}
@ -161,7 +153,7 @@ function Swarm () {
function registerHandles (stream) {
log.info('Registering protocol handlers on new stream')
stream.on('error', errorEmit)
errorUp(self, stream)
var msH = new Select()
msH.handle(stream)
self.handles.forEach(function (handle) {
@ -169,8 +161,12 @@ function Swarm () {
})
}
function errorEmit (err) { self.emit('error', err) }
}
function errorUp (self, emitter) {
emitter.on('error', function (err) {
self.emit('error', err)
})
}
function Counter (target, callback) {

View File

@ -32,34 +32,21 @@ beforeEach(function (done) {
peerB = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmB.port)])
c.hit()
})
})
afterEach({ timeout: 5000 }, function (done) {
var c = new Counter(4, done)
swarmA.closeConns(function () {
c.hit()
swarmA.closeListener(function () {
console.log('AAA CLOSE')
c.hit()
})
})
swarmB.closeConns(function () {
console.log('bb')
c.hit()
swarmB.closeListener(function () {
console.log('BBB CLOSE')
c.hit()
})
})
afterEach(function (done) {
swarmA.closeListener()
swarmB.closeListener()
done()
})
experiment('BASE', function () {
test('Open a stream', {timeout: false}, function (done) {
test('Open a stream', function (done) {
var protocol = '/sparkles/3.3.3'
var c = new Counter(2, done)
swarmB.registerHandle(protocol, function (stream) {
swarmB.registerHandler(protocol, function (stream) {
c.hit()
})
@ -69,17 +56,21 @@ experiment('BASE', function () {
})
})
test('Reuse stream (from dialer)', {timeout: false}, function (done) {
test('Reuse connection (from dialer)', function (done) {
var protocol = '/sparkles/3.3.3'
var c = new Counter(2, done)
swarmB.registerHandle(protocol, function (stream) {
c.hit()
swarmB.registerHandler(protocol, function (err, stream) {
expect(err).to.not.be.instanceof(Error)
})
swarmA.openStream(peerB, protocol, function (err, stream) {
expect(err).to.not.be.instanceof(Error)
c.hit()
swarmA.openStream(peerB, protocol, function (err, stream) {
expect(err).to.not.be.instanceof(Error)
expect(swarmA.connections.length === 1)
done()
})
})
})
})
@ -99,3 +90,8 @@ function Counter (target, callback) {
}
}
}
function checkErr (err) {
console.log('err')
expect(err).to.be.instanceof(Error)
}