2018-07-19 15:53:18 +02:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const chai = require('chai')
|
|
|
|
chai.use(require('dirty-chai'))
|
|
|
|
const expect = chai.expect
|
|
|
|
const series = require('async/series')
|
|
|
|
const createNode = require('./utils/create-node')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
|
|
describe('libp2p creation', () => {
|
|
|
|
it('should be able to start and stop successfully', (done) => {
|
|
|
|
createNode([], {
|
|
|
|
config: {
|
|
|
|
EXPERIMENTAL: {
|
|
|
|
pubsub: true
|
2019-01-29 17:57:09 +00:00
|
|
|
},
|
|
|
|
dht: {
|
|
|
|
enabled: true
|
2018-07-19 15:53:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
|
|
|
|
let sw = node._switch
|
|
|
|
let cm = node.connectionManager
|
|
|
|
let dht = node._dht
|
|
|
|
let pub = node._floodSub
|
|
|
|
|
|
|
|
sinon.spy(sw, 'start')
|
|
|
|
sinon.spy(cm, 'start')
|
|
|
|
sinon.spy(dht, 'start')
|
2018-10-04 13:40:32 +01:00
|
|
|
sinon.spy(dht.randomWalk, 'start')
|
2018-07-19 15:53:18 +02:00
|
|
|
sinon.spy(pub, 'start')
|
|
|
|
sinon.spy(sw, 'stop')
|
|
|
|
sinon.spy(cm, 'stop')
|
|
|
|
sinon.spy(dht, 'stop')
|
2018-10-04 13:40:32 +01:00
|
|
|
sinon.spy(dht.randomWalk, 'stop')
|
2018-07-19 15:53:18 +02:00
|
|
|
sinon.spy(pub, 'stop')
|
|
|
|
sinon.spy(node, 'emit')
|
|
|
|
|
|
|
|
series([
|
|
|
|
(cb) => node.start(cb),
|
|
|
|
(cb) => {
|
|
|
|
expect(sw.start.calledOnce).to.equal(true)
|
|
|
|
expect(cm.start.calledOnce).to.equal(true)
|
|
|
|
expect(dht.start.calledOnce).to.equal(true)
|
2018-10-04 13:40:32 +01:00
|
|
|
expect(dht.randomWalk.start.calledOnce).to.equal(true)
|
2018-07-19 15:53:18 +02:00
|
|
|
expect(pub.start.calledOnce).to.equal(true)
|
|
|
|
expect(node.emit.calledWith('start')).to.equal(true)
|
|
|
|
|
|
|
|
cb()
|
|
|
|
},
|
|
|
|
(cb) => node.stop(cb)
|
|
|
|
], (err) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
|
|
|
|
expect(sw.stop.calledOnce).to.equal(true)
|
|
|
|
expect(cm.stop.calledOnce).to.equal(true)
|
|
|
|
expect(dht.stop.calledOnce).to.equal(true)
|
2018-10-04 13:40:32 +01:00
|
|
|
expect(dht.randomWalk.stop.called).to.equal(true)
|
2018-07-19 15:53:18 +02:00
|
|
|
expect(pub.stop.calledOnce).to.equal(true)
|
|
|
|
expect(node.emit.calledWith('stop')).to.equal(true)
|
|
|
|
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not create disabled modules', (done) => {
|
|
|
|
createNode([], {
|
|
|
|
config: {
|
|
|
|
EXPERIMENTAL: {
|
|
|
|
pubsub: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
expect(node._floodSub).to.not.exist()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2018-11-12 15:43:32 +01:00
|
|
|
|
|
|
|
it('should not throw errors from switch if node has no error listeners', (done) => {
|
|
|
|
createNode([], {}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
|
|
|
|
node._switch.emit('error', new Error('bad things'))
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should emit errors from switch if node has error listeners', (done) => {
|
|
|
|
const error = new Error('bad things')
|
|
|
|
createNode([], {}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
node.once('error', (err) => {
|
|
|
|
expect(err).to.eql(error)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
node._switch.emit('error', error)
|
|
|
|
})
|
|
|
|
})
|
2018-07-19 15:53:18 +02:00
|
|
|
})
|