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

Commit df7cb3a

Browse files
committed
fix: spawn dialable nodes when testing with webworkers
1 parent ec6af74 commit df7cb3a

File tree

10 files changed

+68
-24
lines changed

10 files changed

+68
-24
lines changed

src/bitswap/wantlist.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

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

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

1617
describe('.bitswap.wantlist', function () {
1718
this.timeout(60 * 1000)
19+
1820
let ipfsA
1921
let ipfsB
2022
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'
2123

2224
before(async () => {
2325
ipfsA = (await common.spawn()).api
24-
ipfsB = (await common.spawn()).api
26+
// webworkers are not dialable because webrtc is not available
27+
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
2528
// Add key to the wantlist for ipfsB
2629
ipfsB.block.get(key).catch(() => { /* is ok, expected on teardown */ })
30+
2731
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
2832
})
2933

src/miscellaneous/resolve.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const hat = require('hat')
77
const multibase = require('multibase')
88
const { getDescribe, getIt, expect } = require('../utils/mocha')
99
const all = require('it-all')
10+
const { isWebWorker } = require('ipfs-utils/src/env')
1011

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

8384
it('should resolve IPNS link recursively', async function () {
8485
this.timeout(20 * 1000)
85-
const node = (await common.spawn()).api
86+
// webworkers are not dialable because webrtc is not available
87+
const node = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
8688
await ipfs.swarm.connect(node.peerId.addresses[0])
8789
const [{ path }] = await all(ipfs.add(Buffer.from('should resolve a record recursive === true')))
8890
const { id: keyId } = await ipfs.key.gen('key-name', { type: 'rsa', size: 2048 })

src/ping/ping.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const { getDescribe, getIt, expect } = require('../utils/mocha')
55
const { expectIsPingResponse, isPong } = require('./utils')
66
const all = require('it-all')
7+
const { isWebWorker } = require('ipfs-utils/src/env')
78

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

2324
before(async () => {
2425
ipfsA = (await common.spawn()).api
25-
ipfsB = (await common.spawn()).api
26+
// webworkers are not dialable because webrtc is not available
27+
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
2628
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
2729
})
2830

src/pubsub/peers.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const { waitForPeers, getTopic } = require('./utils')
55
const { getDescribe, getIt, expect } = require('../utils/mocha')
66
const delay = require('delay')
7+
const { isWebWorker } = require('ipfs-utils/src/env')
78

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

2931
const ipfs2Addr = ipfs2.peerId.addresses
3032
.find(ma => ma.nodeAddress().address === '127.0.0.1')

src/pubsub/subscribe.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const all = require('it-all')
66
const { waitForPeers, getTopic } = require('./utils')
77
const { getDescribe, getIt, expect } = require('../utils/mocha')
88
const delay = require('delay')
9+
const { isWebWorker } = require('ipfs-utils/src/env')
910

1011
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
1112
/**
@@ -28,7 +29,9 @@ module.exports = (common, options) => {
2829
ipfs1 = (await common.spawn()).api
2930
// TODO 'multiple connected nodes' tests fails with go in Firefox
3031
// and JS is flaky everywhere
31-
ipfs2 = (await common.spawn()).api
32+
33+
// webworkers are not dialable because webrtc is not available
34+
ipfs2 = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
3235
})
3336

3437
beforeEach(() => {

src/swarm/addrs.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const CID = require('cids')
55
const Multiaddr = require('multiaddr')
66
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
const { isWebWorker } = require('ipfs-utils/src/env')
78

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

2324
before(async () => {
2425
ipfsA = (await common.spawn()).api
25-
ipfsB = (await common.spawn()).api
26+
// webworkers are not dialable because webrtc is not available
27+
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
2628
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
2729
})
2830

src/swarm/connect.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

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

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

2021
before(async () => {
2122
ipfsA = (await common.spawn()).api
22-
ipfsB = (await common.spawn()).api
23+
// webworkers are not dialable because webrtc is not available
24+
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
2325
})
2426

2527
after(() => common.clean())

src/swarm/disconnect.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

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

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

2122
before(async () => {
2223
ipfsA = (await common.spawn()).api
23-
ipfsB = (await common.spawn()).api
24+
// webworkers are not dialable because webrtc is not available
25+
ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
2426
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
2527
})
2628

src/swarm/local-addrs.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

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

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

2627
it('should list local addresses the node is listening on', async () => {
2728
const multiaddrs = await ipfs.swarm.localAddrs()
28-
// js-ipfs in the browser will have zero
29-
expect(Array.isArray(multiaddrs)).to.be.true()
29+
30+
expect(multiaddrs).to.be.an.instanceOf(Array)
31+
32+
if (isWebWorker) {
33+
expect(multiaddrs).to.have.lengthOf(0)
34+
} else {
35+
expect(multiaddrs).to.not.be.empty()
36+
}
3037
})
3138
})
3239
}

src/swarm/peers.js

+31-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const multiaddr = require('multiaddr')
55
const CID = require('cids')
66
const delay = require('delay')
7-
const { isNode, isBrowser, isElectron } = require('ipfs-utils/src/env')
7+
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')
88
const { getDescribe, getIt, expect } = require('../utils/mocha')
99

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

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

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

100100
it('should list peers only once even if they have multiple addresses', async () => {
101101
// TODO: Change to port 0, needs: https://github.com/ipfs/interface-ipfs-core/issues/152
102-
const configA = getConfig(isNode || isElectron || (common.opts && common.opts.type === 'go') ? [
103-
'/ip4/127.0.0.1/tcp/16543',
104-
'/ip4/127.0.0.1/tcp/16544'
105-
] : [
102+
let addresses
103+
104+
if (isBrowser) {
105+
addresses = [
106+
'/ip4/127.0.0.1/tcp/14578/ws/p2p-webrtc-star',
107+
'/ip4/127.0.0.1/tcp/14579/ws/p2p-webrtc-star'
108+
]
109+
} else if (isWebWorker) {
110+
// webworkers are not dialable (no webrtc available) until stardust is async/await
111+
// https://github.com/libp2p/js-libp2p-stardust/pull/14
112+
addresses = []
113+
} else {
114+
addresses = [
115+
'/ip4/127.0.0.1/tcp/26543/ws',
116+
'/ip4/127.0.0.1/tcp/26544/ws'
117+
]
118+
}
119+
120+
const configA = getConfig(addresses)
121+
const configB = getConfig(isBrowser ? [
106122
'/ip4/127.0.0.1/tcp/14578/ws/p2p-webrtc-star',
107123
'/ip4/127.0.0.1/tcp/14579/ws/p2p-webrtc-star'
108-
])
109-
const configB = getConfig(isNode || isElectron || (common.opts && common.opts.type === 'go') ? [
124+
] : [
110125
'/ip4/127.0.0.1/tcp/26545/ws',
111126
'/ip4/127.0.0.1/tcp/26546/ws'
112-
] : [
113-
'/ip4/127.0.0.1/tcp/14578/ws/p2p-webrtc-star',
114-
'/ip4/127.0.0.1/tcp/14579/ws/p2p-webrtc-star'
115127
])
128+
116129
const nodeA = (await common.spawn({ ipfsOptions: { config: configA } })).api
117-
const nodeB = (await common.spawn({ ipfsOptions: { config: configB } })).api
130+
const nodeB = (await common.spawn({
131+
type: isWebWorker ? 'go' : undefined,
132+
ipfsOptions: {
133+
config: configB
134+
}
135+
})).api
118136

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

0 commit comments

Comments
 (0)