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

Commit e309b36

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 d89ce96 commit e309b36

File tree

8 files changed

+77
-104
lines changed

8 files changed

+77
-104
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/block/get.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ module.exports = {
1717
throw err
1818
}
1919

20-
process.stdout.write(block.data)
20+
if (block) {
21+
process.stdout.write(block.data)
22+
} else {
23+
process.stderr.write('Block was unwanted before it could be remotely retrieved')
24+
}
2125
})
2226
}
2327
}

src/core/components/bitswap.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ function formatWantlist (list) {
1111

1212
module.exports = function bitswap (self) {
1313
return {
14-
wantlist: () => {
14+
wantlist: promisify((callback) => {
1515
if (!self.isOnline()) {
16-
throw new Error(OFFLINE_ERROR)
16+
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
1717
}
1818

19-
const list = self._bitswap.getWantlist()
20-
return formatWantlist(list)
21-
},
19+
let list = self._bitswap.getWantlist()
20+
list = formatWantlist(list)
21+
callback(null, list)
22+
}),
2223

2324
stat: promisify((callback) => {
2425
if (!self.isOnline()) {
@@ -40,12 +41,12 @@ module.exports = function bitswap (self) {
4041
})
4142
}),
4243

43-
unwant: (key) => {
44+
unwant: promisify((key, callback) => {
4445
if (!self.isOnline()) {
45-
throw new Error(OFFLINE_ERROR)
46+
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
4647
}
4748

48-
self._bitswap.unwant(key)
49-
}
49+
callback(null, self._bitswap.unwant(key))
50+
})
5051
}
5152
}

src/http/api/resources/bitswap.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ 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()
10+
request.server.app.ipfs.bitswap.wantlist((err, list) => {
11+
if (err) {
12+
return reply(boom.badRequest(err))
13+
}
1314
list = list.map((entry) => entry.cid.toBaseEncodedString())
14-
} catch (err) {
15-
return reply(boom.badRequest(err))
16-
}
17-
18-
reply({
19-
Keys: list
15+
reply({
16+
Keys: list
17+
})
2018
})
2119
}
2220

@@ -53,12 +51,11 @@ exports.unwant = {
5351
handler: (request, reply) => {
5452
const key = request.pre.args.key
5553
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 })
54+
ipfs.bitswap.unwant(key, (err) => {
55+
if (err) {
56+
return reply(boom.badRequest(err))
57+
}
58+
reply({ key: key.toBaseEncodedString() })
59+
})
6360
}
6461
}

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)

0 commit comments

Comments
 (0)