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

Commit

Permalink
chore: make build more stable (#3107)
Browse files Browse the repository at this point in the history
Fixes a lot of random timeouts and unhandled promise rejections.
  • Loading branch information
achingbrain authored Jun 22, 2020
1 parent b7868ae commit 87bff06
Show file tree
Hide file tree
Showing 30 changed files with 128 additions and 114 deletions.
2 changes: 1 addition & 1 deletion packages/interface-ipfs-core/src/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module.exports = (common, options) => {

const dir = files[0]

const err = await expect(concat(ipfs.cat(dir.cid))).to.be.rejected()
const err = await expect(concat(ipfs.cat(dir.cid))).to.eventually.be.rejected()
expect(err.message).to.contain('this dag node is a directory')
})

Expand Down
15 changes: 9 additions & 6 deletions packages/interface-ipfs-core/src/dht/put.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const testTimeout = require('../utils/test-timeout')
const CID = require('cids')
const all = require('it-all')
const last = require('it-last')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -28,15 +27,19 @@ module.exports = (common, options) => {

after(() => common.clean())

it('should respect timeout option when putting a value into the DHT', () => {
return testTimeout(() => nodeA.dht.put(new CID('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ'), Buffer.from('derp'), {
it('should respect timeout option when putting a value into the DHT', async () => {
const { cid } = await last(nodeA.add('should respect timeout option when putting a value into the DH'))
const publish = await nodeA.name.publish(cid)
const record = await nodeA.dht.get(`/ipns/${publish.name}`)

await testTimeout(() => nodeA.dht.put(`/ipns/${publish.name}`, record, {
timeout: 1
}))
})

it('should put a value to the DHT', async function () {
const [data] = await all(nodeA.add('should put a value to the DHT'))
const publish = await nodeA.name.publish(data.cid)
const { cid } = await last(nodeA.add('should put a value to the DHT'))
const publish = await nodeA.name.publish(cid)
const record = await nodeA.dht.get(`/ipns/${publish.name}`)
const value = await all(nodeA.dht.put(`/ipns/${publish.name}`, record, { verbose: true }))
expect(value).to.has.length(3)
Expand Down
12 changes: 6 additions & 6 deletions packages/interface-ipfs-core/src/files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const { createSuite } = require('../utils/suite')

const tests = {
chmod: require('./chmod'),
mkdir: require('./mkdir'),
write: require('./write'),
cp: require('./cp'),
flush: require('./flush'),
ls: require('./ls'),
mkdir: require('./mkdir'),
mv: require('./mv'),
read: require('./read'),
rm: require('./rm'),
stat: require('./stat'),
read: require('./read'),
ls: require('./ls'),
flush: require('./flush'),
touch: require('./touch')
touch: require('./touch'),
write: require('./write')
}

module.exports = createSuite(tests)
6 changes: 3 additions & 3 deletions packages/interface-ipfs-core/src/files/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ module.exports = (common, options) => {
after(() => common.clean())

it('refuses to stat files with an empty path', async () => {
await expect(ipfs.files.stat('')).to.be.rejected()
await expect(ipfs.files.stat('')).to.eventually.be.rejected()
})

it('refuses to lists files with an invalid path', async () => {
await expect(ipfs.files.stat('not-valid')).to.be.rejectedWith(/paths must start with a leading slash/)
await expect(ipfs.files.stat('not-valid')).to.eventually.be.rejectedWith(/paths must start with a leading slash/)
})

it('fails to stat non-existent file', async () => {
await expect(ipfs.files.stat('/i-do-not-exist')).to.be.rejectedWith(/does not exist/)
await expect(ipfs.files.stat('/i-do-not-exist')).to.eventually.be.rejectedWith(/does not exist/)
})

it('stats an empty directory', async () => {
Expand Down
31 changes: 7 additions & 24 deletions packages/interface-ipfs-core/src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const CID = require('cids')
const all = require('it-all')
const concat = require('it-concat')
const drain = require('it-drain')
const last = require('it-last')
const { getDescribe, getIt, expect } = require('./utils/mocha')
const testTimeout = require('./utils/test-timeout')
const importer = require('ipfs-unixfs-importer')
Expand Down Expand Up @@ -162,36 +163,18 @@ module.exports = (common, options) => {
content: fixtures.smallFile.data
}

for await (const fileAdded of importer([file], ipfs.block)) {
if (fileAdded.path === 'a') {
const files = await all(ipfs.get(`/ipfs/${fileAdded.cid.toString()}/testfile.txt`))
expect(files).to.be.length(1)
expect((await concat(files[0].content)).toString()).to.contain('Plz add me!')
}
}
})
const fileAdded = await last(importer([file], ipfs.block))
expect(fileAdded).to.have.property('path', 'a')

it('should get with ipfs path, as array and nested value', async () => {
const file = {
path: 'a/testfile.txt',
content: fixtures.smallFile.data
}

const filesAdded = await all(importer([file], ipfs.block))

filesAdded.forEach(async (file) => {
if (file.path === 'a') {
const files = await all(ipfs.get(`/ipfs/${file.cid}/testfile.txt`))
expect(files).to.be.length(1)
expect((await concat(files[0].content)).toString()).to.contain('Plz add me!')
}
})
const files = await all(ipfs.get(`/ipfs/${fileAdded.cid}/testfile.txt`))
expect(files).to.be.length(1)
expect((await concat(files[0].content)).toString()).to.contain('Plz add me!')
})

it('should error on invalid key', async () => {
const invalidCid = 'somethingNotMultihash'

const err = await expect(all(ipfs.get(invalidCid))).to.be.rejected()
const err = await expect(all(ipfs.get(invalidCid))).to.eventually.be.rejected()

switch (err.toString()) {
case 'Error: invalid ipfs ref path':
Expand Down
2 changes: 1 addition & 1 deletion packages/interface-ipfs-core/src/name-pubsub/cancel.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = (common, options) => {
const subs = await ipfs.name.pubsub.subs()
expect(subs).to.be.an('array').that.does.not.include(ipnsPath)

await expect(all(ipfs.name.resolve(id))).to.be.rejected()
await expect(all(ipfs.name.resolve(id))).to.eventually.be.rejected()

const subs1 = await ipfs.name.pubsub.subs()
const cancel = await ipfs.name.pubsub.cancel(ipnsPath)
Expand Down
2 changes: 1 addition & 1 deletion packages/interface-ipfs-core/src/name-pubsub/subs.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = (common, options) => {
const subs = await ipfs.name.pubsub.subs()
expect(subs).to.eql([]) // initally empty

await expect(all(ipfs.name.resolve(id))).to.be.rejected()
await expect(all(ipfs.name.resolve(id))).to.eventually.be.rejected()

const res = await ipfs.name.pubsub.subs()
expect(res).to.be.an('array').that.does.include(`/ipns/${id}`)
Expand Down
5 changes: 4 additions & 1 deletion packages/interface-ipfs-core/src/pubsub/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ module.exports = (common, options) => {

await waitForPeers(ipfs2, topic, [ipfs1.peerId.id], 30000)
await delay(5000) // gossipsub need this delay https://github.com/libp2p/go-libp2p-pubsub/issues/331
outbox.forEach(msg => ipfs2.pubsub.publish(topic, Buffer.from(msg)))

for (let i = 0; i < outbox.length; i++) {
await ipfs2.pubsub.publish(topic, Buffer.from(outbox[i]))
}

const sub1Msgs = await all(msgStream1)
sub1Msgs.forEach(msg => expect(msg.from).to.eql(ipfs2.peerId.id))
Expand Down
4 changes: 2 additions & 2 deletions packages/interface-ipfs-core/src/utils/test-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ module.exports = (fn) => {

reject(new Error(`API call did not time out after ${timeTaken}ms, got ${JSON.stringify(result, null, 2)}`))
}, (err) => {
if (err.message.includes('timed out')) {
if (err.name === 'TimeoutError') {
return resolve()
}

const timeTaken = Date.now() - start

reject(new Error(`Expected error to include 'timed out' after ${timeTaken}ms, got ${err.stack}`))
reject(new Error(`Expected TimeoutError after ${timeTaken}ms, got ${err.stack}`))
})
}, 10)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/test/dag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('.dag', function () {
cid: new CID('z8mWaJ1dZ9fH5EetPuRsj8jj26pXsgpsr')
})

await expect(ipfs.dag.get(block.cid)).to.be.rejectedWith('Missing IPLD format "git-raw"')
await expect(ipfs.dag.get(block.cid)).to.eventually.be.rejectedWith('Missing IPLD format "git-raw"')
})

it('should error when putting node with esoteric format', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/test/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('.get (specific go-ipfs features)', function () {
await expect(all(ipfs.get(smallFile.cid, {
compress: true,
compressionLevel: 10
}))).to.be.rejectedWith('compression level must be between 1 and 9')
}))).to.eventually.be.rejectedWith('compression level must be between 1 and 9')
})

// TODO Understand why this test started failing
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/test/node/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('.swarm.peers', function () {
.query(true)
.replyWithError('something awful happened')

await expect(ipfs.swarm.peers()).to.be.rejectedWith('something awful happened')
await expect(ipfs.swarm.peers()).to.eventually.be.rejectedWith('something awful happened')

expect(scope.isDone()).to.equal(true)
})
Expand Down
7 changes: 4 additions & 3 deletions packages/ipfs/src/core/components/files/chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const defaultOptions = {
shardSplitThreshold: 1000,
hashAlg: 'sha2-256',
cidVersion: 0,
recursive: false
recursive: false,
signal: undefined
}

function calculateModification (mode, originalMode, isDirectory) {
Expand Down Expand Up @@ -171,7 +172,7 @@ module.exports = (context) => {
cid,
mfsDirectory,
name
} = await toMfsPath(context, path)
} = await toMfsPath(context, path, options)

if (cid.codec !== 'dag-pb') {
throw errCode(new Error(`${path} was not a UnixFS node`), 'ERR_NOT_UNIXFS')
Expand Down Expand Up @@ -254,6 +255,6 @@ module.exports = (context) => {
const newRootCid = await updateTree(context, trail, options)

// Update the MFS record with the new CID for the root of the tree
await updateMfsRoot(context, newRootCid)
await updateMfsRoot(context, newRootCid, options)
})
}
19 changes: 10 additions & 9 deletions packages/ipfs/src/core/components/files/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const errCode = require('err-code')
const updateTree = require('./utils/update-tree')
const updateMfsRoot = require('./utils/update-mfs-root')
const addLink = require('./utils/add-link')
const applyDefaultOptions = require('./utils/apply-default-options')
const toMfsPath = require('./utils/to-mfs-path')
const toSourcesAndDestination = require('./utils/to-sources-and-destination')
const toTrail = require('./utils/to-trail')
Expand All @@ -18,15 +17,17 @@ const defaultOptions = {
flush: true,
hashAlg: 'sha2-256',
cidVersion: 0,
shardSplitThreshold: 1000
shardSplitThreshold: 1000,
signal: undefined
}

module.exports = (context) => {
return withTimeoutOption(async function mfsCp (...args) {
const options = applyDefaultOptions(args, defaultOptions)
let {
sources, destination
} = await toSourcesAndDestination(context, args)
sources,
destination,
options
} = await toSourcesAndDestination(context, args, defaultOptions)

if (!sources.length) {
throw errCode(new Error('Please supply at least one source'), 'ERR_INVALID_PARAMS')
Expand Down Expand Up @@ -59,7 +60,7 @@ module.exports = (context) => {
}

await mkdir(context)(destination.path, options)
destination = await toMfsPath(context, destination.path)
destination = await toMfsPath(context, destination.path, options)
} else if (destination.parts.length > 1) {
// copying to a folder, create it if necessary
const parentFolder = `/${destination.parts.slice(0, -1).join('/')}`
Expand All @@ -76,7 +77,7 @@ module.exports = (context) => {
}

await mkdir(context)(parentFolder, options)
destination = await toMfsPath(context, destination.path)
destination = await toMfsPath(context, destination.path, options)
}
}
}
Expand Down Expand Up @@ -115,7 +116,7 @@ const copyToFile = async (context, source, destination, destinationTrail, option
const newRootCid = await updateTree(context, destinationTrail, options)

// Update the MFS record with the new CID for the root of the tree
await updateMfsRoot(context, newRootCid)
await updateMfsRoot(context, newRootCid, options)
}

const copyToDirectory = async (context, sources, destination, destinationTrail, options) => {
Expand All @@ -132,7 +133,7 @@ const copyToDirectory = async (context, sources, destination, destinationTrail,
const newRootCid = await updateTree(context, destinationTrail, options)

// Update the MFS record with the new CID for the root of the tree
await updateMfsRoot(context, newRootCid)
await updateMfsRoot(context, newRootCid, options)
}

const addSourceToParent = async (context, source, childName, parent, options) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/ipfs/src/core/components/files/flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const applyDefaultOptions = require('./utils/apply-default-options')
const stat = require('./stat')
const { withTimeoutOption } = require('../../utils')

const defaultOptions = {}
const defaultOptions = {
signal: undefined
}

module.exports = (context) => {
return withTimeoutOption(async function mfsFlush (path = '/', options = defaultOptions) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs/src/core/components/files/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = (context) => {
path = '/'
}

const mfsPath = await toMfsPath(context, path)
const mfsPath = await toMfsPath(context, path, options)
const fsDir = await exporter(mfsPath.mfsPath, context.ipld)

// single file/node
Expand Down
7 changes: 4 additions & 3 deletions packages/ipfs/src/core/components/files/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const defaultOptions = {
shardSplitThreshold: 1000,
flush: true,
mode: null,
mtime: null
mtime: null,
signal: undefined
}

module.exports = (context) => {
Expand Down Expand Up @@ -52,7 +53,7 @@ module.exports = (context) => {
throw errCode(new Error("path cannot have the prefix 'ipfs'"), 'ERR_INVALID_PATH')
}

const root = await withMfsRoot(context)
const root = await withMfsRoot(context, options)
let parent
const trail = []
const emptyDir = await createNode(context, 'directory', options)
Expand Down Expand Up @@ -100,7 +101,7 @@ module.exports = (context) => {
const newRootCid = await updateTree(context, trail, options)

// Update the MFS record with the new CID for the root of the tree
await updateMfsRoot(context, newRootCid)
await updateMfsRoot(context, newRootCid, options)
})
}

Expand Down
14 changes: 5 additions & 9 deletions packages/ipfs/src/core/components/files/mv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const applyDefaultOptions = require('./utils/apply-default-options')
const toSources = require('./utils/to-sources')
const cp = require('./cp')
const rm = require('./rm')
Expand All @@ -12,19 +11,16 @@ const defaultOptions = {
flush: true,
cidVersion: 0,
hashAlg: 'sha2-256',
shardSplitThreshold: 1000
shardSplitThreshold: 1000,
signal: undefined
}

module.exports = (context) => {
return withTimeoutOption(async function mfsMv (...args) {
if (Array.isArray(args[0])) {
args = args[0].concat(args.slice(1))
}

const {
sources
} = await toSources(context, args)
const options = applyDefaultOptions(args, defaultOptions)
sources,
options
} = await toSources(context, args, defaultOptions)

const cpArgs = sources
.map(source => source.path).concat(options)
Expand Down
Loading

0 comments on commit 87bff06

Please sign in to comment.