Skip to content

Commit

Permalink
feat: add a group option. GH-Issue #43
Browse files Browse the repository at this point in the history
  • Loading branch information
lholmquist committed Apr 3, 2017
1 parent 9ba4dfc commit 3052f23
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const FALLBACK_FUNCTION = Symbol('fallback');
const NUM_FAILURES = Symbol('num-failures');
const STATUS = Symbol('status');
const NAME = Symbol('name');
const GROUP = Symbol('group');
const CACHE = new WeakMap();

/**
Expand Down Expand Up @@ -52,6 +53,7 @@ class CircuitBreaker extends EventEmitter {
this[PENDING_CLOSE] = false;
this[NUM_FAILURES] = 0;
this[NAME] = options.name || action.name || nextName();
this[GROUP] = options.group || this[NAME];

if (typeof action !== 'function') {
this.action = _ => this.Promise.resolve(action);
Expand Down Expand Up @@ -138,6 +140,10 @@ class CircuitBreaker extends EventEmitter {
return this[NAME];
}

get group () {
return this[GROUP];
}

/**
* True if the circuit is currently closed. False otherwise.
*/
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ test('uses UUID as a name when none is provided and the function is anonymoys',
t.end();
});

test('accepts a group as an option', (t) => {
const breaker = cb(passFail, {group: 'tacoMachine'});
t.equals(breaker.group, 'tacoMachine');
t.end();
});

test('uses name as a group when no group is provided', (t) => {
const breaker = cb(passFail, {name: 'tacoMachine'});
t.equals(breaker.group, 'tacoMachine');
t.end();
});

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

0 comments on commit 3052f23

Please sign in to comment.