From 96cd8ff56b2fff82edcd86f071653055a62c1b57 Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Thu, 1 Sep 2016 21:06:08 -0700 Subject: [PATCH] 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. --- lib/connectors/kv-memory.js | 2 ++ test/kvao/expire.suite.js | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/connectors/kv-memory.js b/lib/connectors/kv-memory.js index bb015053..98cd009e 100644 --- a/lib/connectors/kv-memory.js +++ b/lib/connectors/kv-memory.js @@ -121,6 +121,8 @@ function(modelName, key, value, options, callback) { KeyValueMemoryConnector.prototype.expire = function(modelName, key, ttl, options, callback) { + this._removeIfExpired(modelName, key); + var store = this._getStoreForModel(modelName); if (!(key in store)) { diff --git a/test/kvao/expire.suite.js b/test/kvao/expire.suite.js index ca66eb8a..0d84f027 100644 --- a/test/kvao/expire.suite.js +++ b/test/kvao/expire.suite.js @@ -35,6 +35,17 @@ module.exports = function(dataSourceFactory, connectorCapabilities) { .then(function(value) { should.equal(value, null); }); }); + it('returns error when expiring a key that has expired', function() { + return 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() { return CacheItem.expire('key-does-not-exist', 1).then( function() { throw new Error('expire() should have failed'); },