From 6efdd8617e2ae957baaaf719977920f211c5f59c Mon Sep 17 00:00:00 2001 From: lucaschen <2109121+lucaschen@users.noreply.github.com> Date: Sun, 30 Aug 2020 18:17:56 +1000 Subject: [PATCH] feat: made isFailure work for timeout errors --- lib/breaker.js | 4 +++- package-lock.json | 2 +- test/breaker.js | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) 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});