Skip to content

Commit

Permalink
feat: Add original function parameters to the failure and timeout eve…
Browse files Browse the repository at this point in the history
…nts (#326)

* feat: add the original function parameters to the list of arguments emitted in a timeout event.
* feat: add the original function parameters to the list of arguments emitted in a failure event.
* chore: remove whitespace, editor plugin

Fixes: #324
  • Loading branch information
lholmquist authored and lance committed Jun 11, 2019
1 parent e342338 commit f8918c4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CircuitBreaker extends EventEmitter {
this.options.capacity = Number.isInteger(options.capacity)
? options.capacity : Number.MAX_SAFE_INTEGER;
this.options.errorFilter = options.errorFilter || (_ => false);

this.semaphore = new Semaphore(this.options.capacity);

this[VOLUME_THRESHOLD] = Number.isInteger(options.volumeThreshold)
Expand Down Expand Up @@ -313,7 +313,7 @@ class CircuitBreaker extends EventEmitter {
get stats () {
return this[STATUS].stats;
}

/**
* Get the hystrixStats.
* @type {HystrixStats}
Expand Down Expand Up @@ -471,7 +471,7 @@ class CircuitBreaker extends EventEmitter {
*/
const latency = Date.now() - latencyStartTime;
this.semaphore.release();
this.emit('timeout', error, latency);
this.emit('timeout', error, latency, args);
resolve(handleError(
error, this, timeout, args, latency, resolve, reject));
}, this.options.timeout);
Expand Down 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
35 changes: 35 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 function args');
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 All @@ -808,6 +825,24 @@ test('Circuit Breaker timeout event emits latency', t => {
breaker.fire(-1).catch(noop);
});

test('Circuit Breaker timeout event emits function parameters', t => {
t.plan(6);
const breaker = circuit(slowFunction, { timeout: 10 });

breaker.on('timeout', (result, latencyTime, args) => {
t.ok(args, 'third argument is the function args');
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 with semaphore released', t => {
t.plan(1);
const breaker = circuit(slowFunction, { timeout: 10, capacity: 2 });
Expand Down

0 comments on commit f8918c4

Please sign in to comment.