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

Commit 044409c

Browse files
authored
feat: add support for async/await setup in the swarm tests (#529)
BREAKING CHANGE: swarm tests need promise based setup check PRs below the following PRs add support async setup: ipfs-inactive/js-ipfs-http-client#1105 ipfs/js-ipfs#2428
2 parents da79067 + 78d7169 commit 044409c

File tree

8 files changed

+50
-161
lines changed

8 files changed

+50
-161
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"chai": "^4.2.0",
4343
"cids": "~0.7.1",
4444
"concat-stream": "^2.0.0",
45+
"delay": "^4.3.0",
4546
"dirty-chai": "^2.0.1",
4647
"es6-promisify": "^6.0.1",
4748
"hat": "0.0.3",

src/miscellaneous/id.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ module.exports = (createCommon, options) => {
88
const it = getIt(options)
99
const common = createCommon()
1010

11-
describe('.id', () => {
11+
describe('.id', function () {
12+
this.timeout(60 * 1000)
1213
let ipfs
1314

1415
before(function (done) {
15-
// CI takes longer to instantiate the daemon, so we need to increase the
16-
// timeout for the before step
17-
this.timeout(60 * 1000)
18-
1916
common.setup((err, factory) => {
2017
expect(err).to.not.exist()
2118
factory.spawnNode((err, node) => {

src/miscellaneous/resolve.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = (createCommon, options) => {
1515
const it = getIt(options)
1616
const common = createCommon()
1717

18-
describe('.resolve', () => {
18+
describe('.resolve', function () {
19+
this.timeout(60 * 1000)
1920
let ipfs
2021
let nodeId
2122

src/swarm/addrs.js

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

44
const PeerInfo = require('peer-info')
55
const { getDescribe, getIt, expect } = require('../utils/mocha')
6-
const { spawnNodesWithId } = require('../utils/spawn')
76

87
module.exports = (createCommon, options) => {
98
const describe = getDescribe(options)
@@ -13,26 +12,16 @@ module.exports = (createCommon, options) => {
1312
describe('.swarm.addrs', function () {
1413
this.timeout(80 * 1000)
1514

16-
let ipfsA, ipfsB
15+
let ipfsA
16+
let ipfsB
1717

18-
before(function (done) {
19-
// CI takes longer to instantiate the daemon, so we need to increase the
20-
// timeout for the before step
21-
this.timeout(100 * 1000)
22-
23-
common.setup((err, factory) => {
24-
expect(err).to.not.exist()
25-
26-
spawnNodesWithId(2, factory, (err, nodes) => {
27-
expect(err).to.not.exist()
28-
ipfsA = nodes[0]
29-
ipfsB = nodes[1]
30-
ipfsA.swarm.connect(ipfsB.peerId.addresses[0], done)
31-
})
32-
})
18+
before(async () => {
19+
ipfsA = await common.setup()
20+
ipfsB = await common.setup()
21+
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
3322
})
3423

35-
after((done) => common.teardown(done))
24+
after(() => common.teardown())
3625

3726
it('should get a list of node addresses', (done) => {
3827
ipfsA.swarm.addrs((err, peerInfos) => {

src/swarm/connect.js

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { spawnNodesWithId } = require('../utils/spawn')
5-
const { getDescribe, getIt, expect } = require('../utils/mocha')
4+
const { getDescribe, getIt } = require('../utils/mocha')
65

76
module.exports = (createCommon, options) => {
87
const describe = getDescribe(options)
@@ -11,28 +10,15 @@ module.exports = (createCommon, options) => {
1110

1211
describe('.swarm.connect', function () {
1312
this.timeout(80 * 1000)
14-
1513
let ipfsA
1614
let ipfsB
1715

18-
before(function (done) {
19-
// CI takes longer to instantiate the daemon, so we need to increase the
20-
// timeout for the before step
21-
this.timeout(100 * 1000)
22-
23-
common.setup((err, factory) => {
24-
expect(err).to.not.exist()
25-
26-
spawnNodesWithId(2, factory, (err, nodes) => {
27-
expect(err).to.not.exist()
28-
ipfsA = nodes[0]
29-
ipfsB = nodes[1]
30-
done()
31-
})
32-
})
16+
before(async () => {
17+
ipfsA = await common.setup()
18+
ipfsB = await common.setup()
3319
})
3420

35-
after((done) => common.teardown(done))
21+
after(() => common.teardown())
3622

3723
it('should connect to a peer', (done) => {
3824
ipfsA.swarm.connect(ipfsB.peerId.addresses[0], done)

src/swarm/disconnect.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { spawnNodesWithId } = require('../utils/spawn')
5-
const { getDescribe, getIt, expect } = require('../utils/mocha')
4+
const { getDescribe, getIt } = require('../utils/mocha')
65

76
module.exports = (createCommon, options) => {
87
const describe = getDescribe(options)
@@ -15,24 +14,13 @@ module.exports = (createCommon, options) => {
1514
let ipfsA
1615
let ipfsB
1716

18-
before(function (done) {
19-
// CI takes longer to instantiate the daemon, so we need to increase the
20-
// timeout for the before step
21-
this.timeout(100 * 1000)
22-
23-
common.setup((err, factory) => {
24-
expect(err).to.not.exist()
25-
26-
spawnNodesWithId(2, factory, (err, nodes) => {
27-
expect(err).to.not.exist()
28-
ipfsA = nodes[0]
29-
ipfsB = nodes[1]
30-
ipfsA.swarm.connect(ipfsB.peerId.addresses[0], done)
31-
})
32-
})
17+
before(async () => {
18+
ipfsA = await common.setup()
19+
ipfsB = await common.setup()
20+
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
3321
})
3422

35-
after((done) => common.teardown(done))
23+
after(() => common.teardown())
3624

3725
it('should disconnect from a peer', (done) => {
3826
ipfsA.swarm.disconnect(ipfsB.peerId.addresses[0], done)

src/swarm/local-addrs.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,11 @@ module.exports = (createCommon, options) => {
1313

1414
let ipfs
1515

16-
before(function (done) {
17-
// CI takes longer to instantiate the daemon, so we need to increase the
18-
// timeout for the before step
19-
this.timeout(100 * 1000)
20-
21-
common.setup((err, factory) => {
22-
expect(err).to.not.exist()
23-
factory.spawnNode((err, node) => {
24-
expect(err).to.not.exist()
25-
ipfs = node
26-
done()
27-
})
28-
})
16+
before(async () => {
17+
ipfs = await common.setup()
2918
})
3019

31-
after((done) => common.teardown(done))
20+
after(() => common.teardown())
3221

3322
it('should list local addresses the node is listening on', (done) => {
3423
ipfs.swarm.localAddrs((err, multiaddrs) => {

src/swarm/peers.js

+24-86
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const auto = require('async/auto')
54
const multiaddr = require('multiaddr')
65
const PeerId = require('peer-id')
7-
const os = require('os')
8-
const path = require('path')
9-
const hat = require('hat')
10-
const { spawnNodesWithId } = require('../utils/spawn')
6+
const delay = require('delay')
117
const { getDescribe, getIt, expect } = require('../utils/mocha')
128

139
module.exports = (createCommon, options) => {
@@ -20,27 +16,14 @@ module.exports = (createCommon, options) => {
2016

2117
let ipfsA
2218
let ipfsB
23-
let ipfsFactory
2419

25-
before(function (done) {
26-
// CI takes longer to instantiate the daemon, so we need to increase the
27-
// timeout for the before step
28-
this.timeout(100 * 1000)
29-
30-
common.setup((err, factory) => {
31-
expect(err).to.not.exist()
32-
ipfsFactory = factory
33-
34-
spawnNodesWithId(2, factory, (err, nodes) => {
35-
expect(err).to.not.exist()
36-
ipfsA = nodes[0]
37-
ipfsB = nodes[1]
38-
ipfsA.swarm.connect(ipfsB.peerId.addresses[0], done)
39-
})
40-
})
20+
before(async () => {
21+
ipfsA = await common.setup()
22+
ipfsB = await common.setup()
23+
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
4124
})
4225

43-
after((done) => common.teardown(done))
26+
after(() => common.teardown())
4427

4528
it('should list peers this node is connected to', (done) => {
4629
ipfsA.swarm.peers((err, peers) => {
@@ -119,44 +102,20 @@ module.exports = (createCommon, options) => {
119102
}
120103
}
121104

122-
function getRepoPath () {
123-
return path.join(os.tmpdir(), '.ipfs-' + hat())
124-
}
125-
126-
it('should list peers only once', (done) => {
105+
it('should list peers only once', async () => {
127106
const config = getConfig(['/ip4/127.0.0.1/tcp/0'])
128107

129-
auto({
130-
nodeA: (cb) => ipfsFactory.spawnNode(getRepoPath(), config, cb),
131-
nodeB: ['nodeA', (_, cb) => {
132-
ipfsFactory.spawnNode(getRepoPath(), config, cb)
133-
}],
134-
nodeBAddress: ['nodeB', (res, cb) => {
135-
res.nodeB.id((err, info) => {
136-
if (err) return cb(err)
137-
cb(null, info.addresses[0])
138-
})
139-
}],
140-
connectA2B: ['nodeA', 'nodeBAddress', (res, cb) => {
141-
res.nodeA.swarm.connect(res.nodeBAddress, cb)
142-
}],
143-
// time for identify
144-
wait: ['connectA2B', (_, cb) => setTimeout(cb, 1000)],
145-
nodeAPeers: ['nodeA', 'wait', (res, cb) => {
146-
res.nodeA.swarm.peers(cb)
147-
}],
148-
nodeBPeers: ['nodeB', 'wait', (res, cb) => {
149-
res.nodeB.swarm.peers(cb)
150-
}]
151-
}, (err, res) => {
152-
expect(err).to.not.exist()
153-
expect(res.nodeAPeers).to.have.length(1)
154-
expect(res.nodeBPeers).to.have.length(1)
155-
done()
156-
})
108+
const nodeA = await common.setup({}, { config })
109+
const nodeB = await common.setup({}, { config })
110+
await nodeA.swarm.connect(nodeB.peerId.addresses[0])
111+
await delay(1000)
112+
const peersA = await nodeA.swarm.peers()
113+
const peersB = await nodeB.swarm.peers()
114+
expect(peersA).to.have.length(1)
115+
expect(peersB).to.have.length(1)
157116
})
158117

159-
it('should list peers only once even if they have multiple addresses', (done) => {
118+
it('should list peers only once even if they have multiple addresses', async () => {
160119
// TODO: Change to port 0, needs: https://github.com/ipfs/interface-ipfs-core/issues/152
161120
const configA = getConfig([
162121
'/ip4/127.0.0.1/tcp/16543',
@@ -166,35 +125,14 @@ module.exports = (createCommon, options) => {
166125
'/ip4/127.0.0.1/tcp/26545',
167126
'/ip4/127.0.0.1/tcp/26546'
168127
])
169-
170-
auto({
171-
nodeA: (cb) => ipfsFactory.spawnNode(getRepoPath(), configA, cb),
172-
nodeB: ['nodeA', (_, cb) => {
173-
ipfsFactory.spawnNode(getRepoPath(), configB, cb)
174-
}],
175-
nodeBAddress: ['nodeB', (res, cb) => {
176-
res.nodeB.id((err, info) => {
177-
if (err) return cb(err)
178-
cb(null, info.addresses[0])
179-
})
180-
}],
181-
connectA2B: ['nodeA', 'nodeBAddress', (res, cb) => {
182-
res.nodeA.swarm.connect(res.nodeBAddress, cb)
183-
}],
184-
// time for identify
185-
wait: ['connectA2B', (_, cb) => setTimeout(cb, 1000)],
186-
nodeAPeers: ['nodeA', 'wait', (res, cb) => {
187-
res.nodeA.swarm.peers(cb)
188-
}],
189-
nodeBPeers: ['nodeB', 'wait', (res, cb) => {
190-
res.nodeB.swarm.peers(cb)
191-
}]
192-
}, (err, res) => {
193-
expect(err).to.not.exist()
194-
expect(res.nodeAPeers).to.have.length(1)
195-
expect(res.nodeBPeers).to.have.length(1)
196-
done()
197-
})
128+
const nodeA = await common.setup({}, { configA })
129+
const nodeB = await common.setup({}, { configB })
130+
await nodeA.swarm.connect(nodeB.peerId.addresses[0])
131+
await delay(1000)
132+
const peersA = await nodeA.swarm.peers()
133+
const peersB = await nodeB.swarm.peers()
134+
expect(peersA).to.have.length(1)
135+
expect(peersB).to.have.length(1)
198136
})
199137
})
200138
}

0 commit comments

Comments
 (0)