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

Commit 2ede15d

Browse files
Pedro SantosAlan Shaw
Pedro Santos
authored and
Alan Shaw
committed
refactor: convert tests to async/await (#562)
* chore: bitswap async/await refactor * chore: block async/await refactor * chore: bootstrap async/await refactor * chore: config async/await refactor * chore: dag async/await refactor * chore: dht async/await refactor * chore: files-mfs async/await refactor * chore: files-regular async/await refactor * chore: key async/await refactor * chore: miscellaneous async/await refactor * chore: name-pubsub async/await refactor * chore: name async/await refactor * chore: object async/await refactor * chore: pin async/await refactor * chore: ping async/await refactor * chore: pubsub async/await refactor * chore: repo async/await refactor * chore: stats async/await refactor * chore: swarm async/await refactor * chore: remove unnecessary util file * chore: update dependencies * refactor: remove async dep and unnecessary utils * refactor: bitswap before and after methods to async syntax * refactor: block before and after methods to async syntax * refactor: bootstrap before and after methods to async syntax * refactor: config before and after methods to async syntax * refactor: dag before and after methods to async syntax * refactor: dht before and after methods to async syntax * refactor: files-mfs before and after methods to async syntax * refactor: files-regular before and after methods to async syntax * refactor: key before and after methods to async syntax * refactor: miscellaneous before and after methods to async syntax * refactor: name before and after methods to async syntax * refactor: name-pubsub before and after methods to async syntax * refactor: object before and after methods to async syntax * refactor: pin before and after methods to async syntax * refactor: ping before and after methods to async syntax * refactor: pubsub before and after methods to async syntax * refactor: repo before and after methods to async syntax * refactor: stats before and after methods to async syntax * chore: remove 'only' from files-mfs/cp * chore: uncomment bootstrap rm test assertion The assertion was commented out temporarly due to bootstrap.rm bug that was fixed here ipfs/js-ipfs#2626 * refactor: name resolve test * fix: increase dht findProvs test timeout * fix: miscellaneous stop test
1 parent c766dbf commit 2ede15d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+2627
-5943
lines changed

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
},
3737
"homepage": "https://github.com/ipfs/interface-ipfs-core#readme",
3838
"dependencies": {
39-
"async": "^2.6.2",
4039
"bl": "^3.0.0",
4140
"bs58": "^4.0.1",
4241
"callbackify": "^1.1.0",
@@ -47,6 +46,7 @@
4746
"delay": "^4.3.0",
4847
"dirty-chai": "^2.0.1",
4948
"es6-promisify": "^6.0.1",
49+
"get-stream": "^5.1.0",
5050
"hat": "0.0.3",
5151
"ipfs-block": "~0.8.0",
5252
"ipfs-unixfs": "~0.1.16",
@@ -61,9 +61,15 @@
6161
"multibase": "~0.6.0",
6262
"multihashes": "~0.4.14",
6363
"multihashing-async": "~0.6.0",
64-
"peer-id": "~0.12.0",
64+
"p-each-series": "^2.1.0",
65+
"p-map-series": "^2.1.0",
66+
"p-timeout": "^3.2.0",
67+
"p-times": "^2.1.0",
68+
"p-whilst": "^2.1.0",
69+
"peer-id": "~0.13.5",
6570
"peer-info": "~0.15.0",
6671
"pull-stream": "^3.6.11",
72+
"pull-to-promise": "^1.0.1",
6773
"pump": "^3.0.0",
6874
"readable-stream": "^3.1.1",
6975
"streaming-iterables": "^4.1.0",

src/bitswap/stat.js

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

4-
const waterfall = require('async/waterfall')
54
const { getDescribe, getIt, expect } = require('../utils/mocha')
65
const { expectIsBitswap } = require('../stats/utils')
76

@@ -10,53 +9,26 @@ module.exports = (createCommon, options) => {
109
const it = getIt(options)
1110
const common = createCommon()
1211

13-
describe('.bitswap.stat', () => {
12+
describe('.bitswap.stat', function () {
13+
this.timeout(60 * 1000)
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(60 * 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

33-
it('should get bitswap stats', (done) => {
34-
ipfs.bitswap.stat((err, res) => {
35-
expectIsBitswap(err, res)
36-
done()
37-
})
22+
it('should get bitswap stats', async () => {
23+
const res = await ipfs.bitswap.stat()
24+
expectIsBitswap(null, res)
3825
})
3926

40-
it('should get bitswap stats (promised)', () => {
41-
return ipfs.bitswap.stat().then((res) => {
42-
expectIsBitswap(null, res)
43-
})
44-
})
27+
it('should not get bitswap stats when offline', async () => {
28+
const node = await createCommon().setup()
29+
await node.stop()
4530

46-
it('should not get bitswap stats when offline', function (done) {
47-
this.timeout(60 * 1000)
48-
49-
waterfall([
50-
(cb) => createCommon().setup(cb),
51-
(factory, cb) => factory.spawnNode(cb),
52-
(node, cb) => node.stop((err) => cb(err, node))
53-
], (err, node) => {
54-
expect(err).to.not.exist()
55-
node.bitswap.wantlist((err) => {
56-
expect(err).to.exist()
57-
done()
58-
})
59-
})
31+
return expect(node.bitswap.stat()).to.eventually.be.rejected()
6032
})
6133
})
6234
}

src/bitswap/utils.js

+12-23
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11
'use strict'
22

3-
const until = require('async/until')
3+
const pWhilst = require('p-whilst')
44

5-
function waitForWantlistKey (ipfs, key, opts, cb) {
6-
if (typeof opts === 'function') {
7-
cb = opts
8-
opts = {}
9-
}
10-
11-
opts = opts || {}
12-
opts.timeout = opts.timeout || 1000
5+
function waitForWantlistKey (ipfs, key, opts = {}) {
6+
opts.timeout = opts.timeout || 10000
137

148
let list = { Keys: [] }
15-
let timedOut = false
169

17-
setTimeout(() => { timedOut = true }, opts.timeout)
10+
const start = Date.now()
11+
const findKey = () => !list.Keys.some(k => k['/'] === key)
12+
13+
const iteratee = async () => {
14+
if (Date.now() - start > opts.timeout) {
15+
throw new Error(`Timed out waiting for ${key} in wantlist`)
16+
}
1817

19-
const test = () => timedOut ? true : list.Keys.some(k => k['/'] === key)
20-
const iteratee = (cb) => {
21-
ipfs.bitswap.wantlist(opts.peerId, (err, nextList) => {
22-
if (err) return cb(err)
23-
list = nextList
24-
cb()
25-
})
18+
list = await ipfs.bitswap.wantlist(opts.peerId)
2619
}
2720

28-
until(test, iteratee, (err) => {
29-
if (err) return cb(err)
30-
if (timedOut) return cb(new Error(`Timed out waiting for ${key} in wantlist`))
31-
cb()
32-
})
21+
return pWhilst(findKey, iteratee)
3322
}
3423

3524
module.exports.waitForWantlistKey = waitForWantlistKey

src/bitswap/wantlist.js

+17-46
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,45 @@
22
/* eslint max-nested-callbacks: ["error", 6] */
33
'use strict'
44

5-
const waterfall = require('async/waterfall')
6-
const { spawnNodesWithId } = require('../utils/spawn')
75
const { getDescribe, getIt, expect } = require('../utils/mocha')
86
const { waitForWantlistKey } = require('./utils')
9-
const { connect } = require('../utils/swarm')
107

118
module.exports = (createCommon, options) => {
129
const describe = getDescribe(options)
1310
const it = getIt(options)
1411
const common = createCommon()
1512

16-
describe('.bitswap.wantlist', () => {
13+
describe('.bitswap.wantlist', function () {
14+
this.timeout(60 * 1000)
1715
let ipfsA
1816
let ipfsB
1917
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'
2018

21-
before(function (done) {
22-
// CI takes longer to instantiate the daemon, so we need to increase the
23-
// timeout for the before step
24-
this.timeout(60 * 1000)
19+
before(async () => {
20+
ipfsA = await common.setup()
21+
ipfsB = await common.setup()
2522

26-
common.setup((err, factory) => {
27-
expect(err).to.not.exist()
23+
// Add key to the wantlist for ipfsB
24+
ipfsB.block.get(key, () => {})
2825

29-
spawnNodesWithId(2, factory, (err, nodes) => {
30-
expect(err).to.not.exist()
31-
32-
ipfsA = nodes[0]
33-
ipfsB = nodes[1]
34-
35-
// Add key to the wantlist for ipfsB
36-
ipfsB.block.get(key, () => {})
37-
38-
connect(ipfsA, ipfsB.peerId.addresses[0], done)
39-
})
40-
})
26+
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
4127
})
4228

43-
after(function (done) {
44-
this.timeout(30 * 1000)
45-
common.teardown(done)
46-
})
29+
after(() => common.teardown())
4730

48-
it('should get the wantlist', (done) => {
49-
waitForWantlistKey(ipfsB, key, done)
31+
it('should get the wantlist', () => {
32+
return waitForWantlistKey(ipfsB, key)
5033
})
5134

52-
it('should get the wantlist by peer ID for a diffreent node', (done) => {
53-
ipfsB.id((err, info) => {
54-
expect(err).to.not.exist()
55-
waitForWantlistKey(ipfsA, key, { peerId: info.id }, done)
56-
})
35+
it('should get the wantlist by peer ID for a diffreent node', () => {
36+
return waitForWantlistKey(ipfsA, key, { peerId: ipfsB.peerId.id })
5737
})
5838

59-
it('should not get the wantlist when offline', function (done) {
60-
this.timeout(60 * 1000)
39+
it('should not get the wantlist when offline', async () => {
40+
const node = await createCommon().setup()
41+
await node.stop()
6142

62-
waterfall([
63-
(cb) => createCommon().setup(cb),
64-
(factory, cb) => factory.spawnNode(cb),
65-
(node, cb) => node.stop((err) => cb(err, node))
66-
], (err, node) => {
67-
expect(err).to.not.exist()
68-
node.bitswap.wantlist((err) => {
69-
expect(err).to.exist()
70-
done()
71-
})
72-
})
43+
return expect(node.bitswap.stat()).to.eventually.be.rejected()
7344
})
7445
})
7546
}

0 commit comments

Comments
 (0)