Skip to content

Commit

Permalink
feat: Handling for node-like callback functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lance committed Nov 1, 2016
1 parent 2672c34 commit 77f3d6b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ const defaults = {
function circuitBreaker (action, options) {
return new CircuitBreaker(action, Object.assign({}, defaults, options));
}

circuitBreaker.promisify = require('./lib/promisify');

module.exports = exports = circuitBreaker;
15 changes: 15 additions & 0 deletions lib/promisify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Fidelity = require('fidelity');

module.exports = exports = function promisify (func) {
return function promisifiedFunction () {
return new Fidelity((resolve, reject) => {
const cb = (err, result) => {
if (err) reject(err);
resolve(result);
};
const args = Array.prototype.slice.call(arguments);
args.push(cb);
func.apply(func, args);
});
};
};
54 changes: 35 additions & 19 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const circuitBreaker = require('../');

test('api', (t) => {
const breaker = circuitBreaker(passFail);
t.ok(circuitBreaker.promisify);
t.ok(breaker);
t.ok(breaker.fire);
t.notOk(breaker.open);
Expand All @@ -25,6 +26,26 @@ test('Passes arguments to the circuit function', (t) => {
.catch(t.fail);
});

test('Fails when the circuit function fails', (t) => {
const expected = -1;
const breaker = circuitBreaker(passFail);

breaker.fire(expected)
.then(t.fail)
.catch((e) => t.equals(e, expected))
.then(t.end);
});

test('Fails when the circuit function times out', (t) => {
const expected = 'Action timed out after 10ms';
const breaker = circuitBreaker(slowFunction, { timeout: 10 });

breaker.fire()
.then(t.fail)
.catch((e) => t.equals(e, expected))
.then(t.end);
});

test('Works with functions that do not return a promise', (t) => {
const breaker = circuitBreaker(nonPromise);

Expand All @@ -43,32 +64,23 @@ test('Works with non-functions', (t) => {
.catch(t.fail);
});

test.skip('Works with callback functions', (t) => {
const breaker = circuitBreaker(callbackFunction);
test('Works with callback functions', (t) => {
const promisified = circuitBreaker.promisify(callbackFunction);
const breaker = circuitBreaker(promisified);

breaker.fire(3, 4)
.then((arg) => t.equals(arg, 7))
.then(t.end)
.catch(t.fail);
});

test('Fails when the circuit function fails', (t) => {
const expected = -1;
const breaker = circuitBreaker(passFail);

breaker.fire(expected)
.then(t.fail)
.catch((e) => t.equals(e, expected))
.then(t.end);
});
test('Works with callback functions that fail', (t) => {
const promisified = circuitBreaker.promisify(failedCallbackFunction);
const breaker = circuitBreaker(promisified);

test('Fails when the circuit function times out', (t) => {
const expected = 'Action timed out after 10ms';
const breaker = circuitBreaker(slowFunction, { timeout: 10 });

breaker.fire()
breaker.fire(3, 4)
.then(t.fail)
.catch((e) => t.equals(e, expected))
.catch((e) => t.equals(e, 'Whoops!'))
.then(t.end);
});

Expand Down Expand Up @@ -122,7 +134,7 @@ test('Executes fallback action, if one exists, when breaker is open', (t) => {
});
});

test('Passes arguments to fallback function', (t) => {
test('Passes arguments to the fallback function', (t) => {
const fails = -1;
const expected = 100;
const breaker = circuitBreaker(passFail, { maxFailures: 1 });
Expand Down Expand Up @@ -168,5 +180,9 @@ function nonPromise () {
}

function callbackFunction (x, y, callback) {
callback(x + y);
callback(null, x + y);
}

function failedCallbackFunction () {
Array.prototype.slice.call(arguments).pop()('Whoops!');
}

0 comments on commit 77f3d6b

Please sign in to comment.