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

Commit bd5aac6

Browse files
committed
chore: re-add bitswap tests
Originally from PR #267 License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 2ece673 commit bd5aac6

File tree

6 files changed

+252
-0
lines changed

6 files changed

+252
-0
lines changed

js/src/bitswap/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
const { createSuite } = require('../utils/suite')
3+
4+
const tests = {
5+
stat: require('./stat'),
6+
wantlist: require('./wantlist'),
7+
unwant: require('./unwant')
8+
}
9+
10+
module.exports = createSuite(tests)

js/src/bitswap/stat.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const waterfall = require('async/waterfall')
5+
const { getDescribe, getIt, expect } = require('../utils/mocha')
6+
const { expectIsBitswap } = require('../stats/utils')
7+
8+
module.exports = (createCommon, options) => {
9+
const describe = getDescribe(options)
10+
const it = getIt(options)
11+
const common = createCommon()
12+
13+
describe('.bitswap.stat', () => {
14+
let ipfs
15+
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+
})
29+
})
30+
31+
after((done) => common.teardown(done))
32+
33+
it('should get bitswap stats', (done) => {
34+
ipfs.bitswap.stat((err, res) => {
35+
expectIsBitswap(err, res)
36+
done()
37+
})
38+
})
39+
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) {
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+
})
60+
})
61+
})
62+
}

js/src/bitswap/unwant.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const waterfall = require('async/waterfall')
5+
const { waitUntilConnected } = require('../utils/connections')
6+
const { spawnNodes } = require('../utils/spawn')
7+
const { getDescribe, getIt, expect } = require('../utils/mocha')
8+
const { waitForWantlistKey } = require('./utils')
9+
10+
module.exports = (createCommon, options) => {
11+
const describe = getDescribe(options)
12+
const it = getIt(options)
13+
const common = createCommon()
14+
15+
describe('.bitswap.unwant', () => {
16+
let ipfsA
17+
let ipfsB
18+
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'
19+
20+
before(function (done) {
21+
// CI takes longer to instantiate the daemon, so we need to increase the
22+
// timeout for the before step
23+
this.timeout(60 * 1000)
24+
25+
common.setup((err, factory) => {
26+
expect(err).to.not.exist()
27+
28+
spawnNodes(2, factory, (err, nodes) => {
29+
expect(err).to.not.exist()
30+
31+
ipfsA = nodes[0]
32+
ipfsB = nodes[1]
33+
34+
// Add key to the wantlist for ipfsB
35+
ipfsB.block.get(key, () => {})
36+
37+
waitUntilConnected(ipfsA, ipfsB, done)
38+
})
39+
})
40+
})
41+
42+
after((done) => common.teardown(done))
43+
44+
it('should remove a key from the wantlist', (done) => {
45+
waitForWantlistKey(ipfsB, key, (err) => {
46+
expect(err).to.not.exist()
47+
48+
ipfsB.bitswap.unwant(key, (err) => {
49+
expect(err).to.not.exist()
50+
51+
ipfsB.bitswap.wantlist((err, list) => {
52+
expect(err).to.not.exist()
53+
expect(list.Keys.every(k => k['/'] !== key)).to.equal(true)
54+
done()
55+
})
56+
})
57+
})
58+
})
59+
60+
it('should not remove a key from the wantlist when offline', function (done) {
61+
this.timeout(60 * 1000)
62+
63+
waterfall([
64+
(cb) => createCommon().setup(cb),
65+
(factory, cb) => factory.spawnNode(cb),
66+
(node, cb) => node.stop((err) => cb(err, node))
67+
], (err, node) => {
68+
expect(err).to.not.exist()
69+
node.bitswap.wantlist((err) => {
70+
expect(err).to.exist()
71+
done()
72+
})
73+
})
74+
})
75+
})
76+
}

js/src/bitswap/utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
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+
11+
opts = opts || {}
12+
opts.timeout = opts.timeout || 1000
13+
14+
let list = { Keys: [] }
15+
let timedOut = false
16+
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)
21+
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+
})
27+
}
28+
29+
module.exports.waitForWantlistKey = waitForWantlistKey

js/src/bitswap/wantlist.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const waterfall = require('async/waterfall')
5+
const { waitUntilConnected } = require('../utils/connections')
6+
const { spawnNodes } = require('../utils/spawn')
7+
const { getDescribe, getIt, expect } = require('../utils/mocha')
8+
const { waitForWantlistKey } = require('./utils')
9+
10+
module.exports = (createCommon, options) => {
11+
const describe = getDescribe(options)
12+
const it = getIt(options)
13+
const common = createCommon()
14+
15+
describe('.bitswap.wantlist', () => {
16+
let ipfsA
17+
let ipfsB
18+
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'
19+
20+
before(function (done) {
21+
// CI takes longer to instantiate the daemon, so we need to increase the
22+
// timeout for the before step
23+
this.timeout(60 * 1000)
24+
25+
common.setup((err, factory) => {
26+
expect(err).to.not.exist()
27+
28+
spawnNodes(2, factory, (err, nodes) => {
29+
expect(err).to.not.exist()
30+
31+
ipfsA = nodes[0]
32+
ipfsB = nodes[1]
33+
34+
// Add key to the wantlist for ipfsB
35+
ipfsB.block.get(key, () => {})
36+
37+
waitUntilConnected(ipfsA, ipfsB, done)
38+
})
39+
})
40+
})
41+
42+
after(function (done) {
43+
this.timeout(30 * 1000)
44+
common.teardown(done)
45+
})
46+
47+
it('should get the wantlist', (done) => {
48+
waitForWantlistKey(ipfsB, key, done)
49+
})
50+
51+
it('should get the wantlist by peer ID for a diffreent node', (done) => {
52+
ipfsB.id((err, info) => {
53+
expect(err).to.not.exist()
54+
waitForWantlistKey(ipfsA, key, { peerId: info.id }, done)
55+
})
56+
})
57+
58+
it('should not get the wantlist when offline', function (done) {
59+
this.timeout(60 * 1000)
60+
61+
waterfall([
62+
(cb) => createCommon().setup(cb),
63+
(factory, cb) => factory.spawnNode(cb),
64+
(node, cb) => node.stop((err) => cb(err, node))
65+
], (err, node) => {
66+
expect(err).to.not.exist()
67+
node.bitswap.wantlist((err) => {
68+
expect(err).to.exist()
69+
done()
70+
})
71+
})
72+
})
73+
})
74+
}

js/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
exports.bitswap = require('./bitswap')
34
exports.block = require('./block')
45
exports.bootstrap = require('./bootstrap')
56
exports.config = require('./config')

0 commit comments

Comments
 (0)