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

Commit 974a9eb

Browse files
author
Alan Shaw
committed
refactor: convert bitswap tests to use async/await
NOTE: the PRs for ipfs/js-ipfs#1670 are being merged into the `feat/async-iterators` branch. Once they're all in we can merge them into master. --- This simply refactors the bitswap tests to use async/await and updates the documentation to remove callback support and propose a better method for documenting the parameters and return values. depends on #390 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 8483084 commit 974a9eb

File tree

6 files changed

+79
-127
lines changed

6 files changed

+79
-127
lines changed

SPEC/BITSWAP.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,59 @@
55

66
### `bitswap.wantlist`
77

8-
> Returns the wantlist, optionally filtered by peer ID
8+
> Get the list of wanted CIDs for this peer or another peer on the network.
99
10-
#### `Go` **WIP**
10+
#### Go **WIP**
1111

12-
#### `JavaScript` - ipfs.bitswap.wantlist([peerId], [callback])
12+
#### JavaScript - `ipfs.bitswap.wantlist([peerId])`
1313

14-
`callback` must follow `function (err, list) {}` signature, where `err` is an error if the operation was not successful. `list` is an Object containing the following keys:
14+
##### Parameters
1515

16-
- `Keys` An array of objects containing the following keys:
17-
- `/` A string multihash
16+
| Name | Type | Description |
17+
|------|------|-------------|
18+
| peerId | `String` | (optional) Base 58 encoded Peer ID to get the wantlist for. Default: this node's peer ID |
1819

19-
If no `callback` is passed, a promise is returned.
20+
##### Returns
2021

21-
**Example:**
22+
| Type | Description |
23+
|------|-------------|
24+
| `Promise<{`<br/>&nbsp;&nbsp;`Keys<Array<Object>>`<br/>`}>` | The list of CIDs wanted by the peer. Each object in the array has a single property "/" a string CID. |
2225

23-
```JavaScript
24-
ipfs.bitswap.wantlist((err, list) => console.log(list))
26+
##### Example
2527

26-
// { Keys: [{ '/': 'QmHash' }] }
28+
```js
29+
const list = await ipfs.bitswap.wantlist()
30+
console.log(list) // { Keys: [{ '/': 'QmHash' }] }
31+
```
2732

28-
ipfs.bitswap.wantlist(peerId, (err, list) => console.log(list))
33+
Wantlist for a given peer:
2934

30-
// { Keys: [{ '/': 'QmHash' }] }
35+
```js
36+
ipfs.bitswap.wantlist('QmZEYeEin6wEB7WNyiT7stYTmbYFGy7BzM7T3hRDzRxTvY')
37+
console.log(list) // { Keys: [{ '/': 'QmHash' }] }
3138
```
3239

3340
#### `bitswap.stat`
3441

3542
> Show diagnostic information on the bitswap agent.
3643
37-
##### `Go` **WIP**
44+
##### Go **WIP**
3845

39-
##### `JavaScript` - ipfs.bitswap.stat([callback])
46+
##### JavaScript - `ipfs.bitswap.stat()`
4047

4148
Note: `bitswap.stat` and `stats.bitswap` can be used interchangeably.
4249

43-
`callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful. `stats` is an Object containing the following keys:
44-
45-
- `provideBufLen` is an integer.
46-
- `wantlist` (array of CIDs)
47-
- `peers` (array of peer IDs)
48-
- `blocksReceived` is a [Big Int][1]
49-
- `dataReceived` is a [Big Int][1]
50-
- `blocksSent` is a [Big Int][1]
51-
- `dataSent` is a [Big Int][1]
52-
- `dupBlksReceived` is a [Big Int][1]
53-
- `dupDataReceived` is a [Big Int][1]
54-
55-
If no `callback` is passed, a promise is returned.
50+
##### Returns
5651

