|
|
@ -19,7 +19,7 @@ var methods = { |
|
|
|
describe("caching", function() { |
|
|
|
var cache; |
|
|
|
var key; |
|
|
|
var ttl = 1; |
|
|
|
var defaultTtl = 1; |
|
|
|
var name; |
|
|
|
var value; |
|
|
|
|
|
|
@ -33,7 +33,7 @@ describe("caching", function() { |
|
|
|
}); |
|
|
|
|
|
|
|
it("lets us set and get data in cache", function(done) { |
|
|
|
cache.set(key, value, ttl, function(err) { |
|
|
|
cache.set(key, value, {ttl: defaultTtl}, function(err) { |
|
|
|
checkErr(err); |
|
|
|
|
|
|
|
cache.get(key, function(err, result) { |
|
|
@ -45,7 +45,7 @@ describe("caching", function() { |
|
|
|
}); |
|
|
|
|
|
|
|
it("lets us set and get data without a callback", function(done) { |
|
|
|
cache.set(key, value, ttl); |
|
|
|
cache.set(key, value, {ttl: defaultTtl}); |
|
|
|
|
|
|
|
setTimeout(function() { |
|
|
|
var result = cache.get(key); |
|
|
@ -54,7 +54,7 @@ describe("caching", function() { |
|
|
|
}, 20); |
|
|
|
}); |
|
|
|
|
|
|
|
it("lets us set and get data without a ttl or callback", function(done) { |
|
|
|
it("lets us set and get data without options object or callback", function(done) { |
|
|
|
cache.set(key, value); |
|
|
|
|
|
|
|
setTimeout(function() { |
|
|
@ -74,7 +74,7 @@ describe("caching", function() { |
|
|
|
cache = caching({store: store}); |
|
|
|
key = support.random.string(20); |
|
|
|
value = support.random.string(); |
|
|
|
cache.set(key, value, ttl, function(err) { |
|
|
|
cache.set(key, value, {ttl: defaultTtl}, function(err) { |
|
|
|
checkErr(err); |
|
|
|
done(); |
|
|
|
}); |
|
|
@ -121,13 +121,13 @@ describe("caching", function() { |
|
|
|
cache = caching({store: 'memory'}); |
|
|
|
key = support.random.string(20); |
|
|
|
value = support.random.string(); |
|
|
|
cache.set(key, value, ttl, function(err) { |
|
|
|
cache.set(key, value, function(err) { |
|
|
|
checkErr(err); |
|
|
|
|
|
|
|
key2 = support.random.string(20); |
|
|
|
value2 = support.random.string(); |
|
|
|
|
|
|
|
cache.set(key2, value2, ttl, done); |
|
|
|
cache.set(key2, value2, done); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
@ -241,7 +241,7 @@ describe("caching", function() { |
|
|
|
key = support.random.string(20); |
|
|
|
savedKeys.push(key); |
|
|
|
value = support.random.string(); |
|
|
|
cache.set(key, value, ttl, cb); |
|
|
|
cache.set(key, value, cb); |
|
|
|
}, done); |
|
|
|
}); |
|
|
|
|
|
|
@ -262,14 +262,15 @@ describe("caching", function() { |
|
|
|
describe("wrap()", function() { |
|
|
|
describe("using memory (lru-cache) store", function() { |
|
|
|
var memoryStoreStub; |
|
|
|
var opts; |
|
|
|
|
|
|
|
beforeEach(function() { |
|
|
|
ttl = 0.1; |
|
|
|
memoryStoreStub = memoryStore.create({ttl: ttl}); |
|
|
|
opts = {ttl: 0.1}; |
|
|
|
memoryStoreStub = memoryStore.create(opts); |
|
|
|
|
|
|
|
sinon.stub(memoryStore, 'create').returns(memoryStoreStub); |
|
|
|
|
|
|
|
cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: false}); |
|
|
|
cache = caching({store: 'memory', ttl: opts.ttl, ignoreCacheErrors: false}); |
|
|
|
key = support.random.string(20); |
|
|
|
name = support.random.string(); |
|
|
|
}); |
|
|
@ -290,10 +291,10 @@ describe("caching", function() { |
|
|
|
it("when a ttl is passed in", function(done) { |
|
|
|
cache.wrap(key, function(cb) { |
|
|
|
methods.getWidget(name, cb); |
|
|
|
}, ttl, function(err, widget) { |
|
|
|
}, opts, function(err, widget) { |
|
|
|
checkErr(err); |
|
|
|
assert.deepEqual(widget, {name: name}); |
|
|
|
sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, ttl); |
|
|
|
sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, opts); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -304,7 +305,7 @@ describe("caching", function() { |
|
|
|
}, function(err, widget) { |
|
|
|
checkErr(err); |
|
|
|
assert.deepEqual(widget, {name: name}); |
|
|
|
sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, undefined); |
|
|
|
sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, {}); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -314,7 +315,7 @@ describe("caching", function() { |
|
|
|
function getCachedWidget(name, cb) { |
|
|
|
cache.wrap(key, function(cacheCb) { |
|
|
|
methods.getWidget(name, cacheCb); |
|
|
|
}, ttl, cb); |
|
|
|
}, opts, cb); |
|
|
|
} |
|
|
|
|
|
|
|
beforeEach(function(done) { |
|
|
@ -345,7 +346,7 @@ describe("caching", function() { |
|
|
|
funcCalled = true; |
|
|
|
cb(err, result); |
|
|
|
}); |
|
|
|
}, ttl, function(err, widget) { |
|
|
|
}, function(err, widget) { |
|
|
|
checkErr(err); |
|
|
|
assert.deepEqual(widget, {name: name}); |
|
|
|
assert.ok(memoryStoreStub.get.calledWith(key)); |
|
|
@ -368,7 +369,7 @@ describe("caching", function() { |
|
|
|
function getCachedFalseyValue(cb) { |
|
|
|
cache.wrap(key, function(cacheCb) { |
|
|
|
getFalseyValue(cacheCb); |
|
|
|
}, ttl, cb); |
|
|
|
}, cb); |
|
|
|
} |
|
|
|
|
|
|
|
beforeEach(function(done) { |
|
|
@ -424,7 +425,7 @@ describe("caching", function() { |
|
|
|
getValue(name, function(err, result) { |
|
|
|
cacheCb(err, result); |
|
|
|
}); |
|
|
|
}, ttl, cb); |
|
|
|
}, {ttl: defaultTtl}, cb); |
|
|
|
} |
|
|
|
|
|
|
|
beforeEach(function() { |
|
|
@ -490,9 +491,11 @@ describe("caching", function() { |
|
|
|
}); |
|
|
|
|
|
|
|
it("expires cached result after ttl seconds", function(done) { |
|
|
|
var ttl = 0.1; |
|
|
|
|
|
|
|
cache.wrap(key, function(cb) { |
|
|
|
methods.getWidget(name, cb); |
|
|
|
}, ttl, function(err, widget) { |
|
|
|
}, {ttl: ttl}, function(err, widget) { |
|
|
|
checkErr(err); |
|
|
|
assert.deepEqual(widget, {name: name}); |
|
|
|
|
|
|
@ -529,7 +532,7 @@ describe("caching", function() { |
|
|
|
it("bubbles up that error", function(done) { |
|
|
|
cache.wrap(key, function() { |
|
|
|
throw fakeError; |
|
|
|
}, ttl, function(err) { |
|
|
|
}, function(err) { |
|
|
|
assert.equal(err, fakeError); |
|
|
|
done(); |
|
|
|
}); |
|
|
@ -547,7 +550,7 @@ describe("caching", function() { |
|
|
|
|
|
|
|
cache.wrap(key, function(cb) { |
|
|
|
methods.getWidget(name, cb); |
|
|
|
}, ttl, function(err) { |
|
|
|
}, function(err) { |
|
|
|
assert.equal(err, fakeError); |
|
|
|
memoryStoreStub.get.restore(); |
|
|
|
done(); |
|
|
@ -557,7 +560,7 @@ describe("caching", function() { |
|
|
|
|
|
|
|
context("and ignoreCacheErrors is set to true", function() { |
|
|
|
it("does not bubble up that error", function(done) { |
|
|
|
cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: true}); |
|
|
|
cache = caching({store: 'memory', ttl: defaultTtl, ignoreCacheErrors: true}); |
|
|
|
|
|
|
|
var fakeError = new Error(support.random.string()); |
|
|
|
|
|
|
@ -567,7 +570,7 @@ describe("caching", function() { |
|
|
|
|
|
|
|
cache.wrap(key, function(cb) { |
|
|
|
methods.getWidget(name, cb); |
|
|
|
}, ttl, function(err) { |
|
|
|
}, function(err) { |
|
|
|
assert.equal(err, null); |
|
|
|
memoryStoreStub.get.restore(); |
|
|
|
done(); |
|
|
@ -587,7 +590,7 @@ describe("caching", function() { |
|
|
|
|
|
|
|
cache.wrap(key, function(cb) { |
|
|
|
methods.getWidget(name, cb); |
|
|
|
}, ttl, function(err) { |
|
|
|
}, function(err) { |
|
|
|
assert.equal(err, fakeError); |
|
|
|
memoryStoreStub.set.restore(); |
|
|
|
done(); |
|
|
@ -597,16 +600,14 @@ describe("caching", function() { |
|
|
|
|
|
|
|
context("and ignoreCacheErrors is set to true", function() { |
|
|
|
it("does not bubbles up that error", function(done) { |
|
|
|
cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: true}); |
|
|
|
cache = caching({store: 'memory', ttl: defaultTtl, ignoreCacheErrors: true}); |
|
|
|
var fakeError = new Error(support.random.string()); |
|
|
|
|
|
|
|
sinon.stub(memoryStoreStub, 'set', function(key, val, ttl, cb) { |
|
|
|
cb(fakeError); |
|
|
|
}); |
|
|
|
sinon.stub(memoryStoreStub, 'set').yields(fakeError); |
|
|
|
|
|
|
|
cache.wrap(key, function(cb) { |
|
|
|
methods.getWidget(name, cb); |
|
|
|
}, ttl, function(err) { |
|
|
|
}, function(err) { |
|
|
|
assert.equal(err, null); |
|
|
|
memoryStoreStub.set.restore(); |
|
|
|
done(); |
|
|
@ -624,7 +625,7 @@ describe("caching", function() { |
|
|
|
|
|
|
|
cache.wrap(key, function(cb) { |
|
|
|
methods.getWidget(name, cb); |
|
|
|
}, ttl, function(err, widget) { |
|
|
|
}, function(err, widget) { |
|
|
|
methods.getWidget.restore(); |
|
|
|
assert.equal(err, fakeError); |
|
|
|
assert.ok(!widget); |
|
|
@ -661,7 +662,7 @@ describe("caching", function() { |
|
|
|
async.each(values, function(val, next) { |
|
|
|
cache.wrap('key', function(cb) { |
|
|
|
construct(val, cb); |
|
|
|
}, ttl, function(err, result) { |
|
|
|
}, function(err, result) { |
|
|
|
assert.equal(result, 'value'); |
|
|
|
next(err); |
|
|
|
}); |
|
|
@ -683,9 +684,9 @@ describe("caching", function() { |
|
|
|
|
|
|
|
describe("instantiating with custom store", function() { |
|
|
|
it("allows us to pass in our own store object", function(done) { |
|
|
|
var store = memoryStore.create({ttl: ttl}); |
|
|
|
var store = memoryStore.create({ttl: defaultTtl}); |
|
|
|
cache = caching({store: store}); |
|
|
|
cache.set(key, value, ttl, function(err) { |
|
|
|
cache.set(key, value, function(err) { |
|
|
|
checkErr(err); |
|
|
|
cache.get(key, function(err, result) { |
|
|
|
assert.equal(result, value); |
|
|
@ -697,7 +698,7 @@ describe("caching", function() { |
|
|
|
it("allows us to pass in a path to our own store", function(done) { |
|
|
|
var storePath = '../lib/stores/memory'; |
|
|
|
cache = caching({store: storePath}); |
|
|
|
cache.set(key, value, ttl, function(err) { |
|
|
|
cache.set(key, value, {ttl: defaultTtl}, function(err) { |
|
|
|
checkErr(err); |
|
|
|
cache.get(key, function(err, result) { |
|
|
|
assert.equal(result, value); |
|
|
@ -709,7 +710,7 @@ describe("caching", function() { |
|
|
|
it("allows us to pass in a module (uninstantiated)", function(done) { |
|
|
|
var store = memoryStore; |
|
|
|
cache = caching({store: store}); |
|
|
|
cache.set(key, value, ttl, function(err) { |
|
|
|
cache.set(key, value, {ttl: defaultTtl}, function(err) { |
|
|
|
checkErr(err); |
|
|
|
cache.get(key, function(err, result) { |
|
|
|
assert.equal(result, value); |
|
|
|