Handle shutdown for RedisCacheAdapter (#6658)
* Handle shutdown for RedisCacheAdapter * connected value need to be tested in setTimeout Co-authored-by: Promise Xu <promise@klido.me>
This commit is contained in:
@@ -9,17 +9,17 @@ and make sure a redis server is available on the default port
|
||||
*/
|
||||
describe_only(() => {
|
||||
return process.env.PARSE_SERVER_TEST_CACHE === 'redis';
|
||||
})('RedisCacheAdapter', function() {
|
||||
})('RedisCacheAdapter', function () {
|
||||
const KEY = 'hello';
|
||||
const VALUE = 'world';
|
||||
|
||||
function wait(sleep) {
|
||||
return new Promise(function(resolve) {
|
||||
return new Promise(function (resolve) {
|
||||
setTimeout(resolve, sleep);
|
||||
});
|
||||
}
|
||||
|
||||
it('should get/set/clear', done => {
|
||||
it('should get/set/clear', (done) => {
|
||||
const cache = new RedisCacheAdapter({
|
||||
ttl: NaN,
|
||||
});
|
||||
@@ -27,85 +27,94 @@ describe_only(() => {
|
||||
cache
|
||||
.put(KEY, VALUE)
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(VALUE))
|
||||
.then((value) => expect(value).toEqual(VALUE))
|
||||
.then(() => cache.clear())
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(null))
|
||||
.then((value) => expect(value).toEqual(null))
|
||||
.then(done);
|
||||
});
|
||||
|
||||
it('should expire after ttl', done => {
|
||||
it('should expire after ttl', (done) => {
|
||||
const cache = new RedisCacheAdapter(null, 50);
|
||||
|
||||
cache
|
||||
.put(KEY, VALUE)
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(VALUE))
|
||||
.then((value) => expect(value).toEqual(VALUE))
|
||||
.then(wait.bind(null, 52))
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(null))
|
||||
.then((value) => expect(value).toEqual(null))
|
||||
.then(done);
|
||||
});
|
||||
|
||||
it('should not store value for ttl=0', done => {
|
||||
it('should not store value for ttl=0', (done) => {
|
||||
const cache = new RedisCacheAdapter(null, 5);
|
||||
|
||||
cache
|
||||
.put(KEY, VALUE, 0)
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(null))
|
||||
.then((value) => expect(value).toEqual(null))
|
||||
.then(done);
|
||||
});
|
||||
|
||||
it('should not expire when ttl=Infinity', done => {
|
||||
it('should not expire when ttl=Infinity', (done) => {
|
||||
const cache = new RedisCacheAdapter(null, 1);
|
||||
|
||||
cache
|
||||
.put(KEY, VALUE, Infinity)
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(VALUE))
|
||||
.then((value) => expect(value).toEqual(VALUE))
|
||||
.then(wait.bind(null, 5))
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(VALUE))
|
||||
.then((value) => expect(value).toEqual(VALUE))
|
||||
.then(done);
|
||||
});
|
||||
|
||||
it('should fallback to default ttl', done => {
|
||||
it('should fallback to default ttl', (done) => {
|
||||
const cache = new RedisCacheAdapter(null, 1);
|
||||
let promise = Promise.resolve();
|
||||
|
||||
[-100, null, undefined, 'not number', true].forEach(ttl => {
|
||||
[-100, null, undefined, 'not number', true].forEach((ttl) => {
|
||||
promise = promise.then(() =>
|
||||
cache
|
||||
.put(KEY, VALUE, ttl)
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(VALUE))
|
||||
.then((value) => expect(value).toEqual(VALUE))
|
||||
.then(wait.bind(null, 5))
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(null))
|
||||
.then((value) => expect(value).toEqual(null))
|
||||
);
|
||||
});
|
||||
|
||||
promise.then(done);
|
||||
});
|
||||
|
||||
it('should find un-expired records', done => {
|
||||
it('should find un-expired records', (done) => {
|
||||
const cache = new RedisCacheAdapter(null, 5);
|
||||
|
||||
cache
|
||||
.put(KEY, VALUE)
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).toEqual(VALUE))
|
||||
.then((value) => expect(value).toEqual(VALUE))
|
||||
.then(wait.bind(null, 1))
|
||||
.then(() => cache.get(KEY))
|
||||
.then(value => expect(value).not.toEqual(null))
|
||||
.then((value) => expect(value).not.toEqual(null))
|
||||
.then(done);
|
||||
});
|
||||
|
||||
it('handleShutdown, close connection', async () => {
|
||||
const cache = new RedisCacheAdapter(null, 5);
|
||||
|
||||
await cache.handleShutdown();
|
||||
setTimeout(() => {
|
||||
expect(cache.client.connected).toBe(false);
|
||||
}, 0);
|
||||
});
|
||||
});
|
||||
|
||||
describe_only(() => {
|
||||
return process.env.PARSE_SERVER_TEST_CACHE === 'redis';
|
||||
})('RedisCacheAdapter/KeyPromiseQueue', function() {
|
||||
})('RedisCacheAdapter/KeyPromiseQueue', function () {
|
||||
const KEY1 = 'key1';
|
||||
const KEY2 = 'key2';
|
||||
const VALUE = 'hello';
|
||||
@@ -120,7 +129,7 @@ describe_only(() => {
|
||||
return Object.keys(cache.queue.queue).length;
|
||||
}
|
||||
|
||||
it('it should clear completed operations from queue', done => {
|
||||
it('it should clear completed operations from queue', (done) => {
|
||||
const cache = new RedisCacheAdapter({ ttl: NaN });
|
||||
|
||||
// execute a bunch of operations in sequence
|
||||
@@ -142,7 +151,7 @@ describe_only(() => {
|
||||
promise.then(() => expect(getQueueCount(cache)).toEqual(0)).then(done);
|
||||
});
|
||||
|
||||
it('it should count per key chained operations correctly', done => {
|
||||
it('it should count per key chained operations correctly', (done) => {
|
||||
const cache = new RedisCacheAdapter({ ttl: NaN });
|
||||
|
||||
let key1Promise = Promise.resolve();
|
||||
@@ -168,7 +177,7 @@ describe_only(() => {
|
||||
|
||||
describe_only(() => {
|
||||
return process.env.PARSE_SERVER_TEST_CACHE === 'redis';
|
||||
})('Redis Performance', function() {
|
||||
})('Redis Performance', function () {
|
||||
let cacheAdapter;
|
||||
let getSpy;
|
||||
let putSpy;
|
||||
|
||||
Reference in New Issue
Block a user