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

Commit

Permalink
feat(breaking change): use stream on stats.bw
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 31, 2018
1 parent 9d91267 commit 17aebbf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion SPEC/STATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Where:
- `poll` is used to print bandwidth at an interval.
- `interval` is the time interval to wait between updating output, if `poll` is true.

`callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful. `stats` is an Object containing the following keys:
If `poll` is `true`, then `callback` must follow `function (err, stream) {}` signature, where `err` is an error if the operation was not successful and `stream` is a Readable Stream where you can listen to the event `data` with a listener that must follow `function (stat) {}` signature.

Otherwise, `callback` must follow `function (err, stat) {}` signature, where `err` is an error and `stat` is an Object containing the following keys:

- `totalIn`
- `totalOut`
Expand Down
42 changes: 42 additions & 0 deletions js/src/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ module.exports = (common) => {
})
})

it('.bw Poll', (done) => {
if (!withGo) {
console.log('Not supported in js-ipfs yet')
return done()
}

ipfs.stats.bw({poll: true}, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()

res.once('data', (data) => {
expect(data).to.have.a.property('totalIn')
expect(data).to.have.a.property('totalOut')
expect(data).to.have.a.property('rateIn')
expect(data).to.have.a.property('rateOut')
done()
})
})
})

it('.bw Promise', () => {
if (!withGo) {
console.log('Not supported in js-ipfs yet')
Expand All @@ -110,6 +130,28 @@ module.exports = (common) => {
})
})

it('.bw Promise Poll', (done) => {
if (!withGo) {
console.log('Not supported in js-ipfs yet')
return
}

ipfs.stats.bw({poll: true}).then((res) => {
expect(res).to.exist()

res.once('data', (data) => {
expect(data).to.have.a.property('totalIn')
expect(data).to.have.a.property('totalOut')
expect(data).to.have.a.property('rateIn')
expect(data).to.have.a.property('rateOut')
done()
})
}).catch(err => {
expect(err).to.not.exist()
done()
})
})

it('.repo', (done) => {
if (!withGo) {
console.log('Not supported in js-ipfs yet')
Expand Down

0 comments on commit 17aebbf

Please sign in to comment.