Skip to content

Commit

Permalink
fix: fix bug where options couldn't be set to zero.
Browse files Browse the repository at this point in the history
when setting things like resetTimeout and errorThresholdPercentage to 0, the default value was being used.

fixes #824
  • Loading branch information
lholmquist committed Oct 4, 2023
1 parent c87bc03 commit 244a89c
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ class CircuitBreaker extends EventEmitter {
constructor (action, options = {}) {
super();
this.options = options;
this.options.timeout =
options.timeout === false ? false : options.timeout || 10000;
this.options.resetTimeout = options.resetTimeout || 30000;
this.options.timeout = options.timeout ?? 10000;
this.options.resetTimeout = options.resetTimeout ?? 30000;
this.options.errorThresholdPercentage =
options.errorThresholdPercentage || 50;
this.options.rollingCountTimeout = options.rollingCountTimeout || 10000;
this.options.rollingCountBuckets = options.rollingCountBuckets || 10;
options.errorThresholdPercentage ?? 50;
this.options.rollingCountTimeout = options.rollingCountTimeout ?? 10000;
this.options.rollingCountBuckets = options.rollingCountBuckets ?? 10;
this.options.rollingPercentilesEnabled =
options.rollingPercentilesEnabled !== false;
this.options.capacity = Number.isInteger(options.capacity)
Expand Down
90 changes: 90 additions & 0 deletions test/options-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const test = require('tape');
const CircuitBreaker = require('../');

const common = require('./common');
const passFail = common.passFail;

test('timeout defaults to 10000', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
t.equals(breaker.options.timeout, 10000, 'should be set to 10000 by default');
breaker.shutdown();
t.end();
});

test('timeout can be set to 0', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail,
{ timeout: 0 });
t.equals(breaker.options.timeout, 0, 'should be set to 0');
breaker.shutdown();
t.end();
});

test('resetTimeout defaults to 30000', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
t.equals(breaker.options.resetTimeout, 30000, 'should be set to 30000 by default');
breaker.shutdown();
t.end();
});

test('resetTimeout can be set to 0', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail,
{ resetTimeout: 0 });
t.equals(breaker.options.resetTimeout, 0, 'should be set to 0');
breaker.shutdown();
t.end();
});

test('errorThresholdPercentage defaults to 50', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
t.equals(breaker.options.errorThresholdPercentage, 50, 'should be set to 50 by default');
breaker.shutdown();
t.end();
});

test('errorThresholdPercentage can be set to 0', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail,
{ errorThresholdPercentage: 0 });
t.equals(breaker.options.errorThresholdPercentage, 0, 'should be set to 0');
breaker.shutdown();
t.end();
});

test('rollingCountTimeout defaults to 10000', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
t.equals(breaker.options.rollingCountTimeout, 10000, 'should be set to 10000 by default');
breaker.shutdown();
t.end();
});

test('rollingCountTimeout can be set to 0', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail,
{ rollingCountTimeout: 0 });
t.equals(breaker.options.rollingCountTimeout, 0, 'should be set to 0');
breaker.shutdown();
t.end();
});

test('rollingCountBuckets defaults to 10', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
t.equals(breaker.options.rollingCountBuckets, 10, 'should be set to 10 by default');
breaker.shutdown();
t.end();
});

test('rollingCountBuckets can be set to 0', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail,
{ rollingCountBuckets: 0 });
t.equals(breaker.options.rollingCountBuckets, 0, 'should be set to 0');
breaker.shutdown();
t.end();
});
68 changes: 68 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,74 @@ test('uses name as a group when no group is provided', t => {
t.end();
});

test('errorThresholdPercentage defaults to 50', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
t.equals(breaker.options.errorThresholdPercentage, 50, 'should be set to 50 by default');
breaker.shutdown();
t.end();
});

test('errorThresholdPercentage can be set to 0', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail,
{ errorThresholdPercentage: 0 });
t.equals(breaker.options.errorThresholdPercentage, 0, 'should be set to 0');
breaker.shutdown();
t.end();
});

test('resetTimeout defaults to 30000', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
t.equals(breaker.options.resetTimeout, 30000, 'should be set to 30000 by default');
breaker.shutdown();
t.end();
});

test('resetTimeout can be set to 0', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail,
{ resetTimeout: 0 });
t.equals(breaker.options.resetTimeout, 0, 'should be set to 0');
breaker.shutdown();
t.end();
});

test('rollingCountTimeout defaults to 10000', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
t.equals(breaker.options.rollingCountTimeout, 10000, 'should be set to 10000 by default');
breaker.shutdown();
t.end();
});

test('rollingCountTimeout can be set to 0', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail,
{ rollingCountTimeout: 0 });
t.equals(breaker.options.rollingCountTimeout, 0, 'should be set to 0');
breaker.shutdown();
t.end();
});

test('rollingCountBuckets defaults to 10', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
t.equals(breaker.options.rollingCountBuckets, 10, 'should be set to 10 by default');
breaker.shutdown();
t.end();
});

test('rollingCountBuckets can be set to 0', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail,
{ rollingCountBuckets: 0 });
t.equals(breaker.options.rollingCountBuckets, 0, 'should be set to 0');
breaker.shutdown();
t.end();
});

test('Passes parameters to the circuit function', t => {
t.plan(1);
const expected = 34;
Expand Down

0 comments on commit 244a89c

Please sign in to comment.