levelcache/test/test.ts

44 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2023-07-20 20:54:07 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
2022-03-22 21:45:12 +00:00
import * as levelcache from '../ts/index.js';
import { CacheEntry } from '../ts/index.js';
2020-02-05 17:11:30 +00:00
2020-02-14 17:28:13 +00:00
let testLevelCache: levelcache.LevelCache;
tap.test('should create a new levelcache instance', async () => {
2020-02-15 22:38:28 +00:00
testLevelCache = new levelcache.LevelCache({
2021-04-23 18:41:30 +00:00
cacheId: 'myCache',
2020-02-15 22:38:28 +00:00
});
2022-03-22 21:45:12 +00:00
expect(testLevelCache).toBeInstanceOf(levelcache.LevelCache);
2020-02-14 17:28:13 +00:00
});
tap.test('should cache a value', async () => {
2021-04-23 18:41:30 +00:00
await testLevelCache.storeCacheEntryByKey(
'mykey',
new CacheEntry({
contents: Buffer.from('heythere'),
ttl: 10000,
typeInfo: 'string',
})
);
2021-04-23 18:40:57 +00:00
const result = await testLevelCache.retrieveCacheEntryByKey('mykey');
2022-03-22 21:45:12 +00:00
expect(result.contents.toString()).toEqual('heythere');
});
tap.test('should respect ttl', async (tools) => {
await testLevelCache.storeCacheEntryByKey(
'mykey',
new CacheEntry({
contents: Buffer.from('heythere'),
ttl: 1000,
typeInfo: 'string',
})
);
const result = await testLevelCache.retrieveCacheEntryByKey('mykey');
2022-03-22 21:45:12 +00:00
expect(result.contents.toString()).toEqual('heythere');
await tools.delayFor(1100);
const result2 = await testLevelCache.retrieveCacheEntryByKey('mykey');
2022-03-22 21:45:12 +00:00
expect(result2).toBeNull();
2020-02-05 17:11:30 +00:00
});
tap.start();