2020-10-25 15:06:58 -05:00
|
|
|
const InMemoryCacheAdapter = require('../lib/Adapters/Cache/InMemoryCacheAdapter').default;
|
2016-05-18 12:12:30 +12:00
|
|
|
|
2020-10-25 15:06:58 -05:00
|
|
|
describe('InMemoryCacheAdapter', function () {
|
2018-02-17 09:55:30 -05:00
|
|
|
const KEY = 'hello';
|
|
|
|
|
const VALUE = 'world';
|
2016-05-18 12:12:30 +12:00
|
|
|
|
|
|
|
|
function wait(sleep) {
|
2020-10-25 15:06:58 -05:00
|
|
|
return new Promise(function (resolve) {
|
2016-05-18 12:12:30 +12:00
|
|
|
setTimeout(resolve, sleep);
|
2018-09-01 13:58:06 -04:00
|
|
|
});
|
2016-05-18 12:12:30 +12:00
|
|
|
}
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should expose promisifyed methods', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const cache = new InMemoryCacheAdapter({
|
2018-09-01 13:58:06 -04:00
|
|
|
ttl: NaN,
|
2016-05-18 12:12:30 +12:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Verify all methods return promises.
|
2020-10-25 15:06:58 -05:00
|
|
|
Promise.all([cache.put(KEY, VALUE), cache.del(KEY), cache.get(KEY), cache.clear()]).then(() => {
|
2016-05-18 12:12:30 +12:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should get/set/clear', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const cache = new InMemoryCacheAdapter({
|
2018-09-01 13:58:06 -04:00
|
|
|
ttl: NaN,
|
2016-05-18 12:12:30 +12:00
|
|
|
});
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
cache
|
|
|
|
|
.put(KEY, VALUE)
|
2016-05-18 12:12:30 +12:00
|
|
|
.then(() => cache.get(KEY))
|
2018-09-01 13:58:06 -04:00
|
|
|
.then(value => expect(value).toEqual(VALUE))
|
2016-05-18 12:12:30 +12:00
|
|
|
.then(() => cache.clear())
|
|
|
|
|
.then(() => cache.get(KEY))
|
2018-09-01 13:58:06 -04:00
|
|
|
.then(value => expect(value).toEqual(null))
|
2016-05-18 12:12:30 +12:00
|
|
|
.then(done);
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should expire after ttl', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const cache = new InMemoryCacheAdapter({
|
2018-09-01 13:58:06 -04:00
|
|
|
ttl: 10,
|
2016-05-18 12:12:30 +12:00
|
|
|
});
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
cache
|
|
|
|
|
.put(KEY, VALUE)
|
2016-05-18 12:12:30 +12:00
|
|
|
.then(() => cache.get(KEY))
|
2018-09-01 13:58:06 -04:00
|
|
|
.then(value => expect(value).toEqual(VALUE))
|
2016-05-18 12:12:30 +12:00
|
|
|
.then(wait.bind(null, 50))
|
|
|
|
|
.then(() => cache.get(KEY))
|
2018-09-01 13:58:06 -04:00
|
|
|
.then(value => expect(value).toEqual(null))
|
2016-05-18 12:12:30 +12:00
|
|
|
.then(done);
|
2018-09-01 13:58:06 -04:00
|
|
|
});
|
2016-05-18 12:12:30 +12:00
|
|
|
});
|