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

Commit

Permalink
feat: more tests for swarm - test correct amount of peers
Browse files Browse the repository at this point in the history
  • Loading branch information
victorb authored and daviddias committed Sep 1, 2017
1 parent ac6fd41 commit b19c80f
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions src/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = (common) => {
describe('.swarm', () => {
let ipfsA
let ipfsB
let factoryInstance

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

common.setup((err, factory) => {
expect(err).to.not.exist()
factoryInstance = factory
series([
(cb) => {
factory.spawnNode((err, node) => {
Expand Down Expand Up @@ -107,6 +109,138 @@ module.exports = (common) => {
done()
})
})

const getConfig = (addresses) => {
return {
Addresses: {
Swarm: addresses,
API: null,
Gateway: null
}
}
}
const getRepoPath = () => {
return '/tmp/.ipfs-' + Math.random().toString().substring(2, 8)
}

describe('Shows connected peers only once', () => {
it('Connecting two peers with one address each', (done) => {
let nodeA
let nodeB
let nodeBAddress
const addresses = ['/ip4/127.0.0.1/tcp/0']
const config = getConfig(addresses)
series([
(cb) => {
factoryInstance.spawnNode(getRepoPath(), config, (err, node) => {
expect(err).to.not.exist()
nodeA = node
cb()
})
},
(cb) => {
factoryInstance.spawnNode(getRepoPath(), config, (err, node) => {
expect(err).to.not.exist()
nodeB = node
cb()
})
},
(cb) => {
nodeB.id((err, info) => {
expect(err).to.not.exist()
nodeBAddress = info.addresses[0]
cb()
})
},
(cb) => {
nodeA.swarm.connect(nodeBAddress, (err) => {
expect(err).to.not.exist()
cb()
})
},
(cb) => {
// Waiting to make sure nodes are connected
setTimeout(cb, 1000)
},
(cb) => {
nodeA.swarm.peers((err, peers) => {
expect(err).to.not.exist()
expect(peers).to.have.length(1)
cb()
})
},
(cb) => {
nodeB.swarm.peers((err, peers) => {
expect(err).to.not.exist()
expect(peers).to.have.length(1)
cb()
})
}
], done)
})

it('Connecting two peers with two addresses each', (done) => {
let nodeA
let nodeB
let nodeBAddress
const configA = getConfig([
// TODO: Change to port 0, needs: https://github.com/ipfs/interface-ipfs-core/issues/152
'/ip4/127.0.0.1/tcp/6543',
'/ip4/127.0.0.1/tcp/6544'
])
const configB = getConfig([
'/ip4/127.0.0.1/tcp/6545',
'/ip4/127.0.0.1/tcp/6546'
])
series([
(cb) => {
factoryInstance.spawnNode(getRepoPath(), configA, (err, node) => {
expect(err).to.not.exist()
nodeA = node
cb()
})
},
(cb) => {
factoryInstance.spawnNode(getRepoPath(), configB, (err, node) => {
expect(err).to.not.exist()
nodeB = node
cb()
})
},
(cb) => {
nodeB.id((err, info) => {
expect(err).to.not.exist()
nodeBAddress = info.addresses[0]
cb()
})
},
(cb) => {
nodeA.swarm.connect(nodeBAddress, (err) => {
expect(err).to.not.exist()
cb()
})
},
(cb) => {
// Waiting to make sure nodes are connected
setTimeout(cb, 1000)
},
(cb) => {
nodeA.swarm.peers((err, peers) => {
expect(err).to.not.exist()
expect(peers).to.have.length(1)
cb()
})
},
(cb) => {
nodeB.swarm.peers((err, peers) => {
expect(err).to.not.exist()
expect(peers).to.have.length(1)
cb()
})
}
], done)
})
})
})

it('.addrs', (done) => {
Expand Down

0 comments on commit b19c80f

Please sign in to comment.