Skip to content

Commit ff250de

Browse files
committed
tests for with and without dht
1 parent 5cb52e3 commit ff250de

File tree

9 files changed

+501
-333
lines changed

9 files changed

+501
-333
lines changed

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Node.js implementation of the Bitswap data exchange protocol used by IPFS",
55
"main": "src/index.js",
66
"browser": {
7-
"./test/utils/libp2p-bundle": false,
7+
"./test/utils/create-libp2p-node": false,
88
"./test/utils/create-temp-repo-nodejs.js": "./test/utils/create-temp-repo-browser.js"
99
},
1010
"scripts": {
@@ -40,25 +40,26 @@
4040
"devDependencies": {
4141
"aegir": "^11.0.2",
4242
"benchmark": "^2.1.4",
43-
"chai": "^4.0.2",
44-
"dirty-chai": "^2.0.0",
43+
"chai": "^4.1.0",
44+
"dirty-chai": "^2.0.1",
4545
"ipfs-repo": "~0.15.0",
46-
"libp2p": "^0.9.1",
47-
"libp2p-multiplex": "^0.4.3",
46+
"libp2p": "^0.10.1",
47+
"libp2p-kad-dht": "^0.2.1",
48+
"libp2p-multiplex": "^0.4.4",
4849
"libp2p-secio": "^0.6.8",
4950
"libp2p-tcp": "^0.10.1",
5051
"lodash": "^4.17.4",
5152
"multiaddr": "^2.3.0",
5253
"ncp": "^2.0.0",
5354
"peer-book": "~0.4.0",
5455
"peer-id": "~0.8.7",
55-
"peer-info": "~0.9.2",
56+
"peer-info": "~0.9.3",
5657
"rimraf": "^2.6.1",
5758
"safe-buffer": "^5.1.1"
5859
},
5960
"dependencies": {
6061
"async": "^2.5.0",
61-
"cids": "~0.5.0",
62+
"cids": "~0.5.1",
6263
"debug": "^2.6.8",
6364
"ipfs-block": "~0.6.0",
6465
"lodash.debounce": "^4.0.8",

test/bitswap-mock-internals.js

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
'use strict'
4+
5+
const eachSeries = require('async/eachSeries')
6+
const waterfall = require('async/waterfall')
7+
const map = require('async/map')
8+
const parallel = require('async/parallel')
9+
const setImmediate = require('async/setImmediate')
10+
const _ = require('lodash')
11+
const chai = require('chai')
12+
chai.use(require('dirty-chai'))
13+
const expect = chai.expect
14+
const PeerId = require('peer-id')
15+
16+
const Message = require('../src/types/message')
17+
const Bitswap = require('../src')
18+
19+
const createTempRepo = require('./utils/create-temp-repo-nodejs')
20+
const mockNetwork = require('./utils/mocks').mockNetwork
21+
const applyNetwork = require('./utils/mocks').applyNetwork
22+
const mockLibp2pNode = require('./utils/mocks').mockLibp2pNode
23+
const storeHasBlocks = require('./utils/store-has-blocks')
24+
const makeBlock = require('./utils/make-block')
25+
26+
describe('bitswap with mocks', () => {
27+
let repo
28+
let blocks
29+
let ids
30+
31+
before((done) => {
32+
parallel([
33+
(cb) => createTempRepo(cb),
34+
(cb) => map(_.range(15), (i, cb) => makeBlock(cb), cb),
35+
(cb) => map(_.range(2), (i, cb) => PeerId.create(cb), cb)
36+
], (err, results) => {
37+
if (err) {
38+
return done(err)
39+
}
40+
41+
repo = results[0]
42+
blocks = results[1]
43+
ids = results[2]
44+
45+
done()
46+
})
47+
})
48+
49+
after((done) => repo.teardown(done))
50+
51+
describe('receive message', () => {
52+
it('simple block message', (done) => {
53+
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
54+
bs.start()
55+
56+
const other = ids[1]
57+
58+
const b1 = blocks[0]
59+
const b2 = blocks[1]
60+
61+
const msg = new Message(false)
62+
msg.addBlock(b1)
63+
msg.addBlock(b2)
64+
65+
bs._receiveMessage(other, msg, (err) => {
66+
expect(err).to.not.exist()
67+
expect(bs.blocksRecvd).to.equal(2)
68+
expect(bs.dupBlocksRecvd).to.equal(0)
69+
70+
map([b1.cid, b2.cid], (cid, cb) => repo.blocks.get(cid, cb), (err, blocks) => {
71+
expect(err).to.not.exist()
72+
73+
expect(blocks[0].data).to.eql(b1.data)
74+
expect(blocks[1].data).to.eql(b2.data)
75+
done()
76+
})
77+
})
78+
})
79+
80+
it('simple want message', (done) => {
81+
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
82+
bs.start()
83+
84+
const other = ids[1]
85+
const b1 = blocks[0]
86+
const b2 = blocks[1]
87+
88+
const msg = new Message(false)
89+
90+
msg.addEntry(b1.cid, 1, false)
91+
msg.addEntry(b2.cid, 1, false)
92+
93+
bs._receiveMessage(other, msg, (err) => {
94+
expect(err).to.not.exist()
95+
96+
expect(bs.blocksRecvd).to.be.eql(0)
97+
expect(bs.dupBlocksRecvd).to.be.eql(0)
98+
99+
const wl = bs.wantlistForPeer(other)
100+
101+
expect(wl.has(b1.cid.buffer.toString())).to.eql(true)
102+
expect(wl.has(b2.cid.buffer.toString())).to.eql(true)
103+
104+
done()
105+
})
106+
})
107+
108+
it('multi peer', (done) => {
109+
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
110+
111+
let others
112+
let blocks
113+
114+
bs.start()
115+
116+
parallel([
117+
(cb) => map(_.range(5), (i, cb) => PeerId.create(cb), cb),
118+
(cb) => map(_.range(10), (i, cb) => makeBlock(cb), cb)
119+
], (err, results) => {
120+
if (err) {
121+
return done(err)
122+
}
123+
124+
others = results[0]
125+
blocks = results[1]
126+
test()
127+
})
128+
129+
function test () {
130+
map(_.range(5), (i, cb) => {
131+
const msg = new Message(false)
132+
msg.addBlock(blocks[i])
133+
msg.addBlock(blocks[5 + 1])
134+
cb(null, msg)
135+
}, (err, messages) => {
136+
expect(err).to.not.exist()
137+
let i = 0
138+
eachSeries(others, (other, cb) => {
139+
const msg = messages[i]
140+
i++
141+
bs._receiveMessage(other, msg, (err) => {
142+
expect(err).to.not.exist()
143+
storeHasBlocks(msg, repo.blocks, cb)
144+
})
145+
}, done)
146+
})
147+
}
148+
})
149+
})
150+
151+
describe('get', () => {
152+
it('block exists locally', (done) => {
153+
const block = blocks[4]
154+
155+
repo.blocks.put(block, (err) => {
156+
expect(err).to.not.exist()
157+
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
158+
159+
bs.get(block.cid, (err, res) => {
160+
expect(err).to.not.exist()
161+
expect(res).to.eql(block)
162+
done()
163+
})
164+
})
165+
})
166+
167+
it('blocks exist locally', (done) => {
168+
const b1 = blocks[3]
169+
const b2 = blocks[14]
170+
const b3 = blocks[13]
171+
172+
repo.blocks.putMany([b1, b2, b3], (err) => {
173+
expect(err).to.not.exist()
174+
175+
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
176+
177+
bs.getMany([b1.cid, b2.cid, b3.cid], (err, res) => {
178+
expect(err).to.not.exist()
179+
expect(res).to.be.eql([b1, b2, b3])
180+
done()
181+
})
182+
})
183+
})
184+
185+
it('getMany', (done) => {
186+
const b1 = blocks[5]
187+
const b2 = blocks[6]
188+
const b3 = blocks[7]
189+
190+
repo.blocks.putMany([b1, b2, b3], (err) => {
191+
expect(err).to.not.exist()
192+
193+
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
194+
195+
map([b1.cid, b2.cid, b3.cid], (cid, cb) => bs.get(cid, cb), (err, res) => {
196+
expect(err).to.not.exist()
197+
expect(res).to.eql([b1, b2, b3])
198+
done()
199+
})
200+
})
201+
})
202+
203+
it('block is added locally afterwards', (done) => {
204+
const block = blocks[9]
205+
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
206+
const net = mockNetwork()
207+
208+
bs.network = net
209+
bs.wm.network = net
210+
bs.engine.network = net
211+
bs.start()
212+
213+
bs.get(block.cid, (err, res) => {
214+
expect(err).to.not.exist()
215+
expect(res).to.be.eql(block)
216+
done()
217+
})
218+
219+
setTimeout(() => bs.put(block, () => {}), 200)
220+
})
221+
222+
it('block is sent after local add', (done) => {
223+
const me = ids[0]
224+
const other = ids[1]
225+
const block = blocks[10]
226+
let bs1
227+
let bs2
228+
229+
const n1 = {
230+
connectTo (id, cb) {
231+
let err
232+
if (id.toHexString() !== other.toHexString()) {
233+
err = new Error('unkown peer')
234+
}
235+
setImmediate(() => cb(err))
236+
},
237+
sendMessage (id, msg, cb) {
238+
if (id.toHexString() === other.toHexString()) {
239+
bs2._receiveMessage(me, msg, cb)
240+
} else {
241+
setImmediate(() => cb(new Error('unkown peer')))
242+
}
243+
},
244+
start () {},
245+
stop () {},
246+
findAndConnect (cid, maxProviders, callback) {
247+
setImmediate(() => callback)
248+
},
249+
provide (cid, callback) {
250+
setImmediate(() => callback)
251+
}
252+
}
253+
const n2 = {
254+
connectTo (id, cb) {
255+
let err
256+
if (id.toHexString() !== me.toHexString()) {
257+
err = new Error('unkown peer')
258+
}
259+
setImmediate(() => cb(err))
260+
},
261+
sendMessage (id, msg, cb) {
262+
if (id.toHexString() === me.toHexString()) {
263+
bs1._receiveMessage(other, msg, cb)
264+
} else {
265+
setImmediate(() => cb(new Error('unkown peer')))
266+
}
267+
},
268+
start () {},
269+
stop () {},
270+
findAndConnect (cid, maxProviders, callback) {
271+
setImmediate(() => callback)
272+
},
273+
provide (cid, callback) {
274+
setImmediate(() => callback)
275+
}
276+
}
277+
bs1 = new Bitswap(mockLibp2pNode, repo.blocks)
278+
applyNetwork(bs1, n1)
279+
bs1.start()
280+
281+
let repo2
282+
283+
waterfall([
284+
(cb) => createTempRepo(cb),
285+
(repo, cb) => {
286+
repo2 = repo
287+
bs2 = new Bitswap(mockLibp2pNode(), repo2.blocks)
288+
applyNetwork(bs2, n2)
289+
bs2.start()
290+
bs1._onPeerConnected(other)
291+
bs2._onPeerConnected(me)
292+
293+
bs1.get(block.cid, (err, res) => {
294+
expect(err).to.not.exist()
295+
cb(null, res)
296+
})
297+
setTimeout(() => bs2.put(block, () => {}), 1000)
298+
},
299+
(res, cb) => {
300+
expect(res).to.eql(block)
301+
cb()
302+
}
303+
], done)
304+
})
305+
})
306+
307+
describe('stat', () => {
308+
it('has initial stats', () => {
309+
const bs = new Bitswap(mockLibp2pNode(), {})
310+
311+
const stats = bs.stat()
312+
expect(stats).to.have.property('wantlist')
313+
expect(stats).to.have.property('blocksReceived', 0)
314+
expect(stats).to.have.property('dupBlksReceived', 0)
315+
expect(stats).to.have.property('dupDataReceived', 0)
316+
expect(stats).to.have.property('peers')
317+
})
318+
})
319+
320+
describe('unwant', () => {
321+
it('removes blocks that are wanted multiple times', (done) => {
322+
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
323+
bs.start()
324+
const b = blocks[11]
325+
326+
let counter = 0
327+
const check = (err, res) => {
328+
expect(err).to.not.exist()
329+
expect(res).to.not.exist()
330+
331+
if (++counter === 2) { done() }
332+
}
333+
334+
bs.get(b.cid, check)
335+
bs.get(b.cid, check)
336+
337+
setTimeout(() => bs.unwant(b.cid), 10)
338+
})
339+
})
340+
})

0 commit comments

Comments
 (0)