feat: make libp2p a state machine (#257)

* docs: add events to readme
This commit is contained in:
Jacob Heun
2018-10-19 17:37:34 +02:00
committed by GitHub
parent 686379efb0
commit 0b75f99d75
19 changed files with 519 additions and 109 deletions

118
test/fsm.spec.js Normal file
View File

@ -0,0 +1,118 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-checkmark'))
const expect = chai.expect
const sinon = require('sinon')
const series = require('async/series')
const createNode = require('./utils/create-node')
describe('libp2p state machine (fsm)', () => {
describe('starting and stopping', () => {
let node
beforeEach((done) => {
createNode([], (err, _node) => {
node = _node
done(err)
})
})
afterEach(() => {
node.removeAllListeners()
})
after((done) => {
node.stop(done)
node = null
})
it('should be able to start and stop several times', (done) => {
node.on('start', (err) => {
expect(err).to.not.exist().mark()
})
node.on('stop', (err) => {
expect(err).to.not.exist().mark()
})
expect(4).checks(done)
series([
(cb) => node.start(cb),
(cb) => node.stop(cb),
(cb) => node.start(cb),
(cb) => node.stop(cb)
], () => {})
})
it('should noop when stopping a stopped node', (done) => {
node.once('start', node.stop)
node.once('stop', () => {
node.state.on('STOPPING', () => {
throw new Error('should not stop a stopped node')
})
node.once('stop', done)
// stop the stopped node
node.stop()
})
node.start()
})
it('should noop when starting a started node', (done) => {
node.once('start', () => {
node.state.on('STARTING', () => {
throw new Error('should not start a started node')
})
node.once('start', () => {
node.once('stop', done)
node.stop()
})
// start the started node
node.start()
})
node.start()
})
it('should error on start with no transports', (done) => {
let transports = node._modules.transport
node._modules.transport = null
node.on('stop', () => {
node._modules.transport = transports
expect(node._modules.transport).to.exist().mark()
})
node.on('error', (err) => {
expect(err).to.exist().mark()
})
node.on('start', () => {
throw new Error('should not start')
})
expect(2).checks(done)
node.start()
})
it('should not start if the switch fails to start', (done) => {
const error = new Error('switch didnt start')
const stub = sinon.stub(node._switch, 'start')
.callsArgWith(0, error)
node.on('stop', () => {
expect(stub.calledOnce).to.eql(true).mark()
stub.restore()
})
node.on('error', (err) => {
expect(err).to.eql(error).mark()
})
node.on('start', () => {
throw new Error('should not start')
})
expect(2).checks(done)
node.start()
})
})
})

View File

@ -4,8 +4,9 @@ require('./pnet.node')
require('./transports.node')
require('./stream-muxing.node')
require('./peer-discovery.node')
require('./pubsub.node')
require('./peer-routing.node')
require('./ping.node')
require('./pubsub.node')
require('./content-routing.node')
require('./circuit-relay.node')
require('./multiaddr-trim.node')

61
test/ping.node.js Normal file
View File

@ -0,0 +1,61 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
const createNode = require('./utils/create-node.js')
const echo = require('./utils/echo')
describe('ping', () => {
let nodeA
let nodeB
before((done) => {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
expect(err).to.not.exist()
nodeA = node
node.handle('/echo/1.0.0', echo)
node.start(cb)
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
expect(err).to.not.exist()
nodeB = node
node.handle('/echo/1.0.0', echo)
node.start(cb)
})
], done)
})
after((done) => {
parallel([
(cb) => nodeA.stop(cb),
(cb) => nodeB.stop(cb)
], done)
})
it('should be able to ping another node', (done) => {
nodeA.ping(nodeB.peerInfo, (err, ping) => {
expect(err).to.not.exist()
ping.once('ping', (time) => {
expect(time).to.exist()
ping.stop()
done()
})
ping.start()
})
})
it('should be not be able to ping when stopped', (done) => {
nodeA.stop(() => {
nodeA.ping(nodeB.peerInfo, (err) => {
expect(err).to.exist()
done()
})
})
})
})

View File

@ -148,6 +148,42 @@ describe('transports', () => {
})
})
it('.dialFSM check conn and close', (done) => {
nodeA.dialFSM(peerB, (err, connFSM) => {
expect(err).to.not.exist()
connFSM.once('muxed', () => {
expect(nodeA._switch.muxedConns).to.have.any.keys(
peerB.id.toB58String()
)
connFSM.once('error', done)
connFSM.once('close', () => {
// ensure the connection is closed
expect(nodeA._switch.muxedConns).to.not.have.any.keys([
peerB.id.toB58String()
])
done()
})
connFSM.close()
})
})
})
it('.dialFSM with a protocol, do an echo and close', (done) => {
nodeA.dialFSM(peerB, '/echo/1.0.0', (err, connFSM) => {
expect(err).to.not.exist()
connFSM.once('connection', (conn) => {
tryEcho(conn, () => {
connFSM.close()
})
})
connFSM.once('error', done)
connFSM.once('close', done)
})
})
describe('stress', () => {
it('one big write', (done) => {
nodeA.dialProtocol(peerB, '/echo/1.0.0', (err, conn) => {
@ -194,6 +230,7 @@ describe('transports', () => {
})
describe('webrtc-star', () => {
/* eslint-disable-next-line no-console */
if (!w.support) { return console.log('NO WEBRTC SUPPORT') }
let peer1

View File

@ -185,7 +185,7 @@ describe('transports', () => {
})
it('nodeA.hangUp nodeB using PeerId (third)', (done) => {
nodeA.hangUp(nodeB.peerInfo.multiaddrs.toArray()[0], (err) => {
nodeA.hangUp(nodeB.peerInfo.id, (err) => {
expect(err).to.not.exist()
setTimeout(check, 500)
@ -207,6 +207,51 @@ describe('transports', () => {
}
})
})
it('.dialFSM check conn and close', (done) => {
nodeA.dialFSM(nodeB.peerInfo, (err, connFSM) => {
expect(err).to.not.exist()
connFSM.once('muxed', () => {
expect(nodeA._switch.muxedConns).to.have.any.keys(
nodeB.peerInfo.id.toB58String()
)
connFSM.once('error', done)
connFSM.once('close', () => {
// ensure the connection is closed
expect(nodeA._switch.muxedConns).to.not.have.any.keys([
nodeB.peerInfo.id.toB58String()
])
done()
})
connFSM.close()
})
})
})
it('.dialFSM with a protocol, do an echo and close', (done) => {
nodeA.dialFSM(nodeB.peerInfo, '/echo/1.0.0', (err, connFSM) => {
expect(err).to.not.exist()
connFSM.once('connection', (conn) => {
expect(nodeA._switch.muxedConns).to.have.all.keys([
nodeB.peerInfo.id.toB58String()
])
tryEcho(conn, () => {
connFSM.close()
})
})
connFSM.once('error', done)
connFSM.once('close', () => {
// ensure the connection is closed
expect(nodeA._switch.muxedConns).to.not.have.any.keys([
nodeB.peerInfo.id.toB58String()
])
done()
})
})
})
})
describe('TCP + WebSockets', () => {

View File

@ -42,6 +42,7 @@ describe('Turbolence tests', () => {
}
})
/* eslint-disable-next-line no-console */
nodeSpawn.stderr.on('data', (data) => console.log(data.toString()))
})