2016-08-08 08:15:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const bdd = require('../helpers/bdd-if');
|
|
|
|
const should = require('should');
|
|
|
|
const helpers = require('./_helpers');
|
|
|
|
const Promise = require('bluebird');
|
2016-08-08 08:15:22 +00:00
|
|
|
|
|
|
|
module.exports = function(dataSourceFactory, connectorCapabilities) {
|
2016-10-07 14:26:18 +00:00
|
|
|
// While we support millisecond precision, for the purpose of tests
|
|
|
|
// it's better to use intervals at least 10ms long.
|
2018-12-07 16:13:48 +00:00
|
|
|
const ttlPrecision = connectorCapabilities.ttlPrecision || 10;
|
2016-10-07 14:26:18 +00:00
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const canExpire = connectorCapabilities.canExpire !== false;
|
2016-10-07 14:26:18 +00:00
|
|
|
|
|
|
|
bdd.describeIf(canExpire, 'expire', function() {
|
2018-12-07 16:13:48 +00:00
|
|
|
let CacheItem;
|
2017-01-10 08:18:04 +00:00
|
|
|
beforeEach(setupCacheItem);
|
2016-08-08 08:15:22 +00:00
|
|
|
|
|
|
|
it('sets key ttl - Callback API', function(done) {
|
|
|
|
CacheItem.set('a-key', 'a-value', function(err) {
|
|
|
|
if (err) return done(err);
|
2016-10-07 14:26:18 +00:00
|
|
|
CacheItem.expire('a-key', ttlPrecision, function(err) {
|
2016-08-08 08:15:22 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
setTimeout(function() {
|
|
|
|
CacheItem.get('a-key', function(err, value) {
|
|
|
|
if (err) return done(err);
|
|
|
|
should.equal(value, null);
|
|
|
|
done();
|
|
|
|
});
|
2016-10-07 14:26:18 +00:00
|
|
|
}, 2 * ttlPrecision);
|
2016-08-08 08:15:22 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets key ttl - Promise API', function() {
|
|
|
|
return CacheItem.set('a-key', 'a-value')
|
2016-10-07 14:26:18 +00:00
|
|
|
.then(function() { return CacheItem.expire('a-key', ttlPrecision); })
|
|
|
|
.delay(2 * ttlPrecision)
|
2016-08-08 08:15:22 +00:00
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) { should.equal(value, null); });
|
|
|
|
});
|
|
|
|
|
2016-09-02 04:06:08 +00:00
|
|
|
it('returns error when expiring a key that has expired', function() {
|
2016-10-07 14:26:18 +00:00
|
|
|
return Promise.resolve(CacheItem.set('expired-key', 'a-value', ttlPrecision))
|
|
|
|
.delay(2 * ttlPrecision)
|
2016-09-02 04:06:08 +00:00
|
|
|
.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);
|
2018-07-16 06:46:25 +00:00
|
|
|
}
|
|
|
|
);
|
2016-09-02 04:06:08 +00:00
|
|
|
});
|
|
|
|
|
2016-08-08 08:15:22 +00:00
|
|
|
it('returns error when key does not exist', function() {
|
2016-10-07 14:26:18 +00:00
|
|
|
return CacheItem.expire('key-does-not-exist', ttlPrecision).then(
|
2016-08-08 08:15:22 +00:00
|
|
|
function() { throw new Error('expire() should have failed'); },
|
2016-08-08 15:22:33 +00:00
|
|
|
function(err) {
|
|
|
|
err.message.should.match(/key-does-not-exist/);
|
|
|
|
err.should.have.property('statusCode', 404);
|
2018-07-16 06:46:25 +00:00
|
|
|
}
|
|
|
|
);
|
2016-08-08 08:15:22 +00:00
|
|
|
});
|
2017-01-10 08:18:04 +00:00
|
|
|
|
|
|
|
function setupCacheItem() {
|
|
|
|
return helpers.givenCacheItem(dataSourceFactory)
|
|
|
|
.then(ModelCtor => CacheItem = ModelCtor);
|
2018-12-07 16:13:48 +00:00
|
|
|
}
|
2016-08-08 08:15:22 +00:00
|
|
|
});
|
|
|
|
};
|