2019-11-19 17:44:45 -06:00
|
|
|
'use strict'
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
|
|
|
const chai = require('chai')
|
|
|
|
chai.use(require('dirty-chai'))
|
|
|
|
const { expect } = chai
|
|
|
|
const sinon = require('sinon')
|
2019-11-28 22:50:14 +01:00
|
|
|
|
2019-11-19 17:44:45 -06:00
|
|
|
const defer = require('p-defer')
|
2019-11-28 22:50:14 +01:00
|
|
|
const mergeOptions = require('merge-options')
|
|
|
|
|
|
|
|
const MulticastDNS = require('libp2p-mdns')
|
2019-11-19 17:44:45 -06:00
|
|
|
|
|
|
|
const Libp2p = require('../../src')
|
|
|
|
const baseOptions = require('../utils/base-options.browser')
|
2019-12-01 22:54:59 +01:00
|
|
|
const { createPeerInfo } = require('../utils/creators/peer')
|
2019-11-19 17:44:45 -06:00
|
|
|
|
|
|
|
describe('peer discovery', () => {
|
|
|
|
let peerInfo
|
|
|
|
let remotePeerInfo
|
|
|
|
let libp2p
|
|
|
|
|
|
|
|
before(async () => {
|
2019-12-01 22:54:59 +01:00
|
|
|
[peerInfo, remotePeerInfo] = await createPeerInfo({ number: 2 })
|
2019-11-19 17:44:45 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
libp2p && await libp2p.stop()
|
2019-11-28 22:50:14 +01:00
|
|
|
sinon.reset()
|
2019-11-19 17:44:45 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should dial know peers on startup', async () => {
|
|
|
|
libp2p = new Libp2p({
|
|
|
|
...baseOptions,
|
|
|
|
peerInfo
|
|
|
|
})
|
|
|
|
libp2p.peerStore.add(remotePeerInfo)
|
|
|
|
const deferred = defer()
|
|
|
|
sinon.stub(libp2p.dialer, 'connectToPeer').callsFake((remotePeerInfo) => {
|
|
|
|
expect(remotePeerInfo).to.equal(remotePeerInfo)
|
|
|
|
deferred.resolve()
|
|
|
|
})
|
|
|
|
const spy = sinon.spy()
|
|
|
|
libp2p.on('peer:discovery', spy)
|
|
|
|
|
|
|
|
libp2p.start()
|
|
|
|
await deferred.promise
|
|
|
|
expect(spy.getCall(0).args).to.eql([remotePeerInfo])
|
|
|
|
})
|
2019-11-28 22:50:14 +01:00
|
|
|
|
|
|
|
it('should ignore self on discovery', async () => {
|
|
|
|
libp2p = new Libp2p(mergeOptions(baseOptions, {
|
|
|
|
peerInfo,
|
|
|
|
modules: {
|
|
|
|
peerDiscovery: [MulticastDNS]
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
await libp2p.start()
|
|
|
|
const discoverySpy = sinon.spy()
|
|
|
|
libp2p.on('peer:discovery', discoverySpy)
|
|
|
|
libp2p._discovery[0].emit('peer', libp2p.peerInfo)
|
|
|
|
|
|
|
|
expect(discoverySpy.called).to.eql(false)
|
|
|
|
})
|
2019-11-19 17:44:45 -06:00
|
|
|
})
|