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

fix: stats/bw uses stream #640

Merged
merged 7 commits into from
Dec 6, 2017
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
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')
})
})
})
})