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

Commit

Permalink
feat: add support for async/await setup in the swarm tests (#529)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: swarm tests need promise based setup check PRs below

the following PRs add support async setup:
ipfs-inactive/js-ipfs-http-client#1105
ipfs/js-ipfs#2428
  • Loading branch information
hugomrdias authored Sep 16, 2019
2 parents da79067 + 78d7169 commit 044409c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 161 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
7 changes: 2 additions & 5 deletions src/miscellaneous/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ module.exports = (createCommon, options) => {
const it = getIt(options)
const common = createCommon()

describe('.id', () => {
describe('.id', function () {
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(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
Expand Down
3 changes: 2 additions & 1 deletion src/miscellaneous/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = (createCommon, options) => {
const it = getIt(options)
const common = createCommon()

describe('.resolve', () => {
describe('.resolve', function () {
this.timeout(60 * 1000)
let ipfs
let nodeId

Expand Down
25 changes: 7 additions & 18 deletions src/swarm/addrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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)
Expand All @@ -13,26 +12,16 @@ module.exports = (createCommon, options) => {
describe('.swarm.addrs', function () {
this.timeout(80 * 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
24 changes: 5 additions & 19 deletions src/swarm/connect.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* 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)
Expand All @@ -11,28 +10,15 @@ module.exports = (createCommon, options) => {

describe('.swarm.connect', function () {
this.timeout(80 * 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]
done()
})
})
before(async () => {
ipfsA = await common.setup()
ipfsB = await common.setup()
})

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

it('should connect to a peer', (done) => {
ipfsA.swarm.connect(ipfsB.peerId.addresses[0], done)
Expand Down
24 changes: 6 additions & 18 deletions src/swarm/disconnect.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* 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)
Expand All @@ -15,24 +14,13 @@ module.exports = (createCommon, options) => {
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
17 changes: 3 additions & 14 deletions src/swarm/local-addrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,11 @@ module.exports = (createCommon, options) => {

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
110 changes: 24 additions & 86 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 @@ -20,27 +16,14 @@ module.exports = (createCommon, options) => {

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 044409c

Please sign in to comment.