Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 2ab7b02

Browse files
committed
feat(tests) move some bitswap tests to interface
Will require including the version of interface-ipfs-core that they are moved to
1 parent 2bd7b09 commit 2ab7b02

File tree

11 files changed

+123
-110
lines changed

11 files changed

+123
-110
lines changed

src/cli/commands/bitswap/unwant.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ module.exports = {
1515
}
1616
},
1717
handler (argv) {
18-
argv.ipfs.bitswap.unwant(argv.key)
19-
print(`Key ${argv.key} removed from wantlist`)
18+
argv.ipfs.bitswap.unwant(argv.key, (err) => {
19+
if (err) {
20+
throw err
21+
}
22+
print(`Key ${argv.key} removed from wantlist`)
23+
})
2024
}
2125
}

src/cli/commands/bitswap/wantlist.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ module.exports = {
2121
if (err) {
2222
throw err
2323
}
24-
res.Keys.forEach((cidStr) => {
25-
print(cidStr)
24+
res.Keys.forEach((cid) => {
25+
print(cid['/'])
2626
})
2727
})
2828
}

src/cli/commands/block/get.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const CID = require('cids')
4+
const print = require('../../utils').print
45

56
module.exports = {
67
command: 'get <key>',
@@ -17,7 +18,11 @@ module.exports = {
1718
throw err
1819
}
1920

20-
process.stdout.write(block.data)
21+
if (block) {
22+
print(block.data)
23+
} else {
24+
print('Block was unwanted before it could be remotely retrieved')
25+
}
2126
})
2227
}
2328
}

src/core/components/bitswap.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
44
const promisify = require('promisify-es6')
55
const setImmediate = require('async/setImmediate')
66
const Big = require('big.js')
7+
const CID = require('cids')
78

89
function formatWantlist (list) {
9-
return Array.from(list).map((e) => e[1])
10+
return Array.from(list).map((e) => ({ '/': e[1].cid.toBaseEncodedString() }))
1011
}
1112

1213
module.exports = function bitswap (self) {
1314
return {
14-
wantlist: () => {
15+
wantlist: promisify((callback) => {
1516
if (!self.isOnline()) {
16-
throw new Error(OFFLINE_ERROR)
17+
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
1718
}
1819

19-
const list = self._bitswap.getWantlist()
20-
return formatWantlist(list)
21-
},
20+
let list = self._bitswap.getWantlist()
21+
list = formatWantlist(list)
22+
callback(null, { Keys: list })
23+
}),
2224

2325
stat: promisify((callback) => {
2426
if (!self.isOnline()) {
@@ -40,12 +42,21 @@ module.exports = function bitswap (self) {
4042
})
4143
}),
4244

43-
unwant: (key) => {
45+
unwant: promisify((keys, callback) => {
4446
if (!self.isOnline()) {
45-
throw new Error(OFFLINE_ERROR)
47+
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
4648
}
4749

48-
self._bitswap.unwant(key)
49-
}
50+
if (!Array.isArray(keys)) {
51+
keys = [keys]
52+
}
53+
keys = keys.map((key) => {
54+
if (CID.isCID(key)) {
55+
return key
56+
}
57+
return new CID(key)
58+
})
59+
callback(null, self._bitswap.unwant(keys))
60+
})
5061
}
5162
}

src/http/api/resources/bitswap.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@ const parseKey = require('./block').parseKey
77
exports = module.exports
88

99
exports.wantlist = (request, reply) => {
10-
let list
11-
try {
12-
list = request.server.app.ipfs.bitswap.wantlist()
13-
list = list.map((entry) => entry.cid.toBaseEncodedString())
14-
} catch (err) {
15-
return reply(boom.badRequest(err))
16-
}
17-
18-
reply({
19-
Keys: list
10+
request.server.app.ipfs.bitswap.wantlist((err, list) => {
11+
if (err) {
12+
return reply(boom.badRequest(err))
13+
}
14+
reply(list)
2015
})
2116
}
2217

@@ -53,12 +48,11 @@ exports.unwant = {
5348
handler: (request, reply) => {
5449
const key = request.pre.args.key
5550
const ipfs = request.server.app.ipfs
56-
try {
57-
ipfs.bitswap.unwant(key)
58-
} catch (err) {
59-
return reply(boom.badRequest(err))
60-
}
61-
62-
reply({ Key: key })
51+
ipfs.bitswap.unwant(key, (err) => {
52+
if (err) {
53+
return reply(boom.badRequest(err))
54+
}
55+
reply({ key: key.toBaseEncodedString() })
56+
})
6357
}
6458
}

src/http/api/resources/block.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ exports.get = {
4848
}).code(500)
4949
}
5050

