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

feat: peer-discovery not using peer-info #41

Merged
merged 3 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 5 additions & 2 deletions src/peer-discovery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
- `<Array<`[`Multiaddr`](https://github.com/multiformats/js-multiaddr)`>>` `peerData.multiaddrs`
64 changes: 61 additions & 3 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()
jacobheun marked this conversation as resolved.
Show resolved Hide resolved
})

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()
Expand All @@ -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)
})
})
}
17 changes: 12 additions & 5 deletions test/peer-discovery/compliance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
})
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