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

Commit c4e922e

Browse files
hacdiasdaviddias
authored andcommittedDec 6, 2017
fix: stats/bw uses stream (#640)
1 parent a6592dd commit c4e922e

File tree

2 files changed

+124
-2
lines changed

2 files changed

+124
-2
lines changed
 

‎src/stats/bw.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

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

56
module.exports = (send) => {
67
return promisify((opts, callback) => {
@@ -9,9 +10,17 @@ module.exports = (send) => {
910
opts = {}
1011
}
1112

12-
send({
13+
send.andTransform({
1314
path: 'stats/bw',
1415
qs: opts
15-
}, callback)
16+
}, streamToValue, (err, stats) => {
17+
if (err) {
18+
return callback(err)
19+
}
20+
21+
// streamToValue returns an array and we're only
22+
// interested in returning the object itself.
23+
callback(err, stats[0])
24+
})
1625
})
1726
}

‎test/stats.spec.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const FactoryClient = require('./ipfs-factory/client')
5+
const chai = require('chai')
6+
const dirtyChai = require('dirty-chai')
7+
const expect = chai.expect
8+
chai.use(dirtyChai)
9+
10+
describe('stats', function () {
11+
this.timeout(50 * 1000) // slow CI
12+
13+
let ipfs
14+
let fc
15+
16+
before((done) => {
17+
fc = new FactoryClient()
18+
fc.spawnNode((err, node) => {
19+
expect(err).to.not.exist()
20+
ipfs = node
21+
done()
22+
})
23+
})
24+
25+
after((done) => {
26+
fc.dismantle(done)
27+
})
28+
29+
describe('Callback API', () => {
30+
it('.stats.bitswap', (done) => {
31+
ipfs.stats.bitswap((err, res) => {
32+
expect(err).to.not.exist()
33+
expect(res).to.exist()
34+
expect(res).to.have.a.property('ProvideBufLen')
35+
expect(res).to.have.a.property('Wantlist')
36+
expect(res).to.have.a.property('Peers')
37+
expect(res).to.have.a.property('BlocksReceived')
38+
expect(res).to.have.a.property('DataReceived')
39+
expect(res).to.have.a.property('BlocksSent')
40+
expect(res).to.have.a.property('DataSent')
41+
expect(res).to.have.a.property('DupBlksReceived')
42+
expect(res).to.have.a.property('DupDataReceived')
43+
done()
44+
})
45+
})
46+
47+
it('.stats.bw', (done) => {
48+
ipfs.stats.bw((err, res) => {
49+
expect(err).to.not.exist()
50+
expect(res).to.exist()
51+
expect(res).to.have.a.property('TotalIn')
52+
expect(res).to.have.a.property('TotalOut')
53+
expect(res).to.have.a.property('RateIn')
54+
expect(res).to.have.a.property('RateOut')
55+
done()
56+
})
57+
})
58+
59+
it('.stats.repo', (done) => {
60+
ipfs.stats.repo((err, res) => {
61+
expect(err).to.not.exist()
62+
expect(res).to.exist()
63+
expect(res).to.have.a.property('NumObjects')
64+
expect(res).to.have.a.property('RepoSize')
65+
expect(res).to.have.a.property('RepoPath')
66+
expect(res).to.have.a.property('Version')
67+
expect(res).to.have.a.property('StorageMax')
68+
done()
69+
})
70+
})
71+
})
72+
73+
describe('Promise API', () => {
74+
it('.stats.bw', () => {
75+
return ipfs.stats.bw()
76+
.then((res) => {
77+
expect(res).to.exist()
78+
expect(res).to.have.a.property('TotalIn')
79+
expect(res).to.have.a.property('TotalOut')
80+
expect(res).to.have.a.property('RateIn')
81+
expect(res).to.have.a.property('RateOut')
82+
})
83+
})
84+
85+
it('.stats.repo', () => {
86+
return ipfs.stats.repo()
87+
.then((res) => {
88+
expect(res).to.exist()
89+
expect(res).to.have.a.property('NumObjects')
90+
expect(res).to.have.a.property('RepoSize')
91+
expect(res).to.have.a.property('RepoPath')
92+
expect(res).to.have.a.property('Version')
93+
expect(res).to.have.a.property('StorageMax')
94+
})
95+
})
96+
97+
it('.stats.bitswap', () => {
98+
return ipfs.stats.bitswap()
99+
.then((res) => {
100+
expect(res).to.exist()
101+
expect(res).to.have.a.property('ProvideBufLen')
102+
expect(res).to.have.a.property('Wantlist')
103+
expect(res).to.have.a.property('Peers')
104+
expect(res).to.have.a.property('BlocksReceived')
105+
expect(res).to.have.a.property('DataReceived')
106+
expect(res).to.have.a.property('BlocksSent')
107+
expect(res).to.have.a.property('DataSent')
108+
expect(res).to.have.a.property('DupBlksReceived')
109+
expect(res).to.have.a.property('DupDataReceived')
110+
})
111+
})
112+
})
113+
})

0 commit comments

Comments
 (0)
This repository has been archived.