From 0131d997f13690fde5438099e374d7b384f919a6 Mon Sep 17 00:00:00 2001 From: Maxim Kucherov Date: Wed, 24 Aug 2022 18:37:13 +0300 Subject: [PATCH] Added tests for cache --- tests/core/cache/cache.test.ts | 50 +++++++++++++++++++ .../testCases/absoluteExpirationTestCases.ts | 34 +++++++++++++ tests/core/cache/testCases/index.ts | 2 + .../testCases/slidingExpirationTestCases.ts | 34 +++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 tests/core/cache/cache.test.ts create mode 100644 tests/core/cache/testCases/absoluteExpirationTestCases.ts create mode 100644 tests/core/cache/testCases/index.ts create mode 100644 tests/core/cache/testCases/slidingExpirationTestCases.ts diff --git a/tests/core/cache/cache.test.ts b/tests/core/cache/cache.test.ts new file mode 100644 index 00000000..2213ad89 --- /dev/null +++ b/tests/core/cache/cache.test.ts @@ -0,0 +1,50 @@ +import { InMemoryCache } from '../../../src/core'; +import { wait } from '../../../src/utils'; +import { absoluteExpirationTestCases, slidingExpirationTestCases } from './testCases'; + +describe('InMemoryCache', () => { + test.each(absoluteExpirationTestCases)( + 'sets and returns values with absolute expiration (%s)', + async (_, [defaultSetOptions, setOptions, cacheLiveTime]) => { + const cache = new InMemoryCache(defaultSetOptions); + const key = 'key1'; + const value = 'someValue'; + + cache.set(key, value, setOptions); + const returnedValue = cache.get(key); + expect(returnedValue).toEqual(value); + + await wait(cacheLiveTime / 2); + cache.get(key); + await wait(cacheLiveTime / 2); + + const cleanedValue = cache.get(key); + expect(cleanedValue).toBeUndefined(); + } + ); + + test.each(slidingExpirationTestCases)( + 'sets and returns values with sliding expiration (%s)', + async (_, [defaultSetOptions, setOptions, cacheLiveTime]) => { + const cache = new InMemoryCache(defaultSetOptions); + const key = 'key1'; + const value = 'someValue'; + + cache.set(key, value, setOptions); + const returnedValue = cache.get(key); + expect(returnedValue).toEqual(value); + + await wait(cacheLiveTime / 2); + cache.get(key); + await wait(cacheLiveTime / 2); + + const stillStoredValue = cache.get(key); + expect(stillStoredValue).toEqual(value); + + await wait(cacheLiveTime); + + const cleanedValue = cache.get(key); + expect(cleanedValue).toBeUndefined(); + } + ); +}); diff --git a/tests/core/cache/testCases/absoluteExpirationTestCases.ts b/tests/core/cache/testCases/absoluteExpirationTestCases.ts new file mode 100644 index 00000000..25a0af88 --- /dev/null +++ b/tests/core/cache/testCases/absoluteExpirationTestCases.ts @@ -0,0 +1,34 @@ +import type { SetCacheOptions } from '../../../../src/core'; + +const absoluteExpirationTestCases: ReadonlyArray<[ + message: string, + testData: readonly [defaultSetOptions: SetCacheOptions | undefined, setOptions: SetCacheOptions | undefined, cacheLiveTime: number] +]> = [ + [ + 'default options', + [ + { absoluteExpirationMs: 1000 }, + undefined, + 1000 + ] + ], + [ + 'actual options', + [ + undefined, + { absoluteExpirationMs: 1000 }, + 1000 + ] + ], + [ + 'actual with overridden default options', + [ + { absoluteExpirationMs: 3000 }, + { absoluteExpirationMs: 1000 }, + 1000 + ] + ] + ]; + + +export default absoluteExpirationTestCases; diff --git a/tests/core/cache/testCases/index.ts b/tests/core/cache/testCases/index.ts new file mode 100644 index 00000000..e0d14ebb --- /dev/null +++ b/tests/core/cache/testCases/index.ts @@ -0,0 +1,2 @@ +export { default as absoluteExpirationTestCases } from './absoluteExpirationTestCases'; +export { default as slidingExpirationTestCases } from './slidingExpirationTestCases'; diff --git a/tests/core/cache/testCases/slidingExpirationTestCases.ts b/tests/core/cache/testCases/slidingExpirationTestCases.ts new file mode 100644 index 00000000..3ecdcca6 --- /dev/null +++ b/tests/core/cache/testCases/slidingExpirationTestCases.ts @@ -0,0 +1,34 @@ +import type { SetCacheOptions } from '../../../../src/core'; + +const absoluteExpirationTestCases: ReadonlyArray<[ + message: string, + testData: readonly [defaultSetOptions: SetCacheOptions | undefined, setOptions: SetCacheOptions | undefined, cacheLiveTime: number] +]> = [ + [ + 'default options', + [ + { slidingExpirationMs: 1000 }, + undefined, + 1000 + ] + ], + [ + 'actual options', + [ + undefined, + { slidingExpirationMs: 1000 }, + 1000 + ] + ], + [ + 'actual with overridden default options', + [ + { slidingExpirationMs: 3000 }, + { slidingExpirationMs: 1000 }, + 1000 + ] + ] + ]; + + +export default absoluteExpirationTestCases;