-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option for user-provided EventEmitter to control buckets (#809
) The motivation for this PR. I added an option for a user-provided EventEmitter that would handle the rotating of the buckets. The idea here is that you could do something like this in your app ``` const bucketController = new EventEmitter(); setInterval(() => { bucketController.emit('rotate') }, 1000) ``` And then pass `bucketController` into opossum. Opossum would listen for that event and then rotate the buckets. --------- Co-authored-by: Gautam Jethwani <[email protected]>
- Loading branch information
Showing
6 changed files
with
191 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict'; | ||
|
||
const test = require('tape'); | ||
const CircuitBreaker = require('../'); | ||
const { passFail } = require('./common'); | ||
const EventEmitter = require('events').EventEmitter; | ||
|
||
test('When disabled, the event emitter listener should be removed', 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.disable(); | ||
t.equals(emitter.listeners('rotate').length, 0, 'listener removed when breaker disabled'); | ||
breaker.shutdown(); | ||
t.end(); | ||
}); | ||
|
||
test('Event listener should be removed only for the breaker that is disabled', t => { | ||
t.plan(2); | ||
const emitter = new EventEmitter(); | ||
const breakerToBeDisabled = new CircuitBreaker(passFail, { | ||
rotateBucketController: emitter | ||
}); | ||
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(); | ||
}); | ||
|
||
test('Event listener should be re-added when circuit is re-enabled', t => { | ||
t.plan(3); | ||
const emitter = new EventEmitter(); | ||
const breaker = new CircuitBreaker(passFail, { | ||
rotateBucketController: emitter | ||
}); | ||
t.equals(emitter.listeners('rotate').length, 1, 'listener attached automatically'); | ||
breaker.disable(); | ||
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters