From 12f11c88e2b53649b1cad6982429eb0d9d74c718 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Mon, 6 Apr 2020 20:20:27 +0200 Subject: [PATCH] chore: add tests for peer-discovery --- package.json | 2 + src/peer-discovery/tests/index.js | 62 +++++++++++++++++++++++++- test/peer-discovery/compliance.spec.js | 12 ++++- test/peer-discovery/mock-discovery.js | 8 ++-- 4 files changed, 78 insertions(+), 6 deletions(-) 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/tests/index.js b/src/peer-discovery/tests/index.js index 343a7db37..a9b41da33 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', () => discovery.stop()) + afterEach('ensure discovery was stopped', async () => { + await discovery.stop() + + discovery.removeAllListeners() + }) it('can start the service', async () => { await discovery.start() @@ -30,5 +44,49 @@ module.exports = (common) => { await discovery.start() await discovery.start() }) + + it('should listen 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() + }) + + return 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..6b3568171 100644 --- a/test/peer-discovery/compliance.spec.js +++ b/test/peer-discovery/compliance.spec.js @@ -5,12 +5,22 @@ 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() + + const mockDiscovery = new MockDiscovery({ + discoveryDelay: 1 + }) + + intervalId = setInterval(mockDiscovery._discoverPeer, 1000) + + return mockDiscovery }, async teardown () { + clearInterval(intervalId) await new Promise(resolve => setTimeout(resolve, 10)) } }) 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) } }