-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade dependencies, use vitest for test
- Loading branch information
Showing
4 changed files
with
1,055 additions
and
1,482 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,90 @@ | ||
import { expect } from 'chai' | ||
import { expect, describe, it, beforeEach, afterEach } from 'vitest' | ||
import fs from 'fs' | ||
import Cache from '../src' | ||
|
||
export const sleep = async (t: number) => { | ||
const sleep = async (t: number) => { | ||
return new Promise((resolve) => setTimeout(resolve, t)) | ||
} | ||
|
||
describe('disk cache with ttl', () => { | ||
let cache: Cache | ||
|
||
beforeEach(() => { | ||
cache = new Cache() | ||
}) | ||
|
||
afterEach(async () => { | ||
// Clean up cache after each test | ||
await cache.purge() | ||
}) | ||
|
||
it('init', () => { | ||
const cache = new Cache() | ||
expect(fs.existsSync(cache.path)).to.be.true | ||
expect(fs.existsSync(cache.path)).toBe(true) | ||
}) | ||
|
||
it('init with params', () => { | ||
let cache = new Cache({ path: '/tmp', ttl: 100, tbd: 300 }) | ||
expect(cache.ttl).to.eq(100) | ||
expect(cache.tbd).to.eq(300) | ||
expect(cache.path).to.eq('/tmp') | ||
let customCache = new Cache({ path: '/tmp', ttl: 100, tbd: 300 }) | ||
expect(customCache.ttl).toBe(100) | ||
expect(customCache.tbd).toBe(300) | ||
expect(customCache.path).toBe('/tmp') | ||
|
||
delete process.env.TMPDIR | ||
cache = new Cache({ ttl: 100, tbd: 300 }) | ||
expect(cache.path).to.eq('/tmp/hdc') | ||
customCache = new Cache({ ttl: 100, tbd: 300 }) | ||
expect(customCache.path).toBe('/tmp/hdc') | ||
}) | ||
|
||
it('set / get', async () => { | ||
const cache = new Cache() | ||
const v = Buffer.from('B') | ||
await cache.set('A', v) | ||
expect(await cache.get('A')).to.deep.eq(v) | ||
expect(await cache.get('A')).toEqual(v) | ||
|
||
const v2 = Buffer.from('AAA') | ||
await cache.set('A', v2) | ||
expect(await cache.get('A')).to.deep.eq(v2) | ||
expect(await cache.get('A')).toEqual(v2) | ||
|
||
const defaultValue = Buffer.from('AA') | ||
expect(await cache.get('B', defaultValue)).to.eq(defaultValue) | ||
expect(await cache.get('B', defaultValue)).toBe(defaultValue) | ||
}) | ||
|
||
it('set / get stale / hit / miss', async () => { | ||
const cache = new Cache() | ||
const key = 'key:1' | ||
await cache.set(key, Buffer.from('1'), 0.8) | ||
let s = await cache.has(key) | ||
expect(s).to.eq('hit') | ||
expect(s).toBe('hit') | ||
await sleep(1000) | ||
s = await cache.has(key) | ||
expect(s).to.eq('stale') | ||
expect(s).toBe('stale') | ||
const v = await cache.get(key) | ||
expect(v).to.deep.eq(Buffer.from('1')) | ||
expect(v).toEqual(Buffer.from('1')) | ||
s = await cache.has('key:2') | ||
expect(s).to.eq('miss') | ||
expect(s).toBe('miss') | ||
}) | ||
|
||
it('set / get large buffer', async () => { | ||
const cache = new Cache() | ||
const key1 = 'key:l1' | ||
const d = new Array(20000).fill('A') | ||
const buf = Buffer.from(d) | ||
await cache.set(key1, buf, 0.8) | ||
expect(await cache.get(key1)).to.deep.eq(buf) | ||
expect(await cache.get(key1)).toEqual(buf) | ||
}) | ||
|
||
it('del / get miss', async () => { | ||
const cache = new Cache() | ||
cache.set('A', Buffer.from('1')) | ||
expect(await cache.get('A')).to.deep.eq(Buffer.from('1')) | ||
await cache.set('A', Buffer.from('1')) | ||
expect(await cache.get('A')).toEqual(Buffer.from('1')) | ||
await cache.del('A') | ||
expect(await cache.get('A')).to.be.undefined | ||
expect(await cache.get('A')).toBeUndefined() | ||
await cache.del('not-exist') | ||
}) | ||
|
||
it('purge', async () => { | ||
const cache = new Cache({ ttl: 0.1, tbd: 0.1 }) | ||
const purgeCache = new Cache({ ttl: 0.1, tbd: 0.1 }) | ||
const key1 = 'key:l1' | ||
const d = new Array(20000).fill('A') | ||
const buf = Buffer.from(d) | ||
await cache.set(key1, buf) | ||
expect(await cache.get(key1)).to.deep.eq(buf) | ||
await purgeCache.set(key1, buf) | ||
expect(await purgeCache.get(key1)).toEqual(buf) | ||
await sleep(500) | ||
await cache.purge() | ||
expect(await cache.get(key1)).to.be.undefined | ||
await purgeCache.purge() | ||
expect(await purgeCache.get(key1)).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.