From e690d28e0dc6a2e2b253254b36da717f419e4add Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 26 Sep 2019 11:27:39 +0200 Subject: [PATCH 1/2] refactor: async --- .gitignore | 40 +++-------------------- .travis.yml | 10 ++++++ README.md | 33 ++++++++++--------- package.json | 35 ++++++++++++-------- src/index.js | 32 ++++++++++++++++++ test/{index.js => peer-discovery.spec.js} | 0 6 files changed, 87 insertions(+), 63 deletions(-) create mode 100644 .travis.yml create mode 100644 src/index.js rename test/{index.js => peer-discovery.spec.js} (100%) diff --git a/.gitignore b/.gitignore index 1be92c3..fd34c26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,38 +1,8 @@ -dist -# Logs -logs -*.log -npm-debug.log* - -# Runtime data -pids -*.pid -*.seed - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov +**/node_modules/ +**/*.log +package-lock.json # Coverage directory used by tools like istanbul coverage - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (http://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules -jspm_packages - -# Optional npm cache directory -.npm - -# Optional REPL history -.node_repl_history +docs +dist diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..68ade7c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +cache: npm +stages: + - check + +node_js: + - '10' + +script: + - npm run lint diff --git a/README.md b/README.md index 91995a2..7e29b93 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The API is presented with both Node.js and Go primitives, however, there is not ## Modules that implement the interface - [JavaScript libp2p-mdns](https://github.com/libp2p/js-libp2p-mdns) -- [JavaScript libp2p-railing](https://github.com/libp2p/js-libp2p-railing) +- [JavaScript libp2p-bootstrap](https://github.com/libp2p/js-libp2p-bootstrap) - [JavaScript libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht) - [JavaScript libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star) - [JavaScript libp2p-websocket-star](https://github.com/libp2p/js-libp2p-websocket-star) @@ -44,11 +44,11 @@ Install `interface-peer-discovery` as one of the dependencies of your project an const test = require('interface-peer-discovery') const common = { - setup (cb) { - cb(null, yourMuxer) + setup () { + return YourDiscovery }, - teardown (cb) { - cb() + teardown () { + // Clean up any resources created by setup() } } @@ -56,25 +56,28 @@ const common = { test(common) ``` -### Go - -> WIP - go-libp2p does not have a test suite available for Peer Discovery yet. - ## API A valid (read: that follows this abstraction) Peer Discovery module must implement the following API: ### `start` the service -- `JavaScript` discovery.start(callback) -- `Go` NA +- `await discovery.start()` + +Start the discovery service. + +It returns a `Promise` ### `stop` the service -- `JavaScript` discovery.stop(callback) -- `Go` NA +- `await discovery.stop()` + +Stop the discovery service. + +It returns a `Promise` ### discoverying peers -- `JavaScript` discovery.on('peer', function (peerInfo) {}) -- `Go` NA +- `discovery.on('peer', (peerInfo) => {})` + +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). diff --git a/package.json b/package.json index 3482b9a..70f51a7 100644 --- a/package.json +++ b/package.json @@ -5,22 +5,29 @@ "leadMaintainer": "Vasco Santos ", "main": "src/index.js", "scripts": { - "lint": "aegir-lint", - "build": "aegir-build", - "test": "exit 0", - "release": "aegir-release", - "release-minor": "aegir-release --type minor", - "release-major": "aegir-release --type major" + "lint": "aegir lint", + "test": "aegir test", + "build": "aegir build", + "release": "aegir release --no-test", + "release-minor": "aegir release --type minor --no-test", + "release-major": "aegir release --type major --no-test", + "coverage": "exit(0)", + "coverage-publish": "exit(0)" }, - "pre-commit": [ - "lint", - "test" + "pre-push": [ + "lint" ], "repository": { "type": "git", "url": "https://github.com/libp2p/interface-peer-discovery.git" }, "keywords": [ + "libp2p", + "network", + "p2p", + "peer", + "discovery", + "peer-to-peer", "IPFS" ], "author": "David Dias ", @@ -31,13 +38,15 @@ "homepage": "https://github.com/libp2p/interface-peer-discovery", "dependencies": {}, "devDependencies": { - "aegir": "^18.2.2" + "aegir": "^20.3.1", + "chai": "^4.2.0", + "dirty-chai": "^2.0.1" }, "engines": { - "node": ">=4.0.0", - "npm": ">=3.0.0" + "node": ">=10.0.0", + "npm": ">=6.0.0" }, "contributors": [ "David Dias " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..5b00fbd --- /dev/null +++ b/src/index.js @@ -0,0 +1,32 @@ +/* eslint-env mocha */ +'use strict' + +module.exports = (common) => { + describe('interface-peer-discovery', () => { + let discovery + + before(() => { + discovery = common.setup() + }) + + after(() => common.teardown && common.teardown()) + + it('can start the service', async () => { + await discovery.start() + }) + + it('can start and stop the service', async () => { + await discovery.start() + await discovery.stop() + }) + + it('should not fail to stop the service if it was not started', async () => { + await discovery.stop() + }) + + it('should not fail to start the service if it is already started', async () => { + await discovery.start() + await discovery.start() + }) + }) +} diff --git a/test/index.js b/test/peer-discovery.spec.js similarity index 100% rename from test/index.js rename to test/peer-discovery.spec.js From b0b54db5b1d8418f2e8eb01ccf16eeb532c6c3dd Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 27 Sep 2019 11:48:45 +0200 Subject: [PATCH 2/2] chore: add basic discovery module for compliance spec --- .travis.yml | 34 ++++++++++++++++++++++++-- package.json | 16 +++++++------ test/compliance.spec.js | 13 ++++++++++ test/mock-discovery.js | 48 +++++++++++++++++++++++++++++++++++++ test/peer-discovery.spec.js | 1 - 5 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 test/compliance.spec.js create mode 100644 test/mock-discovery.js delete mode 100644 test/peer-discovery.spec.js diff --git a/.travis.yml b/.travis.yml index 68ade7c..c7f74c1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,39 @@ language: node_js cache: npm stages: - check + - test + - cov node_js: - '10' + - '12' -script: - - npm run lint +os: + - linux + - osx + - windows + +script: npx nyc -s npm run test:node -- --bail +after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov + +jobs: + include: + - stage: check + script: + - npx aegir dep-check + - npm run lint + + - stage: test + name: chrome + addons: + chrome: stable + script: npx aegir test -t browser -t webworker + + - stage: test + name: firefox + addons: + firefox: latest + script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless + +notifications: + email: false diff --git a/package.json b/package.json index 70f51a7..d9b146e 100644 --- a/package.json +++ b/package.json @@ -6,13 +6,13 @@ "main": "src/index.js", "scripts": { "lint": "aegir lint", - "test": "aegir test", "build": "aegir build", - "release": "aegir release --no-test", - "release-minor": "aegir release --type minor --no-test", - "release-major": "aegir release --type major --no-test", - "coverage": "exit(0)", - "coverage-publish": "exit(0)" + "test": "aegir test", + "test:node": "aegir test -t node", + "test:browser": "aegir test -t browser -t webworker", + "release": "aegir release", + "release-minor": "aegir release --type minor", + "release-major": "aegir release --type major" }, "pre-push": [ "lint" @@ -40,7 +40,9 @@ "devDependencies": { "aegir": "^20.3.1", "chai": "^4.2.0", - "dirty-chai": "^2.0.1" + "dirty-chai": "^2.0.1", + "peer-id": "^0.13.3", + "peer-info": "^0.17.0" }, "engines": { "node": ">=10.0.0", diff --git a/test/compliance.spec.js b/test/compliance.spec.js new file mode 100644 index 0000000..1eadba9 --- /dev/null +++ b/test/compliance.spec.js @@ -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() + } + }) +}) diff --git a/test/mock-discovery.js b/test/mock-discovery.js new file mode 100644 index 0000000..c64b878 --- /dev/null +++ b/test/mock-discovery.js @@ -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 diff --git a/test/peer-discovery.spec.js b/test/peer-discovery.spec.js deleted file mode 100644 index ccacec3..0000000 --- a/test/peer-discovery.spec.js +++ /dev/null @@ -1 +0,0 @@ -'use strict'