Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
fix: change swarm test
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Sep 15, 2019
1 parent 6d6c4ca commit 00341f9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 141 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"chai": "^4.2.0",
"cids": "~0.7.1",
"concat-stream": "^2.0.0",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"es6-promisify": "^6.0.1",
"hat": "0.0.3",
Expand Down
27 changes: 8 additions & 19 deletions src/swarm/addrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,25 @@

const PeerInfo = require('peer-info')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { spawnNodesWithId } = require('../utils/spawn')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.swarm.addrs', function () {
this.timeout(80 * 1000)
this.timeout(60 * 1000)

let ipfsA, ipfsB
let ipfsA
let ipfsB

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(100 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()

spawnNodesWithId(2, factory, (err, nodes) => {
expect(err).to.not.exist()
ipfsA = nodes[0]
ipfsB = nodes[1]
ipfsA.swarm.connect(ipfsB.peerId.addresses[0], done)
})
})
before(async () => {
ipfsA = await common.setup()
ipfsB = await common.setup()
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
})

after((done) => common.teardown(done))
after(() => common.teardown())

it('should get a list of node addresses', (done) => {
ipfsA.swarm.addrs((err, peerInfos) => {
Expand Down
2 changes: 1 addition & 1 deletion src/swarm/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = (createCommon, options) => {
const common = createCommon()

describe('.swarm.connect', function () {
this.timeout(60000)
this.timeout(60 * 1000)
let ipfsA
let ipfsB

Expand Down
26 changes: 7 additions & 19 deletions src/swarm/disconnect.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
/* eslint-env mocha */
'use strict'

const { spawnNodesWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { getDescribe, getIt } = require('../utils/mocha')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.swarm.disconnect', function () {
this.timeout(80 * 1000)
this.timeout(60 * 1000)

let ipfsA
let ipfsB

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(100 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()

spawnNodesWithId(2, factory, (err, nodes) => {
expect(err).to.not.exist()
ipfsA = nodes[0]
ipfsB = nodes[1]
ipfsA.swarm.connect(ipfsB.peerId.addresses[0], done)
})
})
before(async () => {
ipfsA = await common.setup()
ipfsB = await common.setup()
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
})

after((done) => common.teardown(done))
after(() => common.teardown())

it('should disconnect from a peer', (done) => {
ipfsA.swarm.disconnect(ipfsB.peerId.addresses[0], done)
Expand Down
19 changes: 4 additions & 15 deletions src/swarm/local-addrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,15 @@ module.exports = (createCommon, options) => {
const common = createCommon()

describe('.swarm.localAddrs', function () {
this.timeout(80 * 1000)
this.timeout(60 * 1000)

let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(100 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
before(async () => {
ipfs = await common.setup()
})

after((done) => common.teardown(done))
after(() => common.teardown())

it('should list local addresses the node is listening on', (done) => {
ipfs.swarm.localAddrs((err, multiaddrs) => {
Expand Down
112 changes: 25 additions & 87 deletions src/swarm/peers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/* eslint-env mocha */
'use strict'

const auto = require('async/auto')
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const os = require('os')
const path = require('path')
const hat = require('hat')
const { spawnNodesWithId } = require('../utils/spawn')
const delay = require('delay')
const { getDescribe, getIt, expect } = require('../utils/mocha')

module.exports = (createCommon, options) => {
Expand All @@ -16,31 +12,18 @@ module.exports = (createCommon, options) => {
const common = createCommon()

describe('.swarm.peers', function () {
this.timeout(80 * 1000)
this.timeout(60 * 1000)

let ipfsA
let ipfsB
let ipfsFactory

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(100 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
ipfsFactory = factory

spawnNodesWithId(2, factory, (err, nodes) => {
expect(err).to.not.exist()
ipfsA = nodes[0]
ipfsB = nodes[1]
ipfsA.swarm.connect(ipfsB.peerId.addresses[0], done)
})
})
before(async () => {
ipfsA = await common.setup()
ipfsB = await common.setup()
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
})

after((done) => common.teardown(done))
after(() => common.teardown())

it('should list peers this node is connected to', (done) => {
ipfsA.swarm.peers((err, peers) => {
Expand Down Expand Up @@ -119,44 +102,20 @@ module.exports = (createCommon, options) => {
}
}

function getRepoPath () {
return path.join(os.tmpdir(), '.ipfs-' + hat())
}

it('should list peers only once', (done) => {
it('should list peers only once', async () => {
const config = getConfig(['/ip4/127.0.0.1/tcp/0'])

auto({
nodeA: (cb) => ipfsFactory.spawnNode(getRepoPath(), config, cb),
nodeB: ['nodeA', (_, cb) => {
ipfsFactory.spawnNode(getRepoPath(), config, cb)
}],
nodeBAddress: ['nodeB', (res, cb) => {
res.nodeB.id((err, info) => {
if (err) return cb(err)
cb(null, info.addresses[0])
})
}],
connectA2B: ['nodeA', 'nodeBAddress', (res, cb) => {
res.nodeA.swarm.connect(res.nodeBAddress, cb)
}],
// time for identify
wait: ['connectA2B', (_, cb) => setTimeout(cb, 1000)],
nodeAPeers: ['nodeA', 'wait', (res, cb) => {
res.nodeA.swarm.peers(cb)
}],
nodeBPeers: ['nodeB', 'wait', (res, cb) => {
res.nodeB.swarm.peers(cb)
}]
}, (err, res) => {
expect(err).to.not.exist()
expect(res.nodeAPeers).to.have.length(1)
expect(res.nodeBPeers).to.have.length(1)
done()
})
const nodeA = await common.setup({}, { config })
const nodeB = await common.setup({}, { config })
await nodeA.swarm.connect(nodeB.peerId.addresses[0])
await delay(1000)
const peersA = await nodeA.swarm.peers()
const peersB = await nodeB.swarm.peers()
expect(peersA).to.have.length(1)
expect(peersB).to.have.length(1)
})

it('should list peers only once even if they have multiple addresses', (done) => {
it('should list peers only once even if they have multiple addresses', async () => {
// TODO: Change to port 0, needs: https://github.com/ipfs/interface-ipfs-core/issues/152
const configA = getConfig([
'/ip4/127.0.0.1/tcp/16543',
Expand All @@ -166,35 +125,14 @@ module.exports = (createCommon, options) => {
'/ip4/127.0.0.1/tcp/26545',
'/ip4/127.0.0.1/tcp/26546'
])

auto({
nodeA: (cb) => ipfsFactory.spawnNode(getRepoPath(), configA, cb),
nodeB: ['nodeA', (_, cb) => {
ipfsFactory.spawnNode(getRepoPath(), configB, cb)
}],
nodeBAddress: ['nodeB', (res, cb) => {
res.nodeB.id((err, info) => {
if (err) return cb(err)
cb(null, info.addresses[0])
})
}],
connectA2B: ['nodeA', 'nodeBAddress', (res, cb) => {
res.nodeA.swarm.connect(res.nodeBAddress, cb)
}],
// time for identify
wait: ['connectA2B', (_, cb) => setTimeout(cb, 1000)],
nodeAPeers: ['nodeA', 'wait', (res, cb) => {
res.nodeA.swarm.peers(cb)
}],
nodeBPeers: ['nodeB', 'wait', (res, cb) => {
res.nodeB.swarm.peers(cb)
}]
}, (err, res) => {
expect(err).to.not.exist()
expect(res.nodeAPeers).to.have.length(1)
expect(res.nodeBPeers).to.have.length(1)
done()
})
const nodeA = await common.setup({}, { configA })
const nodeB = await common.setup({}, { configB })
await nodeA.swarm.connect(nodeB.peerId.addresses[0])
await delay(1000)
const peersA = await nodeA.swarm.peers()
const peersB = await nodeB.swarm.peers()
expect(peersA).to.have.length(1)
expect(peersB).to.have.length(1)
})
})
}

0 comments on commit 00341f9

Please sign in to comment.