Browse Source

minor fixes in memory store for promise support

feature/non-promise-memory-store
Bryan Donovan 9 years ago
parent
commit
a4b9ca7140
  1. 4
      lib/stores/memory.js
  2. 1
      package.json
  3. 41
      test/caching.unit.js
  4. 3
      test/stores/memory.unit.js

4
lib/stores/memory.js

@ -45,7 +45,7 @@ var memoryStore = function(args) {
cb(null, value); cb(null, value);
}); });
} else if (self.usePromises) { } else if (self.usePromises) {
return Promise.resolve(value) return Promise.resolve(value);
} else { } else {
return value; return value;
} }
@ -81,7 +81,7 @@ var memoryStore = function(args) {
cb(null, keys); cb(null, keys);
}); });
} else if (self.usePromises) { } else if (self.usePromises) {
return Promise.resolve(); return Promise.resolve(keys);
} else { } else {
return keys; return keys;
} }

1
package.json

@ -28,6 +28,7 @@
"es6-promise": "^3.0.2", "es6-promise": "^3.0.2",
"istanbul": "^0.2.11", "istanbul": "^0.2.11",
"jscs": "^1.9.0", "jscs": "^1.9.0",
"jsdoc": "^3.3.0",
"jshint": "^2.5.4", "jshint": "^2.5.4",
"mocha": "^1.20.1", "mocha": "^1.20.1",
"optimist": "^0.6.1", "optimist": "^0.6.1",

41
test/caching.unit.js

@ -237,10 +237,11 @@ describe("caching", function() {
describe("keys()", function() { describe("keys()", function() {
var keyCount; var keyCount;
var savedKeys = []; var savedKeys;
beforeEach(function(done) { beforeEach(function(done) {
keyCount = 10; keyCount = 10;
savedKeys = [];
var processed = 0; var processed = 0;
cache = caching({store: 'memory'}); cache = caching({store: 'memory'});
@ -261,15 +262,47 @@ describe("caching", function() {
it("calls back with all keys in cache", function(done) { it("calls back with all keys in cache", function(done) {
cache.keys(function(err, keys) { cache.keys(function(err, keys) {
checkErr(err); checkErr(err);
assert.deepEqual(keys.sort, savedKeys.sort); assert.deepEqual(keys.sort(), savedKeys.sort());
done(); done();
}); });
}); });
it("lets us get the keys without a callback (memory store only)", function() { it("lets us set and get data without a callback, returning a promise", function(done) {
cache.keys()
.then(function(keys) {
assert.deepEqual(keys.sort(), savedKeys.sort());
done();
})
.catch(function(err) {
done(err);
});
});
context("when not using promises", function() {
beforeEach(function(done) {
savedKeys = [];
keyCount = 10;
var processed = 0;
cache = caching({store: memoryStore.create({noPromises: true})}); cache = caching({store: memoryStore.create({noPromises: true})});
function isDone() {
return processed === keyCount;
}
async.until(isDone, function(cb) {
processed += 1;
key = support.random.string(20);
savedKeys.push(key);
value = support.random.string();
cache.set(key, value, cb);
}, done);
});
it("lets us get the keys without a callback (memory store only)", function() {
var keys = cache.keys(); var keys = cache.keys();
assert.deepEqual(keys.sort, savedKeys.sort); assert.deepEqual(keys.sort(), savedKeys.sort());
});
}); });
}); });

3
test/stores/memory.unit.js

@ -17,7 +17,8 @@ describe("memory store", function() {
memoryCache = memoryStore.create({noPromises: true}); memoryCache = memoryStore.create({noPromises: true});
}); });
it("does not require a callback", function(done) { // This test should pass in node v0.10.x:
it("does not require a callback or use of Promises", function(done) {
memoryCache.set('foo', 'bar'); memoryCache.set('foo', 'bar');
setTimeout(function() { setTimeout(function() {

Loading…
Cancel
Save