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

Commit

Permalink
refactor: convert tests to async/await (#562)
Browse files Browse the repository at this point in the history
* chore: bitswap async/await refactor

* chore: block async/await refactor

* chore: bootstrap async/await refactor

* chore: config async/await refactor

* chore: dag async/await refactor

* chore: dht async/await refactor

* chore: files-mfs async/await refactor

* chore: files-regular async/await refactor

* chore: key async/await refactor

* chore: miscellaneous async/await refactor

* chore: name-pubsub async/await refactor

* chore: name async/await refactor

* chore: object async/await refactor

* chore: pin async/await refactor

* chore: ping async/await refactor

* chore: pubsub async/await refactor

* chore: repo async/await refactor

* chore: stats async/await refactor

* chore: swarm async/await refactor

* chore: remove unnecessary util file

* chore: update dependencies

* refactor: remove async dep and unnecessary utils

* refactor: bitswap before and after methods to async syntax

* refactor: block before and after methods to async syntax

* refactor: bootstrap before and after methods to async syntax

* refactor: config before and after methods to async syntax

* refactor: dag before and after methods to async syntax

* refactor: dht before and after methods to async syntax

* refactor: files-mfs before and after methods to async syntax

* refactor: files-regular before and after methods to async syntax

* refactor: key before and after methods to async syntax

* refactor: miscellaneous before and after methods to async syntax

* refactor: name before and after methods to async syntax

* refactor: name-pubsub before and after methods to async syntax

* refactor: object before and after methods to async syntax

* refactor: pin before and after methods to async syntax

* refactor: ping before and after methods to async syntax

* refactor: pubsub before and after methods to async syntax

* refactor: repo before and after methods to async syntax

* refactor: stats before and after methods to async syntax

* chore: remove 'only' from files-mfs/cp

* chore: uncomment bootstrap rm test assertion

The assertion was commented out temporarly due to bootstrap.rm bug that was fixed here ipfs/js-ipfs#2626

* refactor: name resolve test

* fix: increase dht findProvs test timeout

* fix: miscellaneous stop test
  • Loading branch information
Pedro Santos authored and Alan Shaw committed Nov 26, 2019
1 parent c766dbf commit 2ede15d
Show file tree
Hide file tree
Showing 115 changed files with 2,627 additions and 5,943 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
},
"homepage": "https://github.com/ipfs/interface-ipfs-core#readme",
"dependencies": {
"async": "^2.6.2",
"bl": "^3.0.0",
"bs58": "^4.0.1",
"callbackify": "^1.1.0",
Expand All @@ -47,6 +46,7 @@
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"es6-promisify": "^6.0.1",
"get-stream": "^5.1.0",
"hat": "0.0.3",
"ipfs-block": "~0.8.0",
"ipfs-unixfs": "~0.1.16",
Expand All @@ -61,9 +61,15 @@
"multibase": "~0.6.0",
"multihashes": "~0.4.14",
"multihashing-async": "~0.6.0",
"peer-id": "~0.12.0",
"p-each-series": "^2.1.0",
"p-map-series": "^2.1.0",
"p-timeout": "^3.2.0",
"p-times": "^2.1.0",
"p-whilst": "^2.1.0",
"peer-id": "~0.13.5",
"peer-info": "~0.15.0",
"pull-stream": "^3.6.11",
"pull-to-promise": "^1.0.1",
"pump": "^3.0.0",
"readable-stream": "^3.1.1",
"streaming-iterables": "^4.1.0",
Expand Down
52 changes: 12 additions & 40 deletions src/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-env mocha */
'use strict'

const waterfall = require('async/waterfall')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { expectIsBitswap } = require('../stats/utils')

Expand All @@ -10,53 +9,26 @@ module.exports = (createCommon, options) => {
const it = getIt(options)
const common = createCommon()

describe('.bitswap.stat', () => {
describe('.bitswap.stat', 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) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
before(async () => {
ipfs = await common.setup()
})

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

it('should get bitswap stats', (done) => {
ipfs.bitswap.stat((err, res) => {
expectIsBitswap(err, res)
done()
})
it('should get bitswap stats', async () => {
const res = await ipfs.bitswap.stat()
expectIsBitswap(null, res)
})

it('should get bitswap stats (promised)', () => {
return ipfs.bitswap.stat().then((res) => {
expectIsBitswap(null, res)
})
})
it('should not get bitswap stats when offline', async () => {
const node = await createCommon().setup()
await node.stop()

it('should not get bitswap stats when offline', function (done) {
this.timeout(60 * 1000)

waterfall([
(cb) => createCommon().setup(cb),
(factory, cb) => factory.spawnNode(cb),
(node, cb) => node.stop((err) => cb(err, node))
], (err, node) => {
expect(err).to.not.exist()
node.bitswap.wantlist((err) => {
expect(err).to.exist()
done()
})
})
return expect(node.bitswap.stat()).to.eventually.be.rejected()
})
})
}
35 changes: 12 additions & 23 deletions src/bitswap/utils.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
'use strict'

const until = require('async/until')
const pWhilst = require('p-whilst')

function waitForWantlistKey (ipfs, key, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}

opts = opts || {}
opts.timeout = opts.timeout || 1000
function waitForWantlistKey (ipfs, key, opts = {}) {
opts.timeout = opts.timeout || 10000

let list = { Keys: [] }
let timedOut = false

setTimeout(() => { timedOut = true }, opts.timeout)
const start = Date.now()
const findKey = () => !list.Keys.some(k => k['/'] === key)

const iteratee = async () => {
if (Date.now() - start > opts.timeout) {
throw new Error(`Timed out waiting for ${key} in wantlist`)
}

const test = () => timedOut ? true : list.Keys.some(k => k['/'] === key)
const iteratee = (cb) => {
ipfs.bitswap.wantlist(opts.peerId, (err, nextList) => {
if (err) return cb(err)
list = nextList
cb()
})
list = await ipfs.bitswap.wantlist(opts.peerId)
}

until(test, iteratee, (err) => {
if (err) return cb(err)
if (timedOut) return cb(new Error(`Timed out waiting for ${key} in wantlist`))
cb()
})
return pWhilst(findKey, iteratee)
}

module.exports.waitForWantlistKey = waitForWantlistKey
63 changes: 17 additions & 46 deletions src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,45 @@
/* eslint max-nested-callbacks: ["error", 6] */
'use strict'

const waterfall = require('async/waterfall')
const { spawnNodesWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { waitForWantlistKey } = require('./utils')
const { connect } = require('../utils/swarm')

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

describe('.bitswap.wantlist', () => {
describe('.bitswap.wantlist', function () {
this.timeout(60 * 1000)
let ipfsA
let ipfsB
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'

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)
before(async () => {
ipfsA = await common.setup()
ipfsB = await common.setup()

common.setup((err, factory) => {
expect(err).to.not.exist()
// Add key to the wantlist for ipfsB
ipfsB.block.get(key, () => {})

spawnNodesWithId(2, factory, (err, nodes) => {
expect(err).to.not.exist()

ipfsA = nodes[0]
ipfsB = nodes[1]

// Add key to the wantlist for ipfsB
ipfsB.block.get(key, () => {})

connect(ipfsA, ipfsB.peerId.addresses[0], done)
})
})
await ipfsA.swarm.connect(ipfsB.peerId.addresses[0])
})

after(function (done) {
this.timeout(30 * 1000)
common.teardown(done)
})
after(() => common.teardown())

it('should get the wantlist', (done) => {
waitForWantlistKey(ipfsB, key, done)
it('should get the wantlist', () => {
return waitForWantlistKey(ipfsB, key)
})

it('should get the wantlist by peer ID for a diffreent node', (done) => {
ipfsB.id((err, info) => {
expect(err).to.not.exist()
waitForWantlistKey(ipfsA, key, { peerId: info.id }, done)
})
it('should get the wantlist by peer ID for a diffreent node', () => {
return waitForWantlistKey(ipfsA, key, { peerId: ipfsB.peerId.id })
})

it('should not get the wantlist when offline', function (done) {
this.timeout(60 * 1000)
it('should not get the wantlist when offline', async () => {
const node = await createCommon().setup()
await node.stop()

waterfall([
(cb) => createCommon().setup(cb),
(factory, cb) => factory.spawnNode(cb),
(node, cb) => node.stop((err) => cb(err, node))
], (err, node) => {
expect(err).to.not.exist()
node.bitswap.wantlist((err) => {
expect(err).to.exist()
done()
})
})
return expect(node.bitswap.stat()).to.eventually.be.rejected()
})
})
}
Loading

0 comments on commit 2ede15d

Please sign in to comment.