From 9338e5796048ad54e362e7259af468bbc2a5830b Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Mon, 10 Jun 2019 11:29:59 -0400 Subject: [PATCH] feat: add the original function parameters to the list of arguments emitted in a timeout event. fixes #324 --- lib/circuit.js | 2 +- test/test.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/circuit.js b/lib/circuit.js index 2b371791..ad39e306 100644 --- a/lib/circuit.js +++ b/lib/circuit.js @@ -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); diff --git a/test/test.js b/test/test.js index b5912c01..42305c9a 100644 --- a/test/test.js +++ b/test/test.js @@ -808,6 +808,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 });