57-
**Example:**
52+
| Type | Description |
53+
|------|-------------|
54+
| `Promise<{`<br/>&nbsp;&nbsp;`provideBufLen<Number>,`<br/>&nbsp;&nbsp;`wantlist<Array<Object>>,`<br/>&nbsp;&nbsp;`peers<Array<String>>,`<br/>&nbsp;&nbsp;`blocksReceived<`[`Big`][1]`>,`<br/>&nbsp;&nbsp;`dataReceived<`[`Big`][1]`>,`<br/>&nbsp;&nbsp;`blocksSent<`[`Big`][1]`>,`<br/>&nbsp;&nbsp;`dataSent<`[`Big`][1]`>,`<br/>&nbsp;&nbsp;`dupBlksReceived<`[`Big`][1]`>,`<br/>&nbsp;&nbsp;`dupDataReceived<`[`Big`][1]`>,`<br/>`}>` | Diagnostic information on the bitswap agent |
5855

59-
```JavaScript
60-
ipfs.bitswap.stat((err, stats) => console.log(stats))
56+
##### Example
6157

58+
```js
59+
const stats = await ipfs.bitswap.stat()
60+
console.log(stats)
6261
// { provideBufLen: 0,
6362
// wantlist: [ { '/': 'QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM' } ],
6463
// peers:

js/src/bitswap/.tern-port

Lines changed: 0 additions & 1 deletion
This file was deleted.

js/src/bitswap/stat.js

Lines changed: 16 additions & 34 deletions
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

