diff --git a/package.json b/package.json index 417374703..ee21a8f06 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "chai": "^4.2.0", "chai-checkmark": "^1.0.1", "class-is": "^1.1.0", + "delay": "^4.3.0", "detect-node": "^2.0.4", "dirty-chai": "^2.0.1", "err-code": "^2.0.0", @@ -47,6 +48,7 @@ "it-pipe": "^1.0.1", "libp2p-tcp": "^0.14.1", "multiaddr": "^7.1.0", + "p-defer": "^3.0.0", "p-limit": "^2.2.2", "p-wait-for": "^3.1.0", "peer-id": "^0.13.3", diff --git a/src/peer-discovery/README.md b/src/peer-discovery/README.md index 74bb96c6b..339ca3e26 100644 --- a/src/peer-discovery/README.md +++ b/src/peer-discovery/README.md @@ -69,6 +69,9 @@ It returns a `Promise` ### discoverying peers -- `discovery.on('peer', (peerInfo) => {})` +- `discovery.on('peer', (peerData) => {})` -Everytime a peer is discovered by a discovery service, it emmits a `peer` event with the discover peer's [PeerInfo](https://github.com/libp2p/js-peer-info). +Every time a peer is discovered by a discovery service, it emits a `peer` event with the discovered peer's information, which must contain the following properties: + +- `<`[`PeerId`](https://github.com/libp2p/js-peer-id)`>` `peerData.id` +- `>` `peerData.multiaddrs` diff --git a/src/peer-discovery/tests/index.js b/src/peer-discovery/tests/index.js index 343a7db37..bdcda8091 100644 --- a/src/peer-discovery/tests/index.js +++ b/src/peer-discovery/tests/index.js @@ -1,17 +1,31 @@ /* eslint-env mocha */ 'use strict' +const chai = require('chai') +const expect = chai.expect +chai.use(require('dirty-chai')) + +const multiaddr = require('multiaddr') +const PeerId = require('peer-id') + +const delay = require('delay') +const pDefer = require('p-defer') + module.exports = (common) => { describe('interface-peer-discovery', () => { let discovery - before(async () => { + beforeEach(async () => { discovery = await common.setup() }) - after(() => common.teardown && common.teardown()) + afterEach('ensure discovery was stopped', async () => { + await discovery.stop() - afterEach('ensure discovery was stopped', () => discovery.stop()) + discovery.removeAllListeners() + + common.teardown && common.teardown() + }) it('can start the service', async () => { await discovery.start() @@ -30,5 +44,49 @@ module.exports = (common) => { await discovery.start() await discovery.start() }) + + it('should emit a peer event after start', async () => { + const defer = pDefer() + await discovery.start() + + discovery.once('peer', ({ id, multiaddrs }) => { + expect(id).to.exist() + expect(PeerId.isPeerId(id)).to.eql(true) + expect(multiaddrs).to.exist() + + multiaddrs.forEach((m) => expect(multiaddr.isMultiaddr(m)).to.eql(true)) + + defer.resolve() + }) + + await defer.promise + }) + + it('should not receive a peer event before start', async () => { + discovery.once('peer', () => { + throw new Error('should not receive a peer event before start') + }) + + await delay(2000) + }) + + it('should not receive a peer event after stop', async () => { + const deferStart = pDefer() + + await discovery.start() + + discovery.once('peer', () => { + deferStart.resolve() + }) + + await deferStart.promise + await discovery.stop() + + discovery.once('peer', () => { + throw new Error('should not receive a peer event after stop') + }) + + await delay(2000) + }) }) } diff --git a/test/peer-discovery/compliance.spec.js b/test/peer-discovery/compliance.spec.js index 6602f097b..7ab0d6ddd 100644 --- a/test/peer-discovery/compliance.spec.js +++ b/test/peer-discovery/compliance.spec.js @@ -5,13 +5,20 @@ const tests = require('../../src/peer-discovery/tests') const MockDiscovery = require('./mock-discovery') describe('compliance tests', () => { + let intervalId + tests({ - async setup () { - await new Promise(resolve => setTimeout(resolve, 10)) - return new MockDiscovery() + setup () { + const mockDiscovery = new MockDiscovery({ + discoveryDelay: 1 + }) + + intervalId = setInterval(mockDiscovery._discoverPeer, 1000) + + return mockDiscovery }, - async teardown () { - await new Promise(resolve => setTimeout(resolve, 10)) + teardown () { + clearInterval(intervalId) } }) }) diff --git a/test/peer-discovery/mock-discovery.js b/test/peer-discovery/mock-discovery.js index c64b87846..a9f0ca28c 100644 --- a/test/peer-discovery/mock-discovery.js +++ b/test/peer-discovery/mock-discovery.js @@ -2,8 +2,8 @@ const { EventEmitter } = require('events') +const multiaddr = require('multiaddr') const PeerId = require('peer-id') -const PeerInfo = require('peer-info') /** * Emits 'peer' events on discovery. @@ -37,10 +37,12 @@ class MockDiscovery extends EventEmitter { if (!this._isRunning) return const peerId = await PeerId.create({ bits: 512 }) - const peerInfo = new PeerInfo(peerId) this._timer = setTimeout(() => { - this.emit('peer', peerInfo) + this.emit('peer', { + id: peerId, + multiaddrs: [multiaddr('/ip4/127.0.0.1/tcp/8000')] + }) }, this.options.discoveryDelay || 1000) } }