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

Commit b19c80f

Browse files
victorbdaviddias
authored andcommitted
feat: more tests for swarm - test correct amount of peers
1 parent ac6fd41 commit b19c80f

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

src/swarm.js

+134
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = (common) => {
1414
describe('.swarm', () => {
1515
let ipfsA
1616
let ipfsB
17+
let factoryInstance
1718

1819
before(function (done) {
1920
// CI takes longer to instantiate the daemon,
@@ -23,6 +24,7 @@ module.exports = (common) => {
2324

2425
common.setup((err, factory) => {
2526
expect(err).to.not.exist()
27+
factoryInstance = factory
2628
series([
2729
(cb) => {
2830
factory.spawnNode((err, node) => {
@@ -107,6 +109,138 @@ module.exports = (common) => {
107109
done()
108110
})
109111
})
112+
113+
const getConfig = (addresses) => {
114+
return {
115+
Addresses: {
116+
Swarm: addresses,
117+
API: null,
118+
Gateway: null
119+
}
120+
}
121+
}
122+
const getRepoPath = () => {
123+
return '/tmp/.ipfs-' + Math.random().toString().substring(2, 8)
124+
}
125+
126+
describe('Shows connected peers only once', () => {
127+
it('Connecting two peers with one address each', (done) => {
128+
let nodeA
129+
let nodeB
130+
let nodeBAddress
131+
const addresses = ['/ip4/127.0.0.1/tcp/0']
132+
const config = getConfig(addresses)
133+
series([
134+
(cb) => {
135+
factoryInstance.spawnNode(getRepoPath(), config, (err, node) => {
136+
expect(err).to.not.exist()
137+
nodeA = node
138+
cb()
139+
})
140+
},
141+
(cb) => {
142+
factoryInstance.spawnNode(getRepoPath(), config, (err, node) => {
143+
expect(err).to.not.exist()
144+
nodeB = node
145+
cb()
146+
})
147+
},
148+
(cb) => {
149+
nodeB.id((err, info) => {
150+
expect(err).to.not.exist()
151+
nodeBAddress = info.addresses[0]
152+
cb()
153+
})
154+
},
155+
(cb) => {
156+
nodeA.swarm.connect(nodeBAddress, (err) => {
157+
expect(err).to.not.exist()
158+
cb()
159+
})
160+
},
161+
(cb) => {
162+
// Waiting to make sure nodes are connected
163+
setTimeout(cb, 1000)
164+
},
165+
(cb) => {
166+
nodeA.swarm.peers((err, peers) => {
167+
expect(err).to.not.exist()
168+
expect(peers).to.have.length(1)
169+
cb()
170+
})
171+
},
172+
(cb) => {
173+
nodeB.swarm.peers((err, peers) => {
174+
expect(err).to.not.exist()
175+
expect(peers).to.have.length(1)
176+
cb()
177+
})
178+
}
179+
], done)
180+
})
181+
182+
it('Connecting two peers with two addresses each', (done) => {
183+
let nodeA
184+
let nodeB
185+
let nodeBAddress
186+
const configA = getConfig([
187+
// TODO: Change to port 0, needs: https://github.com./ipfs/interface-ipfs-core/issues/152
188+
'/ip4/127.0.0.1/tcp/6543',
189+
'/ip4/127.0.0.1/tcp/6544'
190+
])
191+
const configB = getConfig([
192+
'/ip4/127.0.0.1/tcp/6545',
193+
'/ip4/127.0.0.1/tcp/6546'
194+
])
195+
series([
196+
(cb) => {
197+
factoryInstance.spawnNode(getRepoPath(), configA, (err, node) => {
198+
expect(err).to.not.exist()
199+
nodeA = node
200+
cb()
201+
})
202+
},
203+
(cb) => {
204+
factoryInstance.spawnNode(getRepoPath(), configB, (err, node) => {
205+
expect(err).to.not.exist()
206+
nodeB = node
207+
cb()
208+
})
209+
},
210+
(cb) => {
211+
nodeB.id((err, info) => {
212+
expect(err).to.not.exist()
213+
nodeBAddress = info.addresses[0]
214+
cb()
215+
})
216+
},
217+
(cb) => {
218+
nodeA.swarm.connect(nodeBAddress, (err) => {
219+
expect(err).to.not.exist()
220+
cb()
221+
})
222+
},
223+
(cb) => {
224+
// Waiting to make sure nodes are connected
225+
setTimeout(cb, 1000)
226+
},
227+
(cb) => {
228+
nodeA.swarm.peers((err, peers) => {
229+
expect(err).to.not.exist()
230+
expect(peers).to.have.length(1)
231+
cb()
232+
})
233+
},
234+
(cb) => {
235+
nodeB.swarm.peers((err, peers) => {
236+
expect(err).to.not.exist()
237+
expect(peers).to.have.length(1)
238+
cb()
239+
})
240+
}
241+
], done)
242+
})
243+
})
110244
})
111245

112246
it('.addrs', (done) => {

0 commit comments

Comments
 (0)