diff --git a/lib/breaker.js b/lib/breaker.js index 341b8ba..49888f2 100644 --- a/lib/breaker.js +++ b/lib/breaker.js @@ -98,7 +98,9 @@ Breaker.prototype._run = function _run(/*args...n, callback*/) { timer = undefined; self._pendingClose = false; self.emit('timeout', error); - self._onFailure(); + + if (self.settings.isFailure(error)) self._onFailure(); + callback(error); }, this.settings.timeout); diff --git a/package-lock.json b/package-lock.json index 9cde6fa..47573b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "levee", - "version": "1.4.0", + "version": "1.5.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/test/breaker.js b/test/breaker.js index 771d302..373f2f5 100644 --- a/test/breaker.js +++ b/test/breaker.js @@ -202,6 +202,28 @@ test('timeout', function (t) { }); }); +test('timeout with custom failure check', function (t) { + var breaker; + + breaker = new Breaker(timeout, { + isFailure: function () { + return false; + }, + timeout: 10, + maxFailures: 1 + }); + + t.ok(breaker.isClosed()); + + breaker.run('ok', function (err, data) { + t.ok(err); + t.equal(err.message, 'Command timeout.'); + t.notOk(data); + t.ok(breaker.isClosed()); + t.end(); + }); +}); + test('timeout returned value', function(t){ var breaker; breaker = new Breaker(timeoutCallback, {timeout: 10, maxFailures: 1});