Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
chore: add tests for peer-discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored and jacobheun committed Apr 16, 2020
1 parent e5f4bda commit 12f11c8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
62 changes: 60 additions & 2 deletions src/peer-discovery/tests/index.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
})
})
}
12 changes: 11 additions & 1 deletion test/peer-discovery/compliance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
Expand Down
8 changes: 5 additions & 3 deletions test/peer-discovery/mock-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 12f11c8

Please sign in to comment.