Skip to content

Commit

Permalink
chore: enable some browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed May 30, 2019
1 parent 5f4a7ad commit e66a7df
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 17 deletions.
71 changes: 71 additions & 0 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict'

const pull = require('pull-stream')
const parallel = require('async/parallel')
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')

const Node = require('./test/utils/nodejs-bundle.js')
const {
getPeerRelay,
WS_RENDEZVOUS_MULTIADDR
} = require('./test/utils/constants.js')

let wsRendezvous
let node

const before = (done) => {
parallel([
(cb) => {
WebSocketStarRendezvous.start({
port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port,
refreshPeerListIntervalMS: 1000,
strictMultiaddr: false,
cryptoChallenge: true
}, (err, _server) => {
if (err) {
return cb(err)
}
wsRendezvous = _server
cb()
})
},
(cb) => {
getPeerRelay((err, peerInfo) => {
if (err) {
return done(err)
}

node = new Node({
peerInfo,
config: {
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
}
})

node.handle('/echo/1.0.0', (_, conn) => pull(conn, conn))
node.start(cb)
})
}
], done)
}

const after = (done) => {
setTimeout(() =>
parallel(
[node, wsRendezvous].map((s) => (cb) => s.stop(cb)),
done),
2000)
}

module.exports = {
hooks: {
pre: before,
post: after
}
}
18 changes: 17 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ os:
- osx
- windows

script: npx nyc -s npm run test -- --bail
script: npx nyc -s npm run test:node -- --bail
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov

jobs:
Expand All @@ -24,5 +24,21 @@ jobs:
- npx aegir dep-check
- npm run lint

- stage: test
name: chrome
addons:
chrome: stable
script:
- npx aegir test -t browser
- npx aegir test -t webworker

- stage: test
name: firefox
addons:
firefox: latest
script:
- npx aegir test -t browser -- --browsers FirefoxHeadless
- npx aegir test -t webworker -- --browsers FirefoxHeadless

notifications:
email: false
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
"test": "aegir test -t node",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"build": "aegir build",
"docs": "aegir-docs",
"release": "aegir release --target node --docs",
Expand All @@ -16,6 +17,9 @@
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage --provider coveralls"
},
"browser": {
"test/utils/nodejs-bundle": "./test/utils/browser-bundle.js"
},
"files": [
"src",
"dist"
Expand Down Expand Up @@ -50,7 +54,10 @@
"libp2p-secio": "~0.11.1",
"libp2p-spdy": "~0.13.3",
"libp2p-tcp": "~0.13.0",
"libp2p-websocket-star": "~0.10.2",
"libp2p-websocket-star-rendezvous": "~0.3.0",
"lodash": "^4.17.11",
"multiaddr": "^6.0.6",
"peer-id": "~0.12.2",
"peer-info": "~0.15.1"
},
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/test-peer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "Qmex1SSsueWFsUfjdkugJ5zhcnjddAt8TxcnDLUXKD9Sx7",
"privKey": "CAASqAkwggSkAgEAAoIBAQCXzV127CvVHOGMzvsn/U+/32JM58KA6k0FSCCeNFzNowiDS/vV5eezGN5AFoxsF6icWLoaczz7l9RdVD+I/t6PEt9X7XUdrDCtSS8WmAcCgvZWSSf7yAd3jT4GSZDUIgIEeRZsERDt/yVqTLwsZ1G9dMIeh8sbf2zwjTXZIWaRM6o4lq3DYFfzLvJUXlJodxPogU7l7nLkITPUv+yQAMcVHizbNwJvwiETKYeUj73/m/wEPAlnFESexDstxNiIwE/FH8Ao50QPZRO6E6Jb0hhYSI/4CLRdrzDFm/Vzplei3Wr2DokSROaNyeG37VAueyA+pDqn84um+L9uXLwbv5FbAgMBAAECggEAdBUzV/GaQ0nmoQrWvOnU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LnCtEpHYZfJr4INXNmhnSiB/3TUnHix2vVKjosjMTCk/CjfzXV2H41WPOLZ2/Pi3SxCicWIRj4kCcWhkEuIF2jGkg1+jmNiCl/zNMaBOAIP3QbDPtqOWbYlPd2YIzdj6WQ6R4",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCXzV127CvVHOGMzvsn/U+/32JM58KA6k0FSCCeNFzNowiDS/vV5eezGN5AFoxsF6icWLoaczz7l9RdVD+I/t6PEt9X7XUdrDCtSS8WmAcCgvZWSSf7yAd3jT4GSZDUIgIEeRZsERDt/yVqTLwsZ1G9dMIeh8sbf2zwjTXZIWaRM6o4lq3DYFfzLvJUXlJodxPogU7l7nLkITPUv+yQAMcVHizbNwJvwiETKYeUj73/m/wEPAlnFESexDstxNiIwE/FH8Ao50QPZRO6E6Jb0hhYSI/4CLRdrzDFm/Vzplei3Wr2DokSROaNyeG37VAueyA+pDqn84um+L9uXLwbv5FbAgMBAAE="
}
3 changes: 0 additions & 3 deletions test/node.js

This file was deleted.

20 changes: 10 additions & 10 deletions test/pubsub.js → test/pubsub.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ describe('pubsub base protocol', () => {

before((done) => {
series([
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb),
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb)
(cb) => createNode(cb),
(cb) => createNode(cb)
], (err, nodes) => {
if (err) {
return done(err)
Expand Down Expand Up @@ -126,8 +126,8 @@ describe('pubsub base protocol', () => {

before((done) => {
series([
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb),
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb)
(cb) => createNode(cb),
(cb) => createNode(cb)
], (cb, nodes) => {
nodeA = nodes[0]
nodeB = nodes[1]
Expand Down Expand Up @@ -177,8 +177,8 @@ describe('pubsub base protocol', () => {
sandbox = chai.spy.sandbox()

series([
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb),
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb)
(cb) => createNode(cb),
(cb) => createNode(cb)
], (err, nodes) => {
if (err) return done(err)

Expand Down Expand Up @@ -238,8 +238,8 @@ describe('pubsub base protocol', () => {
sandbox = chai.spy.sandbox()

series([
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb),
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb)
(cb) => createNode(cb),
(cb) => createNode(cb)
], (err, nodes) => {
if (err) return done(err)

Expand Down Expand Up @@ -310,8 +310,8 @@ describe('pubsub base protocol', () => {
sandbox = chai.spy.sandbox()

series([
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb),
(cb) => createNode('/ip4/127.0.0.1/tcp/0', cb)
(cb) => createNode(cb),
(cb) => createNode(cb)
], (err, nodes) => {
if (err) return done(err)

Expand Down
31 changes: 31 additions & 0 deletions test/utils/browser-bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const WebSocketStar = require('libp2p-websocket-star')
const spdy = require('libp2p-spdy')
const secio = require('libp2p-secio')
const libp2p = require('libp2p')

const { WS_STAR_MULTIADDR } = require('./constants')

class Node extends libp2p {
constructor ({ peerInfo, peerBook }) {
const starOpts = { id: peerInfo.id }
const wsStar = new WebSocketStar(starOpts)

peerInfo.multiaddrs.add(WS_STAR_MULTIADDR)

const modules = {
transport: [wsStar],
streamMuxer: [spdy],
connEncryption: [secio]
}

super({
modules,
peerInfo,
peerBook
})
}
}

module.exports = Node
40 changes: 40 additions & 0 deletions test/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const nextTick = require('async/nextTick')
const peerJSON = require('../fixtures/test-peer')
const multiaddr = require('multiaddr')

let peerRelay = null

/**
* Creates a `PeerInfo` that can be used across testing. Once the
* relay `PeerInfo` has been requested, it will be reused for each
* additional request.
*
* This is currently being used to create a relay on test bootstrapping
* so that it can be used by browser nodes during their test suite. This
* is necessary for running a TCP node during browser tests.
* @private
* @param {function(error, PeerInfo)} callback
* @returns {void}
*/
module.exports.getPeerRelay = (callback) => {
if (peerRelay) return nextTick(callback, null, peerRelay)

PeerId.createFromJSON(peerJSON, (err, peerId) => {
if (err) {
return callback(err)
}
peerRelay = new PeerInfo(peerId)

peerRelay.multiaddrs.add('/ip4/127.0.0.1/tcp/9200/ws')
peerRelay.multiaddrs.add('/ip4/127.0.0.1/tcp/9245')

callback(null, peerRelay)
})
}

module.exports.WS_STAR_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/')
module.exports.WS_RENDEZVOUS_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/14444/wss')
4 changes: 2 additions & 2 deletions test/utils.js → test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const Node = require('./nodejs-bundle')

const waterfall = require('async/waterfall')

exports.createNode = (maddr, callback) => {
exports.createNode = (callback) => {
waterfall([
(cb) => PeerId.create({ bits: 1024 }, cb),
(id, cb) => PeerInfo.create(id, cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add(maddr)
cb(null, new Node({ peerInfo }))
},
(node, cb) => node.start((err) => cb(err, node))
Expand Down
2 changes: 2 additions & 0 deletions test/nodejs-bundle.js → test/utils/nodejs-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Node extends libp2p {
connEncryption: [secio]
}

peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/0')

super({
modules,
peerInfo,
Expand Down

0 comments on commit e66a7df

Please sign in to comment.