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

Commit 2893423

Browse files
obo20Alan Shaw
and
Alan Shaw
committed
test: added test for object.stat timeout option (#506)
The opts parameter wasn't correctly passed in when calling ipfs.object.stat. This is the test that makes sure opts is being correctly passed to through the send function to the IPFS daemon. I've also removed the "base58" encoding that was being passed in for some tests as base58 isn't a valid option for encoding and would provide this error: Uncaught AssertionError: expected [Error: invalid encoding: base58] to not exist Co-Authored-By: Alan Shaw <[email protected]>
1 parent f7fb043 commit 2893423

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

SPEC/OBJECT.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ A great source of [examples][] can be found in the tests for this API.
198198

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

201-
- `enc`, the encoding of multihash (base58, base64, etc), if any.
201+
- `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.
202202

203203
`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:
204204

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

223-
ipfs.object.stat(multihash, (err, stats) => {
223+
ipfs.object.stat(multihash, {timeout: '10s'}, (err, stats) => {
224224
if (err) {
225225
throw err
226226
}

src/object/stat.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports = (createCommon, options) => {
6767
}
6868

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

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

84+
it('should respect timeout option', (done) => {
85+
const testObj = {
86+
Data: Buffer.from('get test object'),
87+
Links: []
88+
}
89+
90+
ipfs.object.put(testObj, (err) => {
91+
expect(err).to.not.exist()
92+
const timeout = 2
93+
const startTime = new Date()
94+
const badCid = 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ'
95+
96+
// we can test that we are passing in opts by testing the timeout option for a CID that doesn't exist
97+
ipfs.object.stat(badCid, { timeout: `${timeout}s` }, (err, stats) => {
98+
const timeForRequest = (new Date() - startTime) / 1000
99+
expect(err).to.exist()
100+
expect(err.message).to.equal('failed to get block for QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ: context deadline exceeded')
101+
expect(stats).to.not.exist()
102+
expect(timeForRequest).to.not.lessThan(timeout)
103+
expect(timeForRequest).to.not.greaterThan(timeout + 1)
104+
done()
105+
})
106+
})
107+
})
108+
84109
it('should get stats for object with links by multihash', (done) => {
85110
let node1a
86111
let node1b
@@ -151,7 +176,7 @@ module.exports = (createCommon, options) => {
151176
ipfs.object.put(testObj, (err, cid) => {
152177
expect(err).to.not.exist()
153178

154-
ipfs.object.stat(cid.buffer, { enc: 'base58' }, (err, stats) => {
179+
ipfs.object.stat(cid.buffer, (err, stats) => {
155180
expect(err).to.not.exist()
156181
const expected = {
157182
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
@@ -176,7 +201,7 @@ module.exports = (createCommon, options) => {
176201
ipfs.object.put(testObj, (err, cid) => {
177202
expect(err).to.not.exist()
178203

179-
ipfs.object.stat(cid.toBaseEncodedString(), { enc: 'base58' }, (err, stats) => {
204+
ipfs.object.stat(cid.toBaseEncodedString(), (err, stats) => {
180205
expect(err).to.not.exist()
181206
const expected = {
182207
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',

0 commit comments

Comments
 (0)