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

Commit 6f1005a

Browse files
committed
refactor bitswap tests
1 parent 98e1da1 commit 6f1005a

File tree

1 file changed

+124
-127
lines changed

1 file changed

+124
-127
lines changed

test/core/bitswap.spec.js

Lines changed: 124 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,44 @@ function makeBlock (callback) {
3434
})
3535
}
3636

37+
function wire (targetNode, dialerNode, callback) {
38+
targetNode.id((err, identity) => {
39+
expect(err).to.not.exist()
40+
const addr = identity.addresses
41+
.map((addr) => multiaddr(addr.toString().split('ipfs')[0]))
42+
.filter((addr) => _.includes(addr.protoNames(), 'ws'))[0]
43+
44+
if (!addr) {
45+
// Note: the browser doesn't have a websockets listening addr
46+
return callback()
47+
}
48+
49+
const targetAddr = addr
50+
.encapsulate(multiaddr(`/ipfs/${identity.id}`)).toString()
51+
.replace('0.0.0.0', '127.0.0.1')
52+
53+
dialerNode.swarm.connect(targetAddr, callback)
54+
})
55+
}
56+
57+
function connectNodes (remoteNode, inProcNode, callback) {
58+
series([
59+
(cb) => wire(remoteNode, inProcNode, cb),
60+
// need timeout so we wait for identify to happen.
61+
// This call is just to ensure identify happened
62+
(cb) => setTimeout(() => wire(inProcNode, remoteNode, cb), 500)
63+
], callback)
64+
}
65+
66+
function addNode (num, inProcNode, callback) {
67+
num = leftPad(num, 3, 0)
68+
69+
const apiUrl = `/ip4/127.0.0.1/tcp/31${num}`
70+
const remoteNode = new API(apiUrl)
71+
72+
connectNodes(remoteNode, inProcNode, (err) => callback(err, remoteNode))
73+
}
74+
3775
describe('bitswap', function () {
3876
this.timeout(80 * 1000)
3977

@@ -42,7 +80,7 @@ describe('bitswap', function () {
4280
beforeEach(function (done) {
4381
this.timeout(60 * 1000)
4482

45-
let config = {
83+
let options = {
4684
repo: createTempRepo(),
4785
config: {
4886
Addresses: {
@@ -58,7 +96,7 @@ describe('bitswap', function () {
5896
}
5997

6098
if (isNode) {
61-
config = Object.assign(config, {
99+
options = Object.assign(options, {
62100
config: {
63101
Addresses: {
64102
Swarm: ['/ip4/127.0.0.1/tcp/0']
@@ -67,150 +105,110 @@ describe('bitswap', function () {
67105
})
68106
}
69107

70-
inProcNode = new IPFS(config)
108+
inProcNode = new IPFS(options)
71109
inProcNode.on('start', () => done())
72110
})
73111

74112
afterEach(function (done) {
75-
this.timeout(30 * 1000)
76-
113+
this.timeout(60 * 1000)
77114
inProcNode.stop(() => done())
78115
})
79116

80-
describe('connections', () => {
81-
function wire (targetNode, dialerNode, done) {
82-
targetNode.id((err, identity) => {
83-
expect(err).to.not.exist()
84-
const addr = identity.addresses
85-
.map((addr) => multiaddr(addr.toString().split('ipfs')[0]))
86-
.filter((addr) => _.includes(addr.protoNames(), 'ws'))[0]
117+
describe('transfer a block between', () => {
118+
it('2 peers', function (done) {
119+
this.timeout(40 * 1000)
87120

88-
if (!addr) {
89-
// Note: the browser doesn't have a websockets listening addr
90-
return done()
121+
let remoteNode
122+
let block
123+
waterfall([
124+
(cb) => parallel([
125+
(cb) => makeBlock(cb),
126+
(cb) => addNode(13, inProcNode, cb)
127+
], cb),
128+
(res, cb) => {
129+
block = res[0]
130+
remoteNode = res[1]
131+
cb()
132+
},
133+
(cb) => remoteNode.block.put(block, cb),
134+
(key, cb) => inProcNode.block.get(block.cid, cb),
135+
(b, cb) => {
136+
expect(b.data).to.eql(block.data)
137+
cb()
91138
}
92-
93-
const targetAddr = addr
94-
.encapsulate(multiaddr(`/ipfs/${identity.id}`)).toString()
95-
.replace('0.0.0.0', '127.0.0.1')
96-
97-
dialerNode.swarm.connect(targetAddr, done)
98-
})
99-
}
100-
101-
function connectNodes (remoteNode, ipn, done) {
102-
series([
103-
(cb) => wire(remoteNode, ipn, cb),
104-
// need timeout so we wait for identify to happen.
105-
// This call is just to ensure identify happened
106-
(cb) => setTimeout(() => wire(ipn, remoteNode, cb), 300)
107139
], done)
108-
}
109-
110-
function addNode (num, done) {
111-
num = leftPad(num, 3, 0)
140+
})
112141

113-
const apiUrl = `/ip4/127.0.0.1/tcp/31${num}`
114-
const remoteNode = new API(apiUrl)
142+
it('3 peers', function (done) {
143+
this.timeout(60 * 1000)
115144

116-
connectNodes(remoteNode, inProcNode, (err) => done(err, remoteNode))
117-
}
145+
let blocks
146+
const remoteNodes = []
118147

119-
describe('fetches a remote block', () => {
120-
it('2 peers', function (done) {
121-
this.timeout(10 * 1000)
122-
123-
let remoteNode
124-
let block
125-
waterfall([
126-
(cb) => parallel([
127-
(cb) => makeBlock(cb),
128-
(cb) => addNode(13, cb)
129-
], cb),
130-
(res, cb) => {
131-
block = res[0]
132-
remoteNode = res[1]
133-
cb()
134-
},
135-
(cb) => remoteNode.block.put(block, cb),
136-
(key, cb) => inProcNode.block.get(block.cid, cb),
137-
(b, cb) => {
138-
expect(b.data).to.eql(block.data)
139-
cb()
148+
series([
149+
(cb) => parallel(_.range(6).map((i) => makeBlock), (err, _blocks) => {
150+
expect(err).to.not.exist()
151+
blocks = _blocks
152+
cb()
153+
}),
154+
(cb) => addNode(8, inProcNode, (err, _ipfs) => {
155+
remoteNodes.push(_ipfs)
156+
cb(err)
157+
}),
158+
(cb) => addNode(7, inProcNode, (err, _ipfs) => {
159+
remoteNodes.push(_ipfs)
160+
cb(err)
161+
}),
162+
(cb) => connectNodes(remoteNodes[0], remoteNodes[1], cb),
163+
(cb) => remoteNodes[0].block.put(blocks[0], cb),
164+
(cb) => remoteNodes[0].block.put(blocks[1], cb),
165+
(cb) => remoteNodes[1].block.put(blocks[2], cb),
166+
(cb) => remoteNodes[1].block.put(blocks[3], cb),
167+
(cb) => inProcNode.block.put(blocks[4], cb),
168+
(cb) => inProcNode.block.put(blocks[5], cb),
169+
// 3. Fetch blocks on all nodes
170+
(cb) => parallel(_.range(6).map((i) => (cbI) => {
171+
const check = (n, cid, callback) => {
172+
n.block.get(cid, (err, b) => {
173+
expect(err).to.not.exist()
174+
expect(b).to.eql(blocks[i])
175+
callback()
176+
})
140177
}
141-
], done)
142-
})
143178

144-
it('3 peers', function (done) {
145-
this.timeout(30 * 1000)
146-
147-
let blocks
148-
const remoteNodes = []
149-
150-
series([
151-
(cb) => parallel(_.range(6).map((i) => makeBlock), (err, _blocks) => {
152-
expect(err).to.not.exist()
153-
blocks = _blocks
154-
cb()
155-
}),
156-
(cb) => addNode(8, (err, _ipfs) => {
157-
remoteNodes.push(_ipfs)
158-
cb(err)
159-
}),
160-
(cb) => addNode(7, (err, _ipfs) => {
161-
remoteNodes.push(_ipfs)
162-
cb(err)
163-
}),
164-
(cb) => connectNodes(remoteNodes[0], remoteNodes[1], cb),
165-
(cb) => remoteNodes[0].block.put(blocks[0], cb),
166-
(cb) => remoteNodes[0].block.put(blocks[1], cb),
167-
(cb) => remoteNodes[1].block.put(blocks[2], cb),
168-
(cb) => remoteNodes[1].block.put(blocks[3], cb),
169-
(cb) => inProcNode.block.put(blocks[4], cb),
170-
(cb) => inProcNode.block.put(blocks[5], cb),
171-
// 3. Fetch blocks on all nodes
172-
(cb) => parallel(_.range(6).map((i) => (cbI) => {
173-
const check = (n, cid, callback) => {
174-
n.block.get(cid, (err, b) => {
175-
expect(err).to.not.exist()
176-
expect(b).to.eql(blocks[i])
177-
callback()
178-
})
179-
}
180-
181-
series([
182-
(cbJ) => check(remoteNodes[0], blocks[i].cid, cbJ),
183-
(cbJ) => check(remoteNodes[1], blocks[i].cid, cbJ),
184-
(cbJ) => check(inProcNode, blocks[i].cid, cbJ)
185-
], cbI)
186-
}), cb)
187-
], done)
188-
})
179+
series([
180+
(cbJ) => check(remoteNodes[0], blocks[i].cid, cbJ),
181+
(cbJ) => check(remoteNodes[1], blocks[i].cid, cbJ),
182+
(cbJ) => check(inProcNode, blocks[i].cid, cbJ)
183+
], cbI)
184+
}), cb)
185+
], done)
189186
})
187+
})
190188

191-
describe('fetches a remote file', () => {
192-
it('2 peers', (done) => {
193-
const file = Buffer.from(`I love IPFS <3 ${Math.random()}`)
194-
195-
waterfall([
196-
// 0. Start node
197-
(cb) => addNode(12, cb),
198-
// 1. Add file to tmp instance
199-
(remote, cb) => {
200-
remote.files.add([{path: 'awesome.txt', content: file}], cb)
201-
},
202-
// 2. Request file from local instance
203-
(filesAdded, cb) => inProcNode.files.cat(filesAdded[0].hash, cb)
204-
], (err, data) => {
205-
expect(err).to.not.exist()
206-
expect(data).to.eql(file)
207-
done()
208-
})
189+
describe('transfer a file between', () => {
190+
it('2 peers', (done) => {
191+
// TODO make this test more interesting (10Mb file)
192+
const file = Buffer.from(`I love IPFS <3 ${Math.random()}`)
193+
194+
waterfall([
195+
// 0. Start node
196+
(cb) => addNode(12, inProcNode, cb),
197+
// 1. Add file to tmp instance
198+
(remote, cb) => {
199+
remote.files.add([{path: 'awesome.txt', content: file}], cb)
200+
},
201+
// 2. Request file from local instance
202+
(filesAdded, cb) => inProcNode.files.cat(filesAdded[0].hash, cb)
203+
], (err, data) => {
204+
expect(err).to.not.exist()
205+
expect(data).to.eql(file)
206+
done()
209207
})
210208
})
211209
})
212210

213-
describe('bitswap API', () => {
211+
describe('api', () => {
214212
let node
215213

216214
before(function (done) {
@@ -243,8 +241,7 @@ describe('bitswap', function () {
243241
})
244242

245243
it('throws if offline', () => {
246-
expect(() => node.bitswap.unwant('my key'))
247-
.to.throw(/online/)
244+
expect(() => node.bitswap.unwant('my key')).to.throw(/online/)
248245
})
249246
})
250247

0 commit comments

Comments
 (0)