From 0573c2c643074f9b5fd01dca0adf29a6c066f03c Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 6 Dec 2017 07:55:51 +0000 Subject: [PATCH 1/7] fix: stats/bw uses stream --- src/stats/bw.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/stats/bw.js b/src/stats/bw.js index d9a5155a6..a7a6d274a 100644 --- a/src/stats/bw.js +++ b/src/stats/bw.js @@ -1,6 +1,8 @@ 'use strict' const promisify = require('promisify-es6') +const pump = require('pump') +const ndjson = require('ndjson') module.exports = (send) => { return promisify((opts, callback) => { @@ -12,6 +14,12 @@ module.exports = (send) => { send({ path: 'stats/bw', qs: opts - }, callback) + }, (err, response) => { + if (err) { + return callback(err) + } + const outputStream = pump(response, ndjson.parse()) + callback(null, outputStream) + }) }) } From 6b8eb72f2c4005d3f8ab582285c0d8fa0f4ac762 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 6 Dec 2017 08:04:47 +0000 Subject: [PATCH 2/7] Update bw.js --- src/stats/bw.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/stats/bw.js b/src/stats/bw.js index a7a6d274a..833e52161 100644 --- a/src/stats/bw.js +++ b/src/stats/bw.js @@ -1,8 +1,7 @@ 'use strict' const promisify = require('promisify-es6') -const pump = require('pump') -const ndjson = require('ndjson') +const streamToValue = require('../utils/stream-to-value') module.exports = (send) => { return promisify((opts, callback) => { @@ -10,16 +9,10 @@ module.exports = (send) => { callback = opts opts = {} } - - send({ + + send.andTransform({ path: 'stats/bw', qs: opts - }, (err, response) => { - if (err) { - return callback(err) - } - const outputStream = pump(response, ndjson.parse()) - callback(null, outputStream) - }) + }, streamToValue, callback) }) } From d2a6e7c8f0860358931e640cf2c223f155c13b26 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 6 Dec 2017 08:05:03 +0000 Subject: [PATCH 3/7] Update bw.js --- src/stats/bw.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stats/bw.js b/src/stats/bw.js index 833e52161..fe482a74c 100644 --- a/src/stats/bw.js +++ b/src/stats/bw.js @@ -9,7 +9,7 @@ module.exports = (send) => { callback = opts opts = {} } - + send.andTransform({ path: 'stats/bw', qs: opts From f9983caeb63e86a766dad4d8accc0ba5a5cefd06 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 6 Dec 2017 08:34:29 +0000 Subject: [PATCH 4/7] tests --- src/stats/bw.js | 10 +++- test/stats.spec.js | 114 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 test/stats.spec.js diff --git a/src/stats/bw.js b/src/stats/bw.js index fe482a74c..59d70eb19 100644 --- a/src/stats/bw.js +++ b/src/stats/bw.js @@ -13,6 +13,14 @@ module.exports = (send) => { send.andTransform({ path: 'stats/bw', qs: opts - }, streamToValue, 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]) + }) }) } diff --git a/test/stats.spec.js b/test/stats.spec.js new file mode 100644 index 000000000..323083fb2 --- /dev/null +++ b/test/stats.spec.js @@ -0,0 +1,114 @@ +/* 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() + console.log(res) + 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') + }) + }) + }) +}) From b45ff857d82c7106391120b31714fec56d47f997 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 6 Dec 2017 08:44:15 +0000 Subject: [PATCH 5/7] Remove trailing spaces --- test/stats.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/stats.spec.js b/test/stats.spec.js index 323083fb2..23c88f627 100644 --- a/test/stats.spec.js +++ b/test/stats.spec.js @@ -56,7 +56,7 @@ describe('stats', function () { done() }) }) - + it('.stats.repo', (done) => { ipfs.stats.repo((err, res) => { expect(err).to.not.exist() @@ -110,5 +110,5 @@ describe('stats', function () { expect(res).to.have.a.property('DupDataReceived') }) }) - }) + }) }) From 508df81b12051dadcd1093189b373958a5e7eba3 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 6 Dec 2017 08:44:34 +0000 Subject: [PATCH 6/7] Remove trailing spaces --- src/stats/bw.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stats/bw.js b/src/stats/bw.js index 59d70eb19..a7ee05677 100644 --- a/src/stats/bw.js +++ b/src/stats/bw.js @@ -17,7 +17,7 @@ module.exports = (send) => { if (err) { return callback(err) } - + // streamToValue returns an array and we're only // interested in returning the object itself. callback(err, stats[0]) From 0bc8c083545f15d16a9d67ea1d329dbf74d462f9 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 6 Dec 2017 08:45:10 +0000 Subject: [PATCH 7/7] Update stats.spec.js --- test/stats.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/stats.spec.js b/test/stats.spec.js index 23c88f627..24d54731a 100644 --- a/test/stats.spec.js +++ b/test/stats.spec.js @@ -48,7 +48,6 @@ describe('stats', function () { ipfs.stats.bw((err, res) => { expect(err).to.not.exist() expect(res).to.exist() - console.log(res) expect(res).to.have.a.property('TotalIn') expect(res).to.have.a.property('TotalOut') expect(res).to.have.a.property('RateIn')