@@ -13,50 +12,33 @@ module.exports = (createCommon, options) => {
1312
describe('.bitswap.stat', () => {
1413
let ipfs
1514

16-
before(function (done) {
15+
before(async function () {
1716
// CI takes longer to instantiate the daemon, so we need to increase the
1817
// timeout for the before step
1918
this.timeout(60 * 1000)
2019

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-
})
20+
const factory = await common.setup()
21+
ipfs = await factory.spawnNode()
2922
})
3023

31-
after((done) => common.teardown(done))
24+
after(() => common.teardown())
3225

33-
it('should get bitswap stats', (done) => {
34-
ipfs.bitswap.stat((err, res) => {
35-
expectIsBitswap(err, res)
36-
done()
37-
})
26+
it('should get bitswap stats', async () => {
27+
const res = await ipfs.bitswap.stat()
28+
expectIsBitswap(res)
3829
})
3930

40-
it('should get bitswap stats (promised)', () => {
41-
return ipfs.bitswap.stat().then((res) => {
42-
expectIsBitswap(null, res)
43-
})
44-
})
45-
46-
it('should not get bitswap stats when offline', function (done) {
31+
it('should not get bitswap stats when offline', async function () {
4732
this.timeout(60 * 1000)
4833

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-
})
34+
const common = createCommon()
35+
const factory = await common.setup()
36+
const ipfs = await factory.spawnNode()
37+
await ipfs.stop()
38+
39+
// TODO: assert on error message/code
40+
await expect(ipfs.bitswap.stat()).to.be.rejected()
41+
return common.teardown()
6042
})
6143
})
6244
}

js/src/bitswap/utils.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
'use strict'
22

3-
const until = require('async/until')
4-
5-
function waitForWantlistKey (ipfs, key, opts, cb) {
6-
if (typeof opts === 'function') {
7-
cb = opts
8-
opts = {}
9-
}
10-
3+
async function waitForWantlistKey (ipfs, key, opts) {
114
opts = opts || {}
125
opts.timeout = opts.timeout || 1000
136

14-
let list = { Keys: [] }
15-
let timedOut = false
7+
const start = Date.now()
168

17-
setTimeout(() => { timedOut = true }, opts.timeout)
18-
19-
const test = () => timedOut ? true : list.Keys.every(k => k['/'] === key)
20-
const iteratee = (cb) => ipfs.bitswap.wantlist(opts.peerId, cb)
9+
while (Date.now() <= start + opts.timeout) {
10+
const list = await ipfs.bitswap.wantlist(opts.peerId)
11+
if (list.Keys.find(k => k['/'] === key)) return
12+
}
2113

22-
until(test, iteratee, (err) => {
23-
if (err) return cb(err)
24-
if (timedOut) return cb(new Error(`Timed out waiting for ${key} in wantlist`))
25-
cb()
26-
})
14+
throw new Error(`Timed out waiting for ${key} in wantlist`)
2715
}
2816

2917
module.exports.waitForWantlistKey = waitForWantlistKey

js/src/bitswap/wantlist.js

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* eslint max-nested-callbacks: ["error", 6] */
33
'use strict'
44

5-
const waterfall = require('async/waterfall')
65
const { spawnNodesWithId } = require('../utils/spawn')
76
const { getDescribe, getIt, expect } = require('../utils/mocha')
87
const { waitForWantlistKey } = require('./utils')
@@ -18,58 +17,46 @@ module.exports = (createCommon, options) => {
1817
let ipfsB
1918
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'
2019

21-
before(function (done) {
20+
before(async function () {
2221
// CI takes longer to instantiate the daemon, so we need to increase the
2322
// timeout for the before step
2423
this.timeout(60 * 1000)
2524

26-
common.setup((err, factory) => {
27-
expect(err).to.not.exist()
25+
const factory = await common.setup()
26+
const nodes = await spawnNodesWithId(2, factory)
2827

29-
spawnNodesWithId(2, factory, (err, nodes) => {
30-
expect(err).to.not.exist()
28+
ipfsA = nodes[0]
29+
ipfsB = nodes[1]
3130

32-
ipfsA = nodes[0]
33-
ipfsB = nodes[1]
31+
// Add key to the wantlist for ipfsB
32+
ipfsB.block.get(key)
3433

35-
// Add key to the wantlist for ipfsB
36-
ipfsB.block.get(key, () => {})
37-
38-
connect(ipfsA, ipfsB.peerId.addresses[0], done)
39-
})
40-
})
34+
return connect(ipfsA, ipfsB.peerId.addresses[0])
4135
})
4236

43-
after(function (done) {
37+
after(async function () {
4438
this.timeout(30 * 1000)
45-
common.teardown(done)
39+
return common.teardown()
4640
})
4741

48-
it('should get the wantlist', (done) => {
49-
waitForWantlistKey(ipfsB, key, done)
50-
})
42+
it('should get the wantlist', () => waitForWantlistKey(ipfsB, key))
5143

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-
})
44+
it('should get the wantlist by peer ID for a diffreent node', async () => {
45+
const info = await ipfsB.id()
46+
return waitForWantlistKey(ipfsA, key, { peerId: info.id })
5747
})
5848

59-
it('should not get the wantlist when offline', function (done) {
49+
it('should not get the wantlist when offline', async function () {
6050
this.timeout(60 * 1000)
6151

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-
})
52+
const common = createCommon()
53+
const factory = await common.setup()
54+
const ipfs = factory.spawnNode()
55+
await ipfs.stop()
56+
57+
// TODO: assert on error message/code
58+
await expect(ipfs.bitswap.wantlist()).to.be.rejected()
59+
return common.teardown()
7360
})
7461
})
7562
}

js/src/stats/utils.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const isBigInt = (n) => {
66
return n.constructor.name === 'Big'
77
}
88

9-
exports.expectIsBitswap = (err, stats) => {
10-
expect(err).to.not.exist()
9+
exports.expectIsBitswap = stats => {
1110
expect(stats).to.exist()
1211
expect(stats).to.have.a.property('provideBufLen')
1312
expect(stats).to.have.a.property('wantlist')
@@ -30,8 +29,7 @@ exports.expectIsBitswap = (err, stats) => {
3029
expect(isBigInt(stats.dupDataReceived)).to.eql(true)
3130
}
3231

33-
exports.expectIsBandwidth = (err, stats) => {
34-
expect(err).to.not.exist()
32+
exports.expectIsBandwidth = stats => {
3533
expect(stats).to.exist()
3634
expect(stats).to.have.a.property('totalIn')
3735
expect(stats).to.have.a.property('totalOut')
@@ -43,8 +41,7 @@ exports.expectIsBandwidth = (err, stats) => {
4341
expect(isBigInt(stats.rateOut)).to.eql(true)
4442
}
4543

46-
exports.expectIsRepo = (err, res) => {
47-
expect(err).to.not.exist()
44+
exports.expectIsRepo = res => {
4845
expect(res).to.exist()
4946
expect(res).to.have.a.property('numObjects')
5047
expect(res).to.have.a.property('repoSize')

0 commit comments

Comments
 (0)