feat: auto dial discovered peers (#349)

This commit is contained in:
Jacob Heun
2019-04-11 12:44:58 +02:00
committed by GitHub
parent 8b627797e2
commit 01aa44724e
12 changed files with 430 additions and 153 deletions

View File

@ -68,12 +68,17 @@ describe('peer discovery', () => {
(cb) => ss.stop(cb)
], done)
})
afterEach(() => {
sinon.restore()
})
}
describe('module registration', () => {
it('should enable by default a module passed as an object', (done) => {
const mockDiscovery = {
on: sinon.stub(),
removeListener: sinon.stub(),
start: sinon.stub().callsArg(0),
stop: sinon.stub().callsArg(0)
}
@ -94,6 +99,7 @@ describe('peer discovery', () => {
it('should enable by default a module passed as a function', (done) => {
const mockDiscovery = {
on: sinon.stub(),
removeListener: sinon.stub(),
start: sinon.stub().callsArg(0),
stop: sinon.stub().callsArg(0)
}
@ -116,6 +122,7 @@ describe('peer discovery', () => {
it('should enable module by configutation', (done) => {
const mockDiscovery = {
on: sinon.stub(),
removeListener: sinon.stub(),
start: sinon.stub().callsArg(0),
stop: sinon.stub().callsArg(0),
tag: 'mockDiscovery'
@ -151,6 +158,7 @@ describe('peer discovery', () => {
it('should disable module by configutation', (done) => {
const mockDiscovery = {
on: sinon.stub(),
removeListener: sinon.stub(),
start: sinon.stub().callsArg(0),
stop: sinon.stub().callsArg(0),
tag: 'mockDiscovery'
@ -186,6 +194,7 @@ describe('peer discovery', () => {
it('should register module passed as function', (done) => {
const mockDiscovery = {
on: sinon.stub(),
removeListener: sinon.stub(),
start: sinon.stub().callsArg(0),
stop: sinon.stub().callsArg(0)
}
@ -223,6 +232,7 @@ describe('peer discovery', () => {
it('should register module passed as object', (done) => {
const mockDiscovery = {
on: sinon.stub(),
removeListener: sinon.stub(),
start: sinon.stub().callsArg(0),
stop: sinon.stub().callsArg(0),
tag: 'mockDiscovery'
@ -256,9 +266,10 @@ describe('peer discovery', () => {
enabled: false
},
peerDiscovery: {
autoDial: true,
mdns: {
enabled: true,
interval: 1e3, // discover quickly
interval: 200, // discover quickly
// use a random tag to prevent CI collision
serviceTag: crypto.randomBytes(10).toString('hex')
}
@ -295,6 +306,7 @@ describe('peer discovery', () => {
enabled: false
},
peerDiscovery: {
autoDial: true,
webRTCStar: {
enabled: true
}
@ -331,9 +343,10 @@ describe('peer discovery', () => {
enabled: false
},
peerDiscovery: {
autoDial: true,
mdns: {
enabled: true,
interval: 1e3, // discovery quickly
interval: 200, // discovery quickly
// use a random tag to prevent CI collision
serviceTag: crypto.randomBytes(10).toString('hex')
},
@ -369,6 +382,7 @@ describe('peer discovery', () => {
setup({
config: {
peerDiscovery: {
autoDial: true,
mdns: {
enabled: false
},
@ -382,7 +396,7 @@ describe('peer discovery', () => {
randomWalk: {
enabled: true,
queriesPerPeriod: 1,
interval: 1000, // start the query sooner
interval: 200, // start the query sooner
timeout: 3000
}
}
@ -419,4 +433,45 @@ describe('peer discovery', () => {
})
})
})
describe('auto dial', () => {
setup({
connectionManager: {
minPeers: 1
},
config: {
peerDiscovery: {
autoDial: true,
mdns: {
enabled: false
},
webRTCStar: {
enabled: false
},
bootstrap: {
enabled: true,
list: []
}
},
dht: {
enabled: false
}
}
})
it('should only dial when the peer count is below the low watermark', (done) => {
const bootstrap = nodeA._discovery[0]
sinon.stub(nodeA._switch.dialer, 'connect').callsFake((peerInfo) => {
nodeA._switch.connection.connections[peerInfo.id.toB58String()] = []
})
bootstrap.emit('peer', nodeB.peerInfo)
bootstrap.emit('peer', nodeC.peerInfo)
// Only nodeB should get dialed
expect(nodeA._switch.dialer.connect.callCount).to.eql(1)
expect(nodeA._switch.dialer.connect.getCall(0).args[0]).to.eql(nodeB.peerInfo)
done()
})
})
})