Skip to content

Commit

Permalink
feat: add the original function parameters to the list of arguments e…
Browse files Browse the repository at this point in the history
…mitted in a failure event.

fixes nodeshift#324
  • Loading branch information
lholmquist committed Jun 10, 2019
1 parent 9338e57 commit 426d33b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ function fail (circuit, err, args, latency) {
* @event CircuitBreaker#failure
* @type {Error}
*/
circuit.emit('failure', err, latency);
circuit.emit('failure', err, latency, args);
if (circuit.warmUp) return;

// check stats to see if the circuit should be opened
Expand Down
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,23 @@ test('Circuit Breaker failure event emits latency', t => {
breaker.fire(-1).catch(noop);
});

test('Circuit Breaker failure event emits function parameters', t => {
t.plan(6);
const breaker = circuit(passFail);
breaker.on('failure', (result, latencyTime, args) => {
t.ok(args, 'third argument is the latency');
t.equal(Array.isArray(args), true, 'The args parameter is an array');
t.equal(args[0], -1, 'this is the first argument');
t.equal(args[1].arg1, 'arg1', 'this is the second argument object');
t.equal(args[1].arg2, 'arg2', 'this is the second argument object');
t.equal(args[2][0], '1', 'this is the third argument');
breaker.shutdown();
t.end();
});

breaker.fire(-1, { arg1: 'arg1', arg2: 'arg2' }, ['1', '2']).catch(noop);
});

test('Circuit Breaker timeout event emits latency', t => {
t.plan(1);
const breaker = circuit(slowFunction, { timeout: 10 });
Expand Down

0 comments on commit 426d33b

Please sign in to comment.