-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not close if preexisting task resolves when state is not OPEN (…
…#382) The breaker would close if an earlier task resolved after a later one closed it. This fixes that and adds a test case.
- Loading branch information
1 parent
7b97914
commit 7b92602
Showing
2 changed files
with
59 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
const test = require('tape'); | ||
const CircuitBreaker = require('../'); | ||
|
||
test( | ||
'When closed, an existing request resolving does not reopen circuit', | ||
t => { | ||
t.plan(6); | ||
const options = { | ||
errorThresholdPercentage: 1, | ||
resetTimeout: 60000 | ||
}; | ||
|
||
const breaker = new CircuitBreaker(function (shouldPass, time) { | ||
return new Promise((resolve, reject) => { | ||
const timer = setTimeout( | ||
shouldPass | ||
? () => resolve('success') | ||
: () => reject(new Error('test-error')), | ||
time || 0 | ||
); | ||
if (typeof timer.unref === 'function') { | ||
timer.unref(); | ||
} | ||
}); | ||
}, options); | ||
|
||
const promises = []; | ||
|
||
promises.push(breaker.fire(true, 100) | ||
.then(res => t.equals(res, 'success'))); | ||
|
||
promises.push(breaker.fire(false) | ||
.then(t.fail) | ||
.catch(e => { | ||
t.equals(e.message, 'test-error'); | ||
}) | ||
.then(() => { | ||
t.ok(breaker.opened, 'should be open after initial fire'); | ||
t.notOk(breaker.pendingClose, | ||
'should not be pending close after initial fire'); | ||
})); | ||
|
||
Promise.all(promises) | ||
.then(() => { | ||
t.ok(breaker.opened, | ||
'should still be open even after first fire resolved'); | ||
t.notOk(breaker.pendingClose, | ||
'should still not be pending close'); | ||
}) | ||
.then(() => t.end()); | ||
} | ||
); |