Skip to content

Commit

Permalink
Merge pull request #58 from Rowno/falsey-value
Browse files Browse the repository at this point in the history
Allow falsey values to be cached
  • Loading branch information
Eran Hammer committed Mar 19, 2014
2 parents 3214f68 + 0f6b165 commit 1b7a1a8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/client.js
Expand Up @@ -87,7 +87,8 @@ internals.Client.prototype.get = function (key, callback) {
}

if (!result ||
!result.item) {
result.item === undefined ||
result.item === null) {

// Not found
return callback(null, null);
Expand Down
5 changes: 3 additions & 2 deletions lib/policy.js
Expand Up @@ -109,7 +109,8 @@ internals.Policy.prototype.getOrGenerate = function (id, generateFunc, callback)
// Not found

if (!cached ||
!cached.item) {
cached.item === undefined ||
cached.item === null) {

report = { msec: timer.elapsed() };
return validate();
Expand Down Expand Up @@ -302,4 +303,4 @@ internals.Policy.ttl = function (rule, created) {
}

return 0; // No rule
};
};
57 changes: 56 additions & 1 deletion test/client.js
Expand Up @@ -197,6 +197,61 @@ describe('Client', function () {
});
});

it('returns nothing when item is not found', function (done) {

var engine = {
start: function (callback) {

callback();
},
isReady: function () {

return true;
},
get: function (key, callback) {

callback(null, null);
}
};

var client = new Catbox.Client(engine);
client.get({ id: 'id', segment: 'segment' }, function (err, cached) {

expect(err).to.equal(null);
expect(cached).to.equal(null);
done();
});
});

it('returns falsey items', function (done) {

var engine = {
start: function (callback) {

callback();
},
isReady: function () {

return true;
},
get: function (key, callback) {

callback(null, {
item: false,
stored: false
});
}
};

var client = new Catbox.Client(engine);
client.get({ id: 'id', segment: 'segment' }, function (err, cached) {

expect(err).to.equal(null);
expect(cached.item).to.equal(false);
done();
});
});

it('expires item', function (done) {

var engine = {
Expand Down Expand Up @@ -282,4 +337,4 @@ describe('Client', function () {
});
});
});
});
});
45 changes: 45 additions & 0 deletions test/policy.js
Expand Up @@ -221,6 +221,51 @@ describe('Policy', function () {
});
});

describe('#getOrGenerate', function () {

it('returns falsey items', function (done) {

var engine = {
start: function (callback) {

callback();
},
isReady: function () {

return true;
},
get: function (key, callback) {

callback(null, {
stored: false,
item: false
});
},
validateSegmentName: function () {

return null;
}
};
var policyConfig = {
expiresIn: 50000
};

function generateFunc(next) {
next(null, false);
}

var client = new Catbox.Client(engine);
var policy = new Catbox.Policy(policyConfig, client, 'test');

policy.getOrGenerate('test1', generateFunc, function (err, item) {

expect(err).to.equal(null);
expect(item).to.equal(false);
done();
});
});
});

describe('#drop', function () {

it('calls the extension clients drop function', function (done) {
Expand Down

0 comments on commit 1b7a1a8

Please sign in to comment.