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

Commit

Permalink
fix: stats/bw uses stream (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias authored and daviddias committed Dec 6, 2017
1 parent a6592dd commit c4e922e
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/stats/bw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const promisify = require('promisify-es6')
const streamToValue = require('../utils/stream-to-value')

module.exports = (send) => {
return promisify((opts, callback) => {
Expand All @@ -9,9 +10,17 @@ module.exports = (send) => {
opts = {}
}

send({
send.andTransform({
path: 'stats/bw',
qs: opts
}, callback)
}, streamToValue, (err, stats) => {
if (err) {
return callback(err)
}

// streamToValue returns an array and we're only
// interested in returning the object itself.
callback(err, stats[0])
})
})
}
113 changes: 113 additions & 0 deletions test/stats.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* eslint-env mocha */
'use strict'

const FactoryClient = require('./ipfs-factory/client')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

describe('stats', function () {
this.timeout(50 * 1000) // slow CI

let ipfs
let fc

before((done) => {
fc = new FactoryClient()
fc.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})

after((done) => {
fc.dismantle(done)
})

describe('Callback API', () => {
it('.stats.bitswap', (done) => {
ipfs.stats.bitswap((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.a.property('ProvideBufLen')
expect(res).to.have.a.property('Wantlist')
expect(res).to.have.a.property('Peers')
expect(res).to.have.a.property('BlocksReceived')
expect(res).to.have.a.property('DataReceived')
expect(res).to.have.a.property('BlocksSent')
expect(res).to.have.a.property('DataSent')
expect(res).to.have.a.property('DupBlksReceived')
expect(res).to.have.a.property('DupDataReceived')
done()
})
})

it('.stats.bw', (done) => {
ipfs.stats.bw((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.a.property('TotalIn')
expect(res).to.have.a.property('TotalOut')
expect(res).to.have.a.property('RateIn')
expect(res).to.have.a.property('RateOut')
done()
})
})

it('.stats.repo', (done) => {
ipfs.stats.repo((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.a.property('NumObjects')
expect(res).to.have.a.property('RepoSize')
expect(res).to.have.a.property('RepoPath')
expect(res).to.have.a.property('Version')
expect(res).to.have.a.property('StorageMax')
done()
})
})
})

describe('Promise API', () => {
it('.stats.bw', () => {
return ipfs.stats.bw()
.then((res) => {
expect(res).to.exist()
expect(res).to.have.a.property('TotalIn')
expect(res).to.have.a.property('TotalOut')
expect(res).to.have.a.property('RateIn')
expect(res).to.have.a.property('RateOut')
})
})

it('.stats.repo', () => {
return ipfs.stats.repo()
.then((res) => {
expect(res).to.exist()
expect(res).to.have.a.property('NumObjects')
expect(res).to.have.a.property('RepoSize')
expect(res).to.have.a.property('RepoPath')
expect(res).to.have.a.property('Version')
expect(res).to.have.a.property('StorageMax')
})
})

it('.stats.bitswap', () => {
return ipfs.stats.bitswap()
.then((res) => {
expect(res).to.exist()
expect(res).to.have.a.property('ProvideBufLen')
expect(res).to.have.a.property('Wantlist')
expect(res).to.have.a.property('Peers')
expect(res).to.have.a.property('BlocksReceived')
expect(res).to.have.a.property('DataReceived')
expect(res).to.have.a.property('BlocksSent')
expect(res).to.have.a.property('DataSent')
expect(res).to.have.a.property('DupBlksReceived')
expect(res).to.have.a.property('DupDataReceived')
})
})
})
})

0 comments on commit c4e922e

Please sign in to comment.