Skip to content

Commit

Permalink
Merge pull request #125 from WLBF/master
Browse files Browse the repository at this point in the history
fix default option issue
  • Loading branch information
mcollina authored Mar 4, 2018
2 parents 8ac083a + 085731a commit 3cb9aa1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
20 changes: 20 additions & 0 deletions lib/defaultOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const defaultOptions = {
headers: {},
body: Buffer.alloc(0),
method: 'GET',
duration: 10,
connections: 10,
pipelining: 1,
timeout: 10,
maxConnectionRequests: 0,
maxOverallRequests: 0,
connectionRate: 0,
overallRate: 0,
amount: 0,
reconnectRate: 0,
forever: false,
idReplacement: false,
requests: [{}]
}

module.exports = defaultOptions
23 changes: 3 additions & 20 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,16 @@
const EE = require('events').EventEmitter
const URL = require('url')
const hdr = require('hdr-histogram-js')
const clone = require('clone')
const timestring = require('timestring')
const Client = require('./httpClient')
const DefaultOptions = require('./defaultOptions')
const xtend = require('xtend')
const histUtil = require('hdr-histogram-percentiles-obj')
const reInterval = require('reinterval')
const histAsObj = histUtil.histAsObj
const addPercentiles = histUtil.addPercentiles

const defaultOptions = {
headers: {},
body: Buffer.alloc(0),
method: 'GET',
duration: 10,
connections: 10,
pipelining: 1,
timeout: 10,
maxConnectionRequests: 0,
maxOverallRequests: 0,
connectionRate: 0,
overallRate: 0,
amount: 0,
reconnectRate: 0,
forever: false,
idReplacement: false,
requests: [{}]
}

function run (opts, cb) {
const cbPassedIn = (typeof cb === 'function')

Expand Down Expand Up @@ -69,7 +52,7 @@ function run (opts, cb) {
0 // 5xx
]

opts = xtend(defaultOptions, opts)
opts = xtend(clone(DefaultOptions), opts)

// do error checking, if error, return
if (checkOptsForErrors()) return
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"chalk": "^2.0.0",
"clone": "^2.1.1",
"color-support": "^1.1.1",
"deep-extend": "^0.5.0",
"hdr-histogram-js": "^1.0.0",
Expand Down
15 changes: 15 additions & 0 deletions test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const os = require('os')
const path = require('path')
const test = require('tap').test
const clone = require('clone')
const run = require('../lib/run')
const defaultOptions = require('../lib/defaultOptions')
const helper = require('./helper')
const server = helper.startServer()

Expand Down Expand Up @@ -279,3 +281,16 @@ for (let i = 1; i <= 5; i++) {
})
})
}

test('run should not modify default options', (t) => {
const origin = clone(defaultOptions)
run({
url: 'http://localhost:' + server.address().port,
connections: 2,
duration: 2
}, function (err, result) {
t.error(err)
t.deepEqual(defaultOptions, origin, 'calling run function does not modify default options')
t.end()
})
})

0 comments on commit 3cb9aa1

Please sign in to comment.