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

chore: convert tests async await #547

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
18694bc
chore: bitswap async/await refactor
Oct 15, 2019
fe10d34
chore: block async/await refactor
Oct 16, 2019
729c944
chore: bootstrap async/await refactor
Oct 16, 2019
dcde4fd
chore: config async/await refactor
Oct 16, 2019
0a989a3
chore: dht async/await refactor
Oct 16, 2019
53c0d60
chore: files-mfs async/await refactor
Oct 18, 2019
a5e8194
chore: files-regular async/await refactor
Oct 22, 2019
4a2a968
chore: key async/await refactor
Oct 22, 2019
da306ee
chore: miscellaneous async/await refactor
Oct 22, 2019
22db5a6
chore: name async/await refactor
Oct 22, 2019
2b11917
chore: name-pubsub async/await refactor
Oct 22, 2019
0124e76
chore: object async/await refactor
Oct 22, 2019
c92e0cb
chore: pin async/await refactor
Oct 22, 2019
2f09530
chore: ping async/await refactor
Oct 22, 2019
cc75bb7
chore: pubsub async/await refactor
Oct 22, 2019
570c86c
chore: repo async/await refactor
Oct 22, 2019
b780517
chore: stats async/await refactor
Oct 22, 2019
e2bb172
chore: swarm async/await refactor
Oct 22, 2019
355d61e
chore: remove delay util file in favor of 'delay' module
Oct 22, 2019
dcaf0a3
chore: dag async/await refactor
Oct 22, 2019
d9812ec
chore: convert missing object.get api to async/await syntax
Oct 28, 2019
91adca1
chore: convert before function on files-mfs to async/await
Oct 28, 2019
d02e535
chore: dedup existing promise-based tests
Oct 28, 2019
c1172c2
chore: update assertion on failure tests
Oct 29, 2019
2b4fbf5
chore: code review changes
Nov 6, 2019
8bd5541
chore: more code review changes
Nov 11, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@
},
"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",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"cids": "~0.7.1",
"concat-stream": "^2.0.0",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"es6-promisify": "^6.0.2",
"get-stream": "^5.1.0",
"hat": "0.0.3",
"ipfs-block": "~0.8.0",
"ipfs-unixfs": "~0.1.16",
Expand All @@ -60,10 +57,16 @@
"multiaddr": "^6.0.0",
"multibase": "~0.6.0",
"multihashes": "~0.4.14",
"multihashing-async": "~0.6.0",
"multihashing-async": "~0.8.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.12.0",
"peer-info": "~0.15.0",
"pull-stream": "^3.6.14",
"pull-to-promise": "^1.0.1",
"pump": "^3.0.0",
"readable-stream": "^3.1.1",
"streaming-iterables": "^4.1.0",
Expand Down
21 changes: 4 additions & 17 deletions src/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,16 @@ module.exports = (common, options) => {

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

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

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

it('should not get bitswap stats when offline', async () => {
const node = await common.node()
await node.stop()

try {
await node.api.bitswap.stat()
throw new Error('should error')
} catch (err) {
expect(err).to.exist()
}
await expect(node.api.bitswap.stat()).to.be.rejected()
})
})
}
32 changes: 10 additions & 22 deletions src/bitswap/utils.js
Original file line number Diff line number Diff line change
@@ -1,36 +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 || {}
function waitForWantlistKey (ipfs, key, opts = {}) {
opts.timeout = opts.timeout || 10000

let list = { Keys: [] }

const start = Date.now()
const test = () => list.Keys.some(k => k['/'] === key)
const iteratee = (cb) => {
const test = () => !list.Keys.some(k => k['/'] === key)

const iteratee = async () => {
if (Date.now() - start > opts.timeout) {
return cb(new Error(`Timed out waiting for ${key} in wantlist`))
throw new Error(`Timed out waiting for ${key} in wantlist`)
}
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)
}
cb()
})

return pWhilst(test, iteratee)
}

module.exports.waitForWantlistKey = waitForWantlistKey
15 changes: 5 additions & 10 deletions src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,19 @@ module.exports = (common, options) => {

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) => {
waitForWantlistKey(ipfsA, key, { peerId: ipfsB.peerId.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', async () => {
const node = await common.node()
await node.stop()

try {
await node.bitswap.wantlist()
throw new Error('should error')
} catch (err) {
expect(err).to.exist()
}
await expect(node.api.bitswap.wantlist()).to.be.rejected()
})
})
}
86 changes: 33 additions & 53 deletions src/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,86 +27,66 @@ module.exports = (common, options) => {

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

it('should get by CID object', (done) => {
it('should get by CID object', async () => {
const cid = new CID(hash)
const block = await ipfs.block.get(cid)

ipfs.block.get(cid, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(cid.multihash)
done()
})
expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(cid.multihash)
})

it('should get by CID in string', (done) => {
ipfs.block.get(multihash.toB58String(hash), (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(hash)
done()
})
it('should get by CID in string', async () => {
const block = await ipfs.block.get(multihash.toB58String(hash))

expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(hash)
})

it('should get an empty block', (done) => {
ipfs.block.put(Buffer.alloc(0), {
it('should get an empty block', async () => {
const res = await ipfs.block.put(Buffer.alloc(0), {
format: 'dag-pb',
mhtype: 'sha2-256',
version: 0
}, (err, block) => {
expect(err).to.not.exist()

ipfs.block.get(block.cid, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(Buffer.alloc(0))
done()
})
})

const block = await ipfs.block.get(res.cid)

expect(block.data).to.eql(Buffer.alloc(0))
})

it('should get a block added as CIDv0 with a CIDv1', done => {
it('should get a block added as CIDv0 with a CIDv1', async () => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.block.put(input, { version: 0 }, (err, res) => {
expect(err).to.not.exist()
const res = await ipfs.block.put(input, { version: 0 })

const cidv0 = res.cid
expect(cidv0.version).to.equal(0)
const cidv0 = res.cid
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()
const cidv1 = cidv0.toV1()

ipfs.block.get(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output.data).to.eql(input)
done()
})
})
const block = await ipfs.block.get(cidv1)
expect(block.data).to.eql(input)
})

it('should get a block added as CIDv1 with a CIDv0', done => {
it('should get a block added as CIDv1 with a CIDv0', async () => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.block.put(input, { version: 1 }, (err, res) => {
expect(err).to.not.exist()
const res = await ipfs.block.put(input, { version: 1 })

const cidv1 = res.cid
expect(cidv1.version).to.equal(1)
const cidv1 = res.cid
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()
const cidv0 = cidv1.toV0()

ipfs.block.get(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output.data).to.eql(input)
done()
})
})
const block = await ipfs.block.get(cidv0)
expect(block.data).to.eql(input)
})

it('should return an error for an invalid CID', () => {
return ipfs.block.get('invalid')
.then(
() => expect.fail('should have returned an error for invalid argument'),
(err) => expect(err).to.be.an.instanceof(Error)
)
return expect(ipfs.block.get('invalid')).to.eventually.be.rejected
.and.be.an.instanceOf(Error)
.and.have.property('message')
.that.include('Non-base58 character')
})
})
}
59 changes: 24 additions & 35 deletions src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,68 +25,57 @@ module.exports = (common, options) => {

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

it('should put a buffer, using defaults', (done) => {
it('should put a buffer, using defaults', async () => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const blob = Buffer.from('blorb')

ipfs.block.put(blob, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
done()
})
const block = await ipfs.block.put(blob)

expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
})

it('should put a buffer, using CID', (done) => {
it('should put a buffer, using CID', async () => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(expectedHash)
const blob = Buffer.from('blorb')

ipfs.block.put(blob, { cid: cid }, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
done()
})
const block = await ipfs.block.put(blob, { cid: cid })

expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
})

it('should put a buffer, using options', (done) => {
it('should put a buffer, using options', async () => {
const blob = Buffer.from(`TEST${Date.now()}`)

ipfs.block.put(blob, {
const block = await ipfs.block.put(blob, {
format: 'raw',
mhtype: 'sha2-512',
version: 1
}, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.be.eql(blob)
expect(block.cid.version).to.equal(1)
expect(block.cid.codec).to.equal('raw')
expect(multihash.decode(block.cid.multihash).name).to.equal('sha2-512')
done()
})

expect(block.data).to.be.eql(blob)
expect(block.cid.version).to.equal(1)
expect(block.cid.codec).to.equal('raw')
expect(multihash.decode(block.cid.multihash).name).to.equal('sha2-512')
})

it('should put a Block instance', (done) => {
it('should put a Block instance', async () => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(expectedHash)
const b = new Block(Buffer.from('blorb'), cid)

ipfs.block.put(b, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
done()
})
const block = await ipfs.block.put(b)

expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
})

it('should error with array of blocks', (done) => {
it('should error with array of blocks', () => {
const blob = Buffer.from('blorb')

ipfs.block.put([blob, blob], (err) => {
expect(err).to.be.an.instanceof(Error)
done()
})
return expect(ipfs.block.put([blob, blob])).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
})
}
24 changes: 7 additions & 17 deletions src/block/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,21 @@ module.exports = (common, options) => {

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

it('should stat by CID', (done) => {
it('should stat by CID', async () => {
const cid = new CID(hash)

ipfs.block.stat(cid, (err, stats) => {
expect(err).to.not.exist()
expect(stats).to.have.property('key')
expect(stats).to.have.property('size')
done()
})
const stats = await ipfs.block.stat(cid)

expect(stats).to.have.property('key')
expect(stats).to.have.property('size')
})

it('should return error for missing argument', () => {
return ipfs.block.stat(null)
.then(
() => expect.fail('should have thrown for missing parameter'),
(err) => expect(err).to.be.an.instanceof(Error)
)
return expect(ipfs.block.stat(null)).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})

it('should return error for invalid argument', () => {
return ipfs.block.stat('invalid')
.then(
() => expect.fail('should have thrown for invalid parameter'),
(err) => expect(err).to.be.an.instanceof(Error)
)
return expect(ipfs.block.stat('invalid')).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
})
}
Loading