Remove expired item before executing expire

The expire feature is falsely returning 204 instead of 404 because it is
not removing expired items before execution.
This commit is contained in:
Simon Ho 2016-09-01 21:06:08 -07:00
parent b15681968b
commit 95bc2e93cc
2 changed files with 14 additions and 0 deletions

View File

@ -121,6 +121,8 @@ function(modelName, key, value, options, callback) {
KeyValueMemoryConnector.prototype.expire = KeyValueMemoryConnector.prototype.expire =
function(modelName, key, ttl, options, callback) { function(modelName, key, ttl, options, callback) {
this._removeIfExpired(modelName, key);
var store = this._getStoreForModel(modelName); var store = this._getStoreForModel(modelName);
if (!(key in store)) { if (!(key in store)) {

View File

@ -35,6 +35,18 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
.then(function(value) { should.equal(value, null); }); .then(function(value) { should.equal(value, null); });
}); });
it('returns error when expiring a key that has expired', function() {
return Promise.resolve(CacheItem.set('expired-key', 'a-value', 1))
.delay(20)
.then(function() { return CacheItem.expire('expired-key', 1000); })
.then(
function() { throw new Error('expire() should have failed'); },
function(err) {
err.message.should.match(/expired-key/);
err.should.have.property('statusCode', 404);
});
});
it('returns error when key does not exist', function() { it('returns error when key does not exist', function() {
return CacheItem.expire('key-does-not-exist', 1).then( return CacheItem.expire('key-does-not-exist', 1).then(
function() { throw new Error('expire() should have failed'); }, function() { throw new Error('expire() should have failed'); },