38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { assertEquals } from "@std/assert";
|
|
import { FakeTime } from "@std/testing/time";
|
|
import { TtlCache } from "../../lib/ttl-cache.ts";
|
|
|
|
Deno.test("TtlCache: an entry expires ttlMs after it is written", () => {
|
|
using time = new FakeTime();
|
|
const cache = new TtlCache<string>(1000, 10);
|
|
cache.set("a", "x");
|
|
time.tick(999);
|
|
assertEquals(cache.get("a"), "x");
|
|
time.tick(1);
|
|
assertEquals(cache.get("a"), undefined);
|
|
});
|
|
|
|
Deno.test("TtlCache: past maxEntries, the oldest write goes first", () => {
|
|
const cache = new TtlCache<number>(1000, 2);
|
|
cache.set("a", 1);
|
|
cache.set("b", 2);
|
|
// Rewriting "a" makes it the newest, so "b" is the oldest now.
|
|
cache.set("a", 3);
|
|
cache.set("c", 4);
|
|
assertEquals(cache.get("a"), 3);
|
|
assertEquals(cache.get("b"), undefined);
|
|
assertEquals(cache.get("c"), 4);
|
|
});
|
|
|
|
Deno.test("TtlCache: delete with a value spares a newer entry", () => {
|
|
const cache = new TtlCache<object>(1000, 10);
|
|
const first = {};
|
|
const second = {};
|
|
cache.set("k", first);
|
|
cache.set("k", second);
|
|
cache.delete("k", first);
|
|
assertEquals(cache.get("k"), second);
|
|
cache.delete("k", second);
|
|
assertEquals(cache.get("k"), undefined);
|
|
});
|