Skip to content

Commit

Permalink
fix: disabled breaker emits fire events (#895)
Browse files Browse the repository at this point in the history
* fix: disabled breaker emits fire events

To revert to previous behaviour up to v8.1.4.

#894

* review: add regression test
  • Loading branch information
ullumullu authored Oct 28, 2024
1 parent 3350332 commit d0ee9e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,13 @@ class CircuitBreaker extends EventEmitter {

const args = rest.slice();

/**
* Emitted when the circuit breaker action is executed
* @event CircuitBreaker#fire
* @type {any} the arguments passed to the fired function
*/
this.emit('fire', args);

// Protection, caches and coalesce disabled.
if (!this[ENABLED]) {
const result = this.action.apply(context, args);
Expand All @@ -630,13 +637,6 @@ class CircuitBreaker extends EventEmitter {
// Need to create variable here to prevent extra calls if cache is disabled
let cacheKey = '';

/**
* Emitted when the circuit breaker action is executed
* @event CircuitBreaker#fire
* @type {any} the arguments passed to the fired function
*/
this.emit('fire', args);

// If cache is enabled, check if we have a cached value
if (this.options.cache) {
cacheKey = this.options.cacheGetKey.apply(this, rest);
Expand Down
13 changes: 13 additions & 0 deletions test/enable-disable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ test('When disabled the circuit should always be closed', t => {
.then(t.end);
});
});

test('When disabled fire event is still emitted', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
breaker.on('fire', () => {
t.pass('fire event');
t.end();
});

breaker.disable();
breaker.fire(1)
.finally(_ => breaker.shutdown());
});

0 comments on commit d0ee9e6

Please sign in to comment.