Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
fix: spawn dialable nodes when testing with webworkers
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Feb 2, 2020
1 parent ec6af74 commit df7cb3a
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const { getDescribe, getIt, expect } = require('../utils/mocha')
const { waitForWantlistKey } = require('./utils')
const { isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -15,15 +16,18 @@ module.exports = (common, options) => {

describe('.bitswap.wantlist', function () {
this.timeout(60 * 1000)

let ipfsA
let ipfsB
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'

before(async () => {
ipfsA = (await common.spawn()).api
ipfsB = (await common.spawn()).api
// webworkers are not dialable because webrtc is not available
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
// Add key to the wantlist for ipfsB
ipfsB.block.get(key).catch(() => { /* is ok, expected on teardown */ })

await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
})

Expand Down
4 changes: 3 additions & 1 deletion src/miscellaneous/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const hat = require('hat')
const multibase = require('multibase')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const all = require('it-all')
const { isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand Down Expand Up @@ -82,7 +83,8 @@ module.exports = (common, options) => {

it('should resolve IPNS link recursively', async function () {
this.timeout(20 * 1000)
const node = (await common.spawn()).api
// webworkers are not dialable because webrtc is not available
const node = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
await ipfs.swarm.connect(node.peerId.addresses[0])
const [{ path }] = await all(ipfs.add(Buffer.from('should resolve a record recursive === true')))
const { id: keyId } = await ipfs.key.gen('key-name', { type: 'rsa', size: 2048 })
Expand Down
4 changes: 3 additions & 1 deletion src/ping/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { expectIsPingResponse, isPong } = require('./utils')
const all = require('it-all')
const { isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -22,7 +23,8 @@ module.exports = (common, options) => {

before(async () => {
ipfsA = (await common.spawn()).api
ipfsB = (await common.spawn()).api
// webworkers are not dialable because webrtc is not available
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
})

Expand Down
6 changes: 4 additions & 2 deletions src/pubsub/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const { waitForPeers, getTopic } = require('./utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const delay = require('delay')
const { isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -23,8 +24,9 @@ module.exports = (common, options) => {
let subscribedTopics = []
before(async () => {
ipfs1 = (await common.spawn()).api
ipfs2 = (await common.spawn()).api
ipfs3 = (await common.spawn()).api
// webworkers are not dialable because webrtc is not available
ipfs2 = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
ipfs3 = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api

const ipfs2Addr = ipfs2.peerId.addresses
.find(ma => ma.nodeAddress().address === '127.0.0.1')
Expand Down
5 changes: 4 additions & 1 deletion src/pubsub/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const all = require('it-all')
const { waitForPeers, getTopic } = require('./utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const delay = require('delay')
const { isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -28,7 +29,9 @@ module.exports = (common, options) => {
ipfs1 = (await common.spawn()).api
// TODO 'multiple connected nodes' tests fails with go in Firefox
// and JS is flaky everywhere
ipfs2 = (await common.spawn()).api

// webworkers are not dialable because webrtc is not available
ipfs2 = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
})

beforeEach(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/swarm/addrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const CID = require('cids')
const Multiaddr = require('multiaddr')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -22,7 +23,8 @@ module.exports = (common, options) => {

before(async () => {
ipfsA = (await common.spawn()).api
ipfsB = (await common.spawn()).api
// webworkers are not dialable because webrtc is not available
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
})

Expand Down
4 changes: 3 additions & 1 deletion src/swarm/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict'

const { getDescribe, getIt, expect } = require('../utils/mocha')
const { isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -19,7 +20,8 @@ module.exports = (common, options) => {

before(async () => {
ipfsA = (await common.spawn()).api
ipfsB = (await common.spawn()).api
// webworkers are not dialable because webrtc is not available
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
})

after(() => common.clean())
Expand Down
4 changes: 3 additions & 1 deletion src/swarm/disconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict'

const { getDescribe, getIt, expect } = require('../utils/mocha')
const { isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -20,7 +21,8 @@ module.exports = (common, options) => {

before(async () => {
ipfsA = (await common.spawn()).api
ipfsB = (await common.spawn()).api
// webworkers are not dialable because webrtc is not available
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
})

Expand Down
11 changes: 9 additions & 2 deletions src/swarm/local-addrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict'

const { getDescribe, getIt, expect } = require('../utils/mocha')
const { isWebWorker } = require('ipfs-utils/src/env')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -25,8 +26,14 @@ module.exports = (common, options) => {

it('should list local addresses the node is listening on', async () => {
const multiaddrs = await ipfs.swarm.localAddrs()
// js-ipfs in the browser will have zero
expect(Array.isArray(multiaddrs)).to.be.true()

expect(multiaddrs).to.be.an.instanceOf(Array)

if (isWebWorker) {
expect(multiaddrs).to.have.lengthOf(0)
} else {
expect(multiaddrs).to.not.be.empty()
}
})
})
}
44 changes: 31 additions & 13 deletions src/swarm/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const multiaddr = require('multiaddr')
const CID = require('cids')
const delay = require('delay')
const { isNode, isBrowser, isElectron } = require('ipfs-utils/src/env')
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')
const { getDescribe, getIt, expect } = require('../utils/mocha')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
Expand All @@ -24,7 +24,7 @@ module.exports = (common, options) => {

before(async () => {
ipfsA = (await common.spawn()).api
ipfsB = (await common.spawn()).api
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
/* TODO: Seen if we still need this after this is fixed
https://github.com/ipfs/js-ipfs/issues/2601 gets resolved */
Expand Down Expand Up @@ -88,7 +88,7 @@ module.exports = (common, options) => {

it('should list peers only once', async () => {
const nodeA = (await common.spawn()).api
const nodeB = (await common.spawn()).api
const nodeB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
await nodeA.swarm.connect(nodeB.peerId.addresses[0])
await delay(1000)
const peersA = await nodeA.swarm.peers()
Expand All @@ -99,22 +99,40 @@ module.exports = (common, options) => {

it('should list peers only once even if they have multiple addresses', async () => {
// TODO: Change to port 0, needs: https://github.com/ipfs/interface-ipfs-core/issues/152
const configA = getConfig(isNode || isElectron || (common.opts && common.opts.type === 'go') ? [
'/ip4/127.0.0.1/tcp/16543',
'/ip4/127.0.0.1/tcp/16544'
] : [
let addresses

if (isBrowser) {
addresses = [
'/ip4/127.0.0.1/tcp/14578/ws/p2p-webrtc-star',
'/ip4/127.0.0.1/tcp/14579/ws/p2p-webrtc-star'
]
} else if (isWebWorker) {
// webworkers are not dialable (no webrtc available) until stardust is async/await
// https://github.com/libp2p/js-libp2p-stardust/pull/14
addresses = []
} else {
addresses = [
'/ip4/127.0.0.1/tcp/26543/ws',
'/ip4/127.0.0.1/tcp/26544/ws'
]
}

const configA = getConfig(addresses)
const configB = getConfig(isBrowser ? [
'/ip4/127.0.0.1/tcp/14578/ws/p2p-webrtc-star',
'/ip4/127.0.0.1/tcp/14579/ws/p2p-webrtc-star'
])
const configB = getConfig(isNode || isElectron || (common.opts && common.opts.type === 'go') ? [
] : [
'/ip4/127.0.0.1/tcp/26545/ws',
'/ip4/127.0.0.1/tcp/26546/ws'
] : [
'/ip4/127.0.0.1/tcp/14578/ws/p2p-webrtc-star',
'/ip4/127.0.0.1/tcp/14579/ws/p2p-webrtc-star'
])

const nodeA = (await common.spawn({ ipfsOptions: { config: configA } })).api
const nodeB = (await common.spawn({ ipfsOptions: { config: configB } })).api
const nodeB = (await common.spawn({
type: isWebWorker ? 'go' : undefined,
ipfsOptions: {
config: configB
}
})).api

// TODO: the webrtc-star transport only keeps the last listened on address around
// so the browser has to use 1 as the array index
Expand Down

0 comments on commit df7cb3a

Please sign in to comment.