mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-13 01:01:23 +00:00
118
test/fsm.spec.js
Normal file
118
test/fsm.spec.js
Normal 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()
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user