Browse Source

fixing multi_cache.wrap() when wrapped function calls back with an error

hotfix/0.7.1
Bryan Donovan 12 years ago
parent
commit
16d1024017
  1. 1
      .jshintrc
  2. 4
      lib/multi_caching.js
  3. 67
      test/multi_caching.unit.js

1
.jshintrc

@ -43,7 +43,6 @@
// EcmaScript 5.
"es5" : true, // Allow EcmaScript 5 syntax.
"strict" : false, // Require `use strict` pragma in every file.
"globalstrict" : false, // Allow global "use strict" (also enables 'strict').

4
lib/multi_caching.js

@ -50,7 +50,9 @@ var multi_caching = function (caches) {
} else {
work(function () {
var work_args = Array.prototype.slice.call(arguments, 0);
if (work_args[0]) { // assume first arg is an error
return cb(work_args[0]);
}
set_in_multiple_caches(caches, key, work_args[1], function (err) {
if (err) { return cb(err); }
cb.apply(null, work_args);

67
test/multi_caching.unit.js

@ -1,14 +1,17 @@
var assert = require('assert');
var sinon = require('sinon');
var redis = require('redis');
var support = require('./support');
var check_err = support.check_err;
var caching = require('../index').caching;
var multi_caching = require('../index').multi_caching;
var memory_store = require('../lib/stores/memory');
function get_widget(name, cb) {
cb(null, {name: name});
}
var methods = {
get_widget: function (name, cb) {
cb(null, {name: name});
}
};
describe("multi_caching", function () {
var redis_cache;
@ -154,6 +157,17 @@ describe("multi_caching", function () {
});
describe("wrap()", function () {
var redis_client;
beforeEach(function () {
redis_client = redis.createClient();
sinon.stub(redis, 'createClient').returns(redis_client);
});
afterEach(function () {
redis.createClient.restore();
});
describe("using a single cache store", function () {
beforeEach(function () {
multi_cache = multi_caching([redis_cache]);
@ -161,13 +175,36 @@ describe("multi_caching", function () {
it("calls back with the result of a function", function (done) {
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
done();
});
});
context("when wrapped function calls back with an error", function () {
it("calls back with that error and doesn't cache result", function (done) {
var fake_error = new Error(support.random.string());
sinon.stub(methods, 'get_widget', function (name, cb) {
cb(fake_error, {name: name});
});
multi_cache.wrap(key, function (cb) {
methods.get_widget(name, cb);
}, function (err, widget) {
methods.get_widget.restore();
assert.equal(err, fake_error);
assert.ok(!widget);
redis_client.get(key, function (err, result) {
check_err(err);
assert.ok(!result);
done();
});
});
});
});
});
describe("using two cache stores", function () {
@ -177,7 +214,7 @@ describe("multi_caching", function () {
it("calls back with the result of a function", function (done) {
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
@ -187,7 +224,7 @@ describe("multi_caching", function () {
it("sets value in all caches", function (done) {
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
@ -211,7 +248,7 @@ describe("multi_caching", function () {
check_err(err);
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
@ -232,7 +269,7 @@ describe("multi_caching", function () {
check_err(err);
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
@ -255,7 +292,7 @@ describe("multi_caching", function () {
it("calls back with the result of a function", function (done) {
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
@ -265,7 +302,7 @@ describe("multi_caching", function () {
it("sets value in all caches", function (done) {
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
@ -294,7 +331,7 @@ describe("multi_caching", function () {
check_err(err);
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
@ -320,7 +357,7 @@ describe("multi_caching", function () {
check_err(err);
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
@ -346,7 +383,7 @@ describe("multi_caching", function () {
check_err(err);
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err, widget) {
check_err(err);
assert.deepEqual(widget, {name: name});
@ -393,7 +430,7 @@ describe("multi_caching", function () {
});
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err) {
assert.equal(err, fake_error);
memory_store_stub.get.restore();
@ -411,7 +448,7 @@ describe("multi_caching", function () {
});
multi_cache.wrap(key, function (cb) {
get_widget(name, cb);
methods.get_widget(name, cb);
}, function (err) {
assert.equal(err, fake_error);
memory_store_stub.set.restore();

Loading…
Cancel
Save