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

Update eslint-config-standard to the latest version 🚀 #118

Merged
merged 9 commits into from
Dec 14, 2017
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ node_js:
- "8"
- "9"
before_install:
- if [ $(npm --version | cut -d'.' -f1) != "5" ] ; then npm install -g npm@5 ; fi
- npm install -g greenkeeper-lockfile@1
before_script:
- export DISPLAY=:99.0
Expand Down
5 changes: 3 additions & 2 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class CircuitBreaker extends EventEmitter {
this.emit('cacheMiss');
}

if (this.opened && !this.pendingClose) {
if (!this.closed && !this.pendingClose) {
/**
* Emitted when the circuit breaker is open and failing fast
* @event CircuitBreaker#reject
Expand Down Expand Up @@ -432,7 +432,8 @@ function fail (circuit, err, args, latency) {
const stats = circuit.stats;
const errorRate = stats.failures / stats.fires * 100;
if (errorRate > circuit.options.errorThresholdPercentage ||
circuit.options.maxFailures >= stats.failures) {
circuit.options.maxFailures >= stats.failures ||
circuit.halfOpen) {
circuit.open();
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
"babel-plugin-transform-undefined-to-void": "6.8.3",
"browserify": "~14.5.0",
"escompress": "~0.5.0",
"eslint": "~4.12.0",
"eslint": "~4.13.1",
"eslint-config-semistandard": "~11.0.0",
"eslint-config-standard": "10.2.1",
"eslint-config-standard": "11.0.0-beta.0",
"eslint-plugin-import": "2.8.0",
"eslint-plugin-node": "5.2.1",
"eslint-plugin-promise": "~3.6.0",
Expand Down
12 changes: 12 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ function timedFunction (ms) {
});
}

function timedFailingFunction (ms) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(`Failed after ${ms}`);
}, ms);
if (typeof timer.unref === 'function') {
timer.unref();
}
});
}

function nonPromise () {
return 'foo';
}
Expand All @@ -49,6 +60,7 @@ module.exports = exports = {
passFail,
slowFunction,
timedFunction,
timedFailingFunction,
callbackFunction,
failedCallbackFunction,
nonPromise
Expand Down
39 changes: 39 additions & 0 deletions test/half-open-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const test = require('tape');
const opossum = require('../');
const { timedFailingFunction } = require('./common');

test('When half-open, the circuit only allows one request through', t => {
t.plan(10);
const options = {
errorThresholdPercentage: 1,
resetTimeout: 100
};

const breaker = opossum(timedFailingFunction, options);
breaker.fire(1)
.catch((e) => t.equals(e, 'Failed after 1'))
.then(() => {
t.ok(breaker.opened, 'breaker should be open after initial fire');
t.notOk(breaker.pendingClose, 'breaker should not be pending close after initial fire');
});

// Fire again after reset timeout. should be half open
setTimeout(() => {
t.ok(breaker.halfOpen, 'breaker should be halfOpen after timeout');
t.ok(breaker.pendingClose, 'breaker should be pending close after timeout');
breaker
.fire(500) // fail after a long time, letting possibly other fire()s to occur
.catch((e) => t.equals(e, 'Failed after 500', 'function should fail again'))
.then(() => {
t.ok(breaker.opened, 'breaker should be open again after long failing function');
t.notOk(breaker.halfOpen, 'breaker should not be halfOpen after long failing function');
t.notOk(breaker.pendingClose, 'breaker should not be pending close after long failing function');
});
// fire the breaker again, and be sure it fails as expected
breaker
.fire(1)
.catch((e) => t.equals(e.message, 'Breaker is open'));
}, options.resetTimeout * 1.5);
});