loopback-datasource-juggler/test/kvao/ttl.suite.js

87 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-08-10 06:21:51 +00:00
'use strict';
var bdd = require('../helpers/bdd-if');
2016-08-10 06:21:51 +00:00
var should = require('should');
var helpers = require('./_helpers');
var Promise = require('bluebird');
module.exports = function(dataSourceFactory, connectorCapabilities) {
var TTL_PRECISION = connectorCapabilities.ttlPrecision;
// Use ~1s for stores with precision of 1 ms,
// about 3s for stores with precision of 1s.
var INITIAL_TTL = Math.max(TTL_PRECISION + 1000, TTL_PRECISION * 3);
// 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..
var SMALL_DELAY = Math.max(1, Math.floor(TTL_PRECISION / 10));
var canQueryTtl = connectorCapabilities.canQueryTtl !== false;
bdd.describeIf(canQueryTtl, 'ttl', function() {
let CacheItem;
beforeEach(setupCacheItem);
2016-08-10 06:21:51 +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(
CacheItem.set('a-key', 'a-value', {ttl: INITIAL_TTL})
)
2018-06-12 07:13:32 +00:00
.delay(SMALL_DELAY)
.then(function() { return CacheItem.ttl('a-key'); })
.then(function(ttl) { ttl.should.be.within(1, INITIAL_TTL); });
});
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) {
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-08-10 06:21:51 +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); });
});
it('fails when getting TTL for a key with expired TTL', function() {
return Promise.resolve(
CacheItem.set('expired-key', 'a-value', {ttl: TTL_PRECISION})
)
.delay(2 * TTL_PRECISION)
.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);
}
);
2016-08-10 06:21:51 +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);
}
);
2016-08-10 06:21:51 +00:00
});
function setupCacheItem() {
return helpers.givenCacheItem(dataSourceFactory)
.then(ModelCtor => CacheItem = ModelCtor);
}
2016-08-10 06:21:51 +00:00
});
};