chore: add basic discovery module for compliance spec

This commit is contained in:
Vasco Santos
2019-09-27 11:48:45 +02:00
parent e690d28e0d
commit b0b54db5b1
5 changed files with 102 additions and 10 deletions

13
test/compliance.spec.js Normal file
View File

@ -0,0 +1,13 @@
/* eslint-env mocha */
'use strict'
const tests = require('../src')
const MockDiscovery = require('./mock-discovery')
describe('compliance tests', () => {
tests({
setup () {
return new MockDiscovery()
}
})
})

48
test/mock-discovery.js Normal file
View File

@ -0,0 +1,48 @@
'use strict'
const { EventEmitter } = require('events')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
/**
* Emits 'peer' events on discovery.
*/
class MockDiscovery extends EventEmitter {
/**
* Constructs a new Bootstrap.
*
* @param {Object} options
* @param {number} options.discoveryDelay - the delay to find a peer (in milli-seconds)
*/
constructor (options = {}) {
super()
this.options = options
this._isRunning = false
this._timer = null
}
start () {
this._isRunning = true
this._discoverPeer()
}
stop () {
clearTimeout(this._timer)
this._isRunning = false
}
async _discoverPeer () {
if (!this._isRunning) return
const peerId = await PeerId.create({ bits: 512 })
const peerInfo = new PeerInfo(peerId)
this._timer = setTimeout(() => {
this.emit('peer', peerInfo)
}, this.options.discoveryDelay || 1000)
}
}
module.exports = MockDiscovery

View File

@ -1 +0,0 @@
'use strict'