Skip to content

Commit

Permalink
feat: add semicolon
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam Jethwani committed Sep 12, 2023
1 parent 1fa1a27 commit e2cb181
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class Status extends EventEmitter {
// interval is not set if rotateBucketController is provided
if (this.rotateBucketController === undefined) {
clearInterval(this[BUCKET_INTERVAL]);
} else {
this.removeRotateBucketControllerListener();
}
if (this.enableSnapshots) {
clearInterval(this[SNAPSHOT_INTERVAL]);
Expand All @@ -200,7 +202,7 @@ class Status extends EventEmitter {
}

startListeneningForRotateEvent () {
if (this.rotateBucketController) {
if (this.rotateBucketController && this.rotateBucketController.listenerCount('rotate', this.rotateBucket) === 0) {
this.rotateBucketController.on(this[ROTATE_EVENT_NAME],
this.rotateBucket);
}
Expand Down
35 changes: 34 additions & 1 deletion test/enable-disable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ test('Event listener should be removed only for the breaker that is disabled', t
const breakerToBeDisabled = new CircuitBreaker(passFail, {
rotateBucketController: emitter
});
const _breakerNotToBeDisabled = new CircuitBreaker(passFail, {
const breakerNotToBeDisabled = new CircuitBreaker(passFail, {
rotateBucketController: emitter
});
t.equals(emitter.listeners('rotate').length, 2, '1 listener attached for each breaker');
breakerToBeDisabled.disable();
t.equals(emitter.listeners('rotate').length, 1, '1 listener should be disabled and 1 should remain');
breakerToBeDisabled.shutdown();
breakerNotToBeDisabled.shutdown();
t.end();
});

Expand All @@ -98,5 +100,36 @@ test('Event listener should be re-added when circuit is re-enabled', t => {
t.equals(emitter.listeners('rotate').length, 0, 'listener removed when breaker disabled');
breaker.enable();
t.equals(emitter.listeners('rotate').length, 1, 'listener re-attached when breaker re-enabled');
breaker.shutdown();
t.end();
});

test('Listener should not be attached with a call to enable if there is already a listener', t => {
t.plan(2);
const emitter = new EventEmitter();
const breaker = new CircuitBreaker(passFail, {
rotateBucketController: emitter
});
t.equals(emitter.listeners('rotate').length, 1, 'listener attached automatically');
breaker.enable();
t.equals(emitter.listeners('rotate').length, 1, 'listener should not be added again');
breaker.shutdown();
t.end();
});

test('Listener should not be attached with a call to enable if there is already a listener and there is another breaker in the mix', t => {
t.plan(2);
const emitter = new EventEmitter();
const breaker = new CircuitBreaker(passFail, {
rotateBucketController: emitter
});
const anotherBreaker = new CircuitBreaker(passFail, {
rotateBucketController: emitter
});
t.equals(emitter.listeners('rotate').length, 2, 'listener attached automatically');
breaker.enable();
t.equals(emitter.listeners('rotate').length, 2, 'listener should not be added again');
breaker.shutdown();
anotherBreaker.shutdown();
t.end();
});

0 comments on commit e2cb181

Please sign in to comment.