51-
return reply(block.data)
51+
if (block) {
52+
return reply(block.data)
53+
}
54+
return reply({
55+
Message: 'Block was unwanted before it could be remotely retrieved',
56+
Code: 0
57+
}).code(404)
5258
})
5359
}
5460
}

test/cli/bitswap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('bitswap', () => runOn((thing) => {
1313
ipfs('block get ' + key)
1414
.then(() => {})
1515
.catch(() => {})
16-
setTimeout(done, 800)
16+
setTimeout(done, 250)
1717
})
1818

1919
it('wantlist', function () {

test/core/bitswap.spec.js

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ const CID = require('cids')
1818

1919
const IPFSFactory = require('ipfsd-ctl')
2020

21-
// This gets replaced by '../utils/create-repo-browser.js' in the browser
22-
const createTempRepo = require('../utils/create-repo-nodejs.js')
23-
2421
const IPFS = require('../../src/core')
2522

2623
// TODO bitswap tests on windows is failing, missing proper shutdown of daemon
@@ -247,75 +244,4 @@ skipOnWindows('bitswap', function () {
247244
})
248245
})
249246
})
250-
251-
describe('api', () => {
252-
let node
253-
254-
before(function (done) {
255-
this.timeout(40 * 1000)
256-
257-
node = new IPFS({
258-
repo: createTempRepo(),
259-
start: false,
260-
config: {
261-
Addresses: {
262-
Swarm: []
263-
},
264-
Discovery: {
265-
MDNS: {
266-
Enabled: false
267-
}
268-
}
269-
}
270-
})
271-
node.on('ready', () => done())
272-
})
273-
274-
describe('while offline', () => {
275-
it('.wantlist throws if offline', () => {
276-
expect(() => node.bitswap.wantlist()).to.throw(/online/)
277-
})
278-
279-
it('.stat gives error while offline', () => {
280-
node.bitswap.stat((err, stats) => {
281-
expect(err).to.exist()
282-
expect(stats).to.not.exist()
283-
})
284-
})
285-
286-
it('.unwant throws if offline', () => {
287-
expect(() => node.bitswap.unwant('my key')).to.throw(/online/)
288-
})
289-
})
290-
291-
describe('while online', () => {
292-
before(function (done) {
293-
this.timeout(80 * 1000)
294-
295-
node.start(() => done())
296-
})
297-
298-
it('.wantlist returns an array of wanted blocks', () => {
299-
expect(node.bitswap.wantlist()).to.eql([])
300-
})
301-
302-
it('returns the stats', (done) => {
303-
node.bitswap.stat((err, stats) => {
304-
expect(err).to.not.exist()
305-
expect(stats).to.have.keys([
306-
'blocksReceived',
307-
'blocksSent',
308-
'dataReceived',
309-
'dataSent',
310-
'wantlist',
311-
'peers',
312-
'provideBufLen',
313-
'dupDataReceived',
314-
'dupBlksReceived'
315-
])
316-
done()
317-
})
318-
})
319-
})
320-
})
321247
})

test/core/interface/bitswap.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const test = require('interface-ipfs-core')
5+
const parallel = require('async/parallel')
6+
7+
const IPFS = require('../../../src')
8+
9+
const DaemonFactory = require('ipfsd-ctl')
10+
const df = DaemonFactory.create({ type: 'proc', exec: IPFS })
11+
12+
const nodes = []
13+
const common = {
14+
setup: function (callback) {
15+
callback(null, {
16+
spawnNode: (cb) => {
17+
df.spawn({
18+
initOptions: { bits: 512 }
19+
}, (err, _ipfsd) => {
20+
if (err) {
21+
return cb(err)
22+
}
23+
24+
nodes.push(_ipfsd)
25+
cb(null, _ipfsd.api)
26+
})
27+
}
28+
})
29+
},
30+
teardown: function (callback) {
31+
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
32+
}
33+
}
34+
35+
test.bitswap(common)

test/http-api/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict'
22

3-
require('./interface')
4-
require('./inject')
3+
require('./bitswap')
54
require('./block')
65
require('./bootstrap')
76
require('./config')
87
require('./dns')
98
require('./id')
9+
require('./inject')
10+
require('./interface')
1011
require('./object')
1112
require('./version')

0 commit comments

Comments
 (0)