Skip to content
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

remove PENDING_CLOSE flag #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const STATE = Symbol('state');
const OPEN = Symbol('open');
const CLOSED = Symbol('closed');
const HALF_OPEN = Symbol('half-open');
const PENDING_CLOSE = Symbol('pending-close');
const FALLBACK_FUNCTION = Symbol('fallback');
const NUM_FAILURES = Symbol('num-failures');
const STATUS = Symbol('status');
Expand Down Expand Up @@ -46,7 +45,6 @@ class CircuitBreaker extends EventEmitter {
this[STATUS] = new Status(this);
this[STATE] = CLOSED;
this[FALLBACK_FUNCTION] = null;
this[PENDING_CLOSE] = false;
this[NUM_FAILURES] = 0;

function _startTimer (circuit) {
Expand Down Expand Up @@ -139,12 +137,11 @@ class CircuitBreaker extends EventEmitter {
this.emit('fire');
const args = Array.prototype.slice.call(arguments);

if (this.opened || (this.halfOpen && this[PENDING_CLOSE])) {
if (this.opened) {
this.emit('reject');
return failFast(this, 'Breaker is open', args);
}

this[PENDING_CLOSE] = this.halfOpen;
return new this.Promise((resolve, reject) => {
const timeout = setTimeout(
() => {
Expand Down
38 changes: 38 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,44 @@ test('CircuitBreaker events', (t) => {
.catch(t.fail);
});

test('circuit halfOpen', (t) => {
const options = {
maxFailures: 1,
resetTimeout: 100
};

const breaker = circuitBreaker(passFail, options);
breaker.fire(-1)
.catch((e) => t.equals(e, 'Error: -1 is < 0', 'function should fail'))
.then(() => {
t.ok(breaker.opened, 'breaker should be open');
})
.then(() => {
setTimeout(() => {
t.ok(breaker.halfOpen, 'breaker should be halfOpen');
// breaker should be half open, fail it again should open the circuit again
breaker
.fire(-1)
.catch((e) => t.equals(e, 'Error: -1 is < 0', 'function should fail again'))
.then(() => {
t.ok(breaker.opened, 'breaker should be open again');
setTimeout(() => {
t.ok(breaker.halfOpen, 'breaker should be halfOpen again');
// breaker should be half open again and it should allow the original function to be called, and it should pass this time.
breaker
.fire(1)
.then((result) => {
t.equals(1, result);
t.ok(breaker.closed, 'breaker should be closed');
t.end();
})
.catch(t.fail);
}, options.resetTimeout * 1.1);
});
}, options.resetTimeout * 1.1);
});
});

/**
* Returns a promise that resolves if the parameter
* 'x' evaluates to >= 0. Otherwise the returned promise fails.
Expand Down