-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add option for user-provided EventEmitter to control buckets #809
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f9c6fc6
feat: add enableSnapshots description
60dafdb
feat: add EventEmitteR
dfdafce
feat: add eventemitter option
c7814c4
feat: fix event emitter import
342b977
feat: fix import
0157369
feat: remove listener on disable
9f41fe9
feat: fix nextBucket call in periodic rotations
a92d305
feat: resolve warnings
13860e9
feat: fix lint
6810a86
feat: unused variable
1fa1a27
feat: remove only
e2cb181
feat: add semicolon
627729d
feat: use constant
a3966be
feat: add space
f4e139d
feat: add new line
96b0d77
fix: exclude the rolling event emitter tests from the browser tests
lholmquist d3fbe16
Merge pull request #1 from lholmquist/gjethwani-event-emitter
gjethwani 1687143
feat: remove unused EventEmitter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lholmquist Was this a typo? If yes, I took the liberty of correcting it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤣 most likely. damn copy/paste