Browse Source

More coverage + refactor promise usage on multi cache getters

feature/remove-domains
Jonathan 9 years ago
parent
commit
9c2ae65bc4
  1. 36
      lib/multi_caching.js
  2. 28
      test/multi_caching.unit.js

36
lib/multi_caching.js

@ -53,6 +53,8 @@ var multiCaching = function(caches, options) {
options = {};
}
var defer = Promise.defer();
var i = 0;
async.eachSeries(caches, function(cache, next) {
var callback = function(err, result) {
@ -66,9 +68,8 @@ var multiCaching = function(caches, options) {
// break out of async loop.
if (typeof cb === 'function') {
return cb(err, result, i);
} else {
return cb.resolve(result);
}
return defer.resolve(result);
}
i += 1;
@ -77,16 +78,17 @@ var multiCaching = function(caches, options) {
cache.store.get(key, options, callback);
}, function(err, result) {
if (typeof cb === 'object') {
if (err) {
cb.reject(err);
} else {
cb.resolve(result);
}
} else if (typeof cb === 'function') {
if (typeof cb === 'function') {
cb(err, result);
}
if (err) {
return defer.reject(err);
}
defer.resolve(result);
});
return defer.promise;
}
function setInMultipleCaches(caches, opts, cb) {
@ -120,19 +122,15 @@ var multiCaching = function(caches, options) {
* @param {function} cb
*/
self.getAndPassUp = function(key, cb) {
getFromHighestPriorityCache(key, function(err, result, index) {
return getFromHighestPriorityCache(key, function(err, result, index) {
if (err) {
if (typeof cb === 'function') {
if (cb) {
return cb(err);
} else {
return cb.reject(err);
}
}
if (typeof cb === 'function') {
if (cb) {
cb(err, result);
} else {
cb.resolve(result);
}
if (index) {
@ -282,11 +280,7 @@ var multiCaching = function(caches, options) {
options = {};
}
var defer = Promise.defer();
getFromHighestPriorityCache(key, options, cb || defer);
return defer.promise;
return getFromHighestPriorityCache(key, options, cb);
};
/**

28
test/multi_caching.unit.js

@ -183,7 +183,7 @@ describe("multiCaching", function() {
});
describe('using promises', function() {
it('should return a promise and resolve it', function(done) {
it('gets data from first cache that has it', function(done) {
memoryCache3.set(key, value)
.then(function() {
return multiCache.get(key);
@ -193,6 +193,23 @@ describe("multiCaching", function() {
})
.then(done);
});
it("passes any options to underlying caches", function(done) {
var opts = {foo: 'bar'};
multiCache.set(key, value)
.then(function() {
sinon.spy(memoryCache.store, 'get');
return multiCache.get(key, opts);
})
.then(function(result) {
assert.equal(result, value);
assert.ok(memoryCache.store.get.calledWith(key, opts));
memoryCache.store.get.restore();
})
.then(done);
});
});
});
@ -360,6 +377,15 @@ describe("multiCaching", function() {
done();
});
});
it("bubbles up errors from caches and reject promise", function(done) {
multiCache.getAndPassUp(key)
.catch(function(err) {
assert.ok(memoryStoreStub.get.called);
assert.equal(err, fakeError);
done();
});
});
});
});
});

Loading…
Cancel
Save