2019-05-08 15:45:37 +00:00
|
|
|
// Copyright IBM Corp. 2016,2018. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-10 06:21:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const bdd = require('../helpers/bdd-if');
|
|
|
|
const should = require('should');
|
|
|
|
const helpers = require('./_helpers');
|
2016-08-10 06:21:51 +00:00
|
|
|
|
|
|
|
module.exports = function(dataSourceFactory, connectorCapabilities) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const TTL_PRECISION = connectorCapabilities.ttlPrecision;
|
2016-10-07 14:26:18 +00:00
|
|
|
|
|
|
|
// Use ~1s for stores with precision of 1 ms,
|
|
|
|
// about 3s for stores with precision of 1s.
|
2018-12-07 14:54:29 +00:00
|
|
|
const INITIAL_TTL = Math.max(TTL_PRECISION + 1000, TTL_PRECISION * 3);
|
2016-10-07 14:26:18 +00:00
|
|
|
|
|
|
|
// A small delay to allow the backend to process the request, run any
|
|
|
|
// TTL/expire checks, etc. Use 1ms for backends supporting sub-10ms
|
|
|
|
// resolution to ensure the delay is not too short..
|
2018-12-07 14:54:29 +00:00
|
|
|
const SMALL_DELAY = Math.max(1, Math.floor(TTL_PRECISION / 10));
|
2016-10-07 14:26:18 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const canQueryTtl = connectorCapabilities.canQueryTtl !== false;
|
2016-10-07 14:26:18 +00:00
|
|
|
|
|
|
|
bdd.describeIf(canQueryTtl, 'ttl', function() {
|
2017-01-10 08:18:04 +00:00
|
|
|
let CacheItem;
|
|
|
|
beforeEach(setupCacheItem);
|
2016-08-10 06:21:51 +00:00
|
|
|
|
2016-09-04 19:13:13 +00:00
|
|
|
it('gets TTL when key with unexpired TTL exists - Promise API',
|
2018-06-12 07:13:32 +00:00
|
|
|
function() {
|
|
|
|
return Promise.resolve(
|
2019-12-03 09:09:16 +00:00
|
|
|
CacheItem.set('a-key', 'a-value', {ttl: INITIAL_TTL}),
|
2018-07-16 06:46:25 +00:00
|
|
|
)
|
2018-10-05 07:30:02 +00:00
|
|
|
.then(() => helpers.delay(SMALL_DELAY))
|
2018-06-12 07:13:32 +00:00
|
|
|
.then(function() { return CacheItem.ttl('a-key'); })
|
|
|
|
.then(function(ttl) { ttl.should.be.within(1, INITIAL_TTL); });
|
|
|
|
});
|
2016-09-04 19:13:13 +00:00
|
|
|
|
|
|
|
it('gets TTL when key with unexpired TTL exists - Callback API',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
CacheItem.set('a-key', 'a-value', {ttl: INITIAL_TTL}, function(err) {
|
2016-09-04 19:13:13 +00:00
|
|
|
if (err) return done(err);
|
2018-06-12 07:13:32 +00:00
|
|
|
CacheItem.ttl('a-key', function(err, ttl) {
|
|
|
|
if (err) return done(err);
|
|
|
|
ttl.should.be.within(1, INITIAL_TTL);
|
|
|
|
done();
|
|
|
|
});
|
2016-08-10 06:21:51 +00:00
|
|
|
});
|
2016-09-04 19:13:13 +00:00
|
|
|
});
|
2016-08-10 06:21:51 +00:00
|
|
|
|
2016-09-04 19:13:13 +00:00
|
|
|
it('succeeds when key without TTL exists', function() {
|
2016-08-10 06:21:51 +00:00
|
|
|
return CacheItem.set('a-key', 'a-value')
|
|
|
|
.then(function() { return CacheItem.ttl('a-key'); })
|
|
|
|
.then(function(ttl) { should.not.exist(ttl); });
|
|
|
|
});
|
|
|
|
|
2016-09-04 19:13:13 +00:00
|
|
|
it('fails when getting TTL for a key with expired TTL', function() {
|
|
|
|
return Promise.resolve(
|
2019-12-03 09:09:16 +00:00
|
|
|
CacheItem.set('expired-key', 'a-value', {ttl: TTL_PRECISION}),
|
2018-07-16 06:46:25 +00:00
|
|
|
)
|
2018-10-05 07:30:02 +00:00
|
|
|
.then(() => helpers.delay(2 * TTL_PRECISION))
|
2016-09-04 19:13:13 +00:00
|
|
|
.then(function() {
|
|
|
|
return CacheItem.ttl('expired-key');
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
function() { throw new Error('ttl() should have failed'); },
|
|
|
|
function(err) {
|
|
|
|
err.message.should.match(/expired-key/);
|
|
|
|
err.should.have.property('statusCode', 404);
|
2019-12-03 09:09:16 +00:00
|
|
|
},
|
2018-07-16 06:46:25 +00:00
|
|
|
);
|
2016-08-10 06:21:51 +00:00
|
|
|
});
|
|
|
|
|
2016-09-04 19:13:13 +00:00
|
|
|
it('fails when key does not exist', function() {
|
|
|
|
return CacheItem.ttl('key-does-not-exist').then(
|
|
|
|
function() { throw new Error('ttl() should have failed'); },
|
|
|
|
function(err) {
|
|
|
|
err.message.should.match(/key-does-not-exist/);
|
|
|
|
err.should.have.property('statusCode', 404);
|
2019-12-03 09:09:16 +00:00
|
|
|
},
|
2018-07-16 06:46:25 +00:00
|
|
|
);
|
2016-08-10 06:21:51 +00:00
|
|
|
});
|
2017-01-10 08:18:04 +00:00
|
|
|
|
|
|
|
function setupCacheItem() {
|
|
|
|
return helpers.givenCacheItem(dataSourceFactory)
|
|
|
|
.then(ModelCtor => CacheItem = ModelCtor);
|
|
|
|
}
|
2016-08-10 06:21:51 +00:00
|
|
|
});
|
|
|
|
};
|