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

test: add human option test bitswap stat #559

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 7 additions & 1 deletion src/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const waterfall = require('async/waterfall')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { expectIsBitswap } = require('../stats/utils')
const { expectIsBitswap, expectIsBitswapHumanReadable } = require('../stats/utils')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
Expand Down Expand Up @@ -43,6 +43,12 @@ module.exports = (createCommon, options) => {
})
})

it('should get human readable bitswap stats', async () => {
const stats = await ipfs.bitswap.stat({ human: true })

expectIsBitswapHumanReadable(null, stats)
})

it('should not get bitswap stats when offline', function (done) {
this.timeout(60 * 1000)

Expand Down
8 changes: 7 additions & 1 deletion src/stats/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict'

const { getDescribe, getIt, expect } = require('../utils/mocha')
const { expectIsBitswap } = require('./utils')
const { expectIsBitswap, expectIsBitswapHumanReadable } = require('./utils')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
Expand Down Expand Up @@ -41,5 +41,11 @@ module.exports = (createCommon, options) => {
expectIsBitswap(null, res)
})
})

it('should get human readable bitswap stats', async () => {
const stats = await ipfs.stats.bitswap({ human: true })

expectIsBitswapHumanReadable(null, stats)
})
})
}
30 changes: 29 additions & 1 deletion src/stats/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ exports.expectIsBitswap = (err, stats) => {
expect(stats).to.have.a.property('dupBlksReceived')
expect(stats).to.have.a.property('dupDataReceived')

expect(stats.provideBufLen).to.a('number')
expect(stats.wantlist).to.be.an('array')
expect(stats.peers).to.be.an('array')
expect(isBigInt(stats.provideBufLen)).to.eql(true)
expect(isBigInt(stats.blocksReceived)).to.eql(true)
expect(isBigInt(stats.dataReceived)).to.eql(true)
expect(isBigInt(stats.blocksSent)).to.eql(true)
Expand All @@ -30,6 +30,34 @@ exports.expectIsBitswap = (err, stats) => {
expect(isBigInt(stats.dupDataReceived)).to.eql(true)
}

exports.expectIsBitswapHumanReadable = (err, stats) => {
expect(err).to.not.exist()
expect(stats).to.exist()
expect(stats).to.have.a.property('provideBufLen')
.and.to.be.a('number')
expect(stats).to.have.a.property('blocksReceived')
.and.to.be.a('number')
expect(stats).to.have.a.property('wantlist')
.and.to.be.a('string')
.and.to.match(/\[\d+\skeys\]$/gm)
expect(stats).to.have.a.property('peers')
.and.to.be.a('string')
.and.to.match(/\[\d+\]$/gm)
expect(stats).to.have.a.property('dupBlksReceived')
.and.to.be.a('number')
expect(stats).to.have.a.property('dupDataReceived')
.and.to.be.a('string')
.and.to.match(/[\d.]+\s[PTGMK]?B$/gm)
expect(stats).to.have.a.property('dataReceived')
.and.to.be.a('string')
.and.to.match(/[\d.]+\s[PTGMK]?B$/gm)
expect(stats).to.have.a.property('blocksSent')
.and.to.be.a('number')
expect(stats).to.have.a.property('dataSent')
.and.to.be.a('string')
.and.to.match(/[\d.]+\s[PTGMK]?B$/gm)
}

exports.expectIsBandwidth = (err, stats) => {
expect(err).to.not.exist()
expect(stats).to.exist()
Expand Down