From 7ae4f245d5d1e3ac06a1af07bffa360a2a84639e Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sat, 17 Oct 2015 10:17:01 -0700 Subject: [PATCH] trying to fix #28 by checking if callbackFiller has key in domain.error --- lib/multi_caching.js | 6 +++++- test/multi_caching.unit.js | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 7a0b8ba..5b4a90b 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -163,7 +163,11 @@ var multiCaching = function(caches, options) { domain .create() .on('error', function(err) { - callbackFiller.fill(key, err); + if (callbackFiller.has(key)) { + callbackFiller.fill(key, err); + } else { + cb(err); + } }) .bind(work)(function(err, data) { if (err) { diff --git a/test/multi_caching.unit.js b/test/multi_caching.unit.js index f33f70d..4b81b0c 100644 --- a/test/multi_caching.unit.js +++ b/test/multi_caching.unit.js @@ -388,6 +388,7 @@ describe("multiCaching", function() { checkErr(err); assert.deepEqual(widget, {name: name}); sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, {ttl: defaultTtl}); + done(); }); }); @@ -866,6 +867,31 @@ describe("multiCaching", function() { }); }); + context("when an error is thrown in the work function's callback", function() { + var fakeError; + + beforeEach(function() { + fakeError = new Error(support.random.string()); + }); + + it("bubbles up that error", function(done) { + multiCache.wrap(key, function(next) { + next(); + }, ttl, function() { + // This test as-is doesn't prove a fix for #28 (https://github.com/BryanDonovan/node-cache-manager/issues/28) + // but if you remove the try/catch, it shows that the undefined `waiting` array issue + // is no longer present (the domain doesn't try to process the error in the callbackFiller). + try { + throw new Error('foo'); + } catch (e) { + assert.equal(e.message, 'foo'); + } + + done(); + }); + }); + }); + context("when store.get() calls back with an error", function() { it("bubbles up that error", function(done) { var fakeError = new Error(support.random.string());