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

Added in a test for the opts parameter. #506

Merged
merged 7 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions SPEC/OBJECT.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ A great source of [examples][] can be found in the tests for this API.

`options` is a optional argument of type object, that can contain the following properties:

- `enc`, the encoding of multihash (base58, base64, etc), if any.
- `timeout`, A timeout to pass to the IPFS daemon so the request expires after a certain amount of time without any response. NOTE: not yet supported in JS IPFS.

`callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful and `stats` is an Object with following format:

Expand All @@ -220,7 +220,7 @@ If no `callback` is passed, a [promise][] is returned.
```JavaScript
const multihash = 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD'

ipfs.object.stat(multihash, (err, stats) => {
ipfs.object.stat(multihash, {timeout: '10s'}, (err, stats) => {
if (err) {
throw err
}
Expand Down
31 changes: 28 additions & 3 deletions src/object/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = (createCommon, options) => {
}

await ipfs.object.put(testObj)
const stats = await ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', { enc: 'base58' })
const stats = await ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ')

const expected = {
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
Expand All @@ -81,6 +81,31 @@ module.exports = (createCommon, options) => {
expect(expected).to.deep.equal(stats)
})

it('should respect timeout option', (done) => {
const testObj = {
Data: Buffer.from('get test object'),
Links: []
}

ipfs.object.put(testObj, (err) => {
expect(err).to.not.exist()
const timeout = 2
const startTime = new Date()
const badCid = 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ'

// we can test that we are passing in opts by testing the timeout option for a CID that doesn't exist
ipfs.object.stat(badCid, { timeout: `${timeout}s` }, (err, stats) => {
const timeForRequest = (new Date() - startTime) / 1000
expect(err).to.exist()
expect(err.message).to.equal('failed to get block for QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ: context deadline exceeded')
expect(stats).to.not.exist()
expect(timeForRequest).to.not.lessThan(timeout)
expect(timeForRequest).to.not.greaterThan(timeout + 1)
done()
})
})
})

it('should get stats for object with links by multihash', (done) => {
let node1a
let node1b
Expand Down Expand Up @@ -151,7 +176,7 @@ module.exports = (createCommon, options) => {
ipfs.object.put(testObj, (err, cid) => {
expect(err).to.not.exist()

ipfs.object.stat(cid.buffer, { enc: 'base58' }, (err, stats) => {
ipfs.object.stat(cid.buffer, (err, stats) => {
expect(err).to.not.exist()
const expected = {
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
Expand All @@ -176,7 +201,7 @@ module.exports = (createCommon, options) => {
ipfs.object.put(testObj, (err, cid) => {
expect(err).to.not.exist()

ipfs.object.stat(cid.toBaseEncodedString(), { enc: 'base58' }, (err, stats) => {
ipfs.object.stat(cid.toBaseEncodedString(), (err, stats) => {
expect(err).to.not.exist()
const expected = {
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
Expand Down