Skip to content

Commit

Permalink
test: add test for circuit.halfOpen
Browse files Browse the repository at this point in the history
Provided by @wei-lee in #7
  • Loading branch information
lance committed Dec 20, 2016
1 parent 35b5be1 commit 5ccef7f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,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

0 comments on commit 5ccef7f

Please sign in to comment.