Skip to content

Commit

Permalink
Added tests for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
maxima-net committed Aug 24, 2022
1 parent 4f2ef8c commit 0131d99
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/core/cache/cache.test.ts
Original file line number Diff line number Diff line change
@@ -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();
}
);
});
34 changes: 34 additions & 0 deletions tests/core/cache/testCases/absoluteExpirationTestCases.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions tests/core/cache/testCases/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as absoluteExpirationTestCases } from './absoluteExpirationTestCases';
export { default as slidingExpirationTestCases } from './slidingExpirationTestCases';
34 changes: 34 additions & 0 deletions tests/core/cache/testCases/slidingExpirationTestCases.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 0131d99

Please sign in to comment.