|
1 | | -import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { MemoryCache, RedisCache } from "./cache"; |
3 | 3 |
|
4 | 4 | describe("MemoryCache", () => { |
5 | 5 | let cache: MemoryCache; |
6 | 6 |
|
7 | 7 | beforeEach(() => { |
8 | | - cache = new MemoryCache(); |
| 8 | + cache = new MemoryCache({ cleanupInterval: 60_000 }); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + cache.dispose(); |
9 | 13 | }); |
10 | 14 |
|
11 | 15 | it("should store and retrieve values", () => { |
@@ -58,6 +62,83 @@ describe("MemoryCache", () => { |
58 | 62 | expect(cache.get("key1")).toBeNull(); |
59 | 63 | expect(cache.get("key2")).toBe("value2"); |
60 | 64 | }); |
| 65 | + |
| 66 | + it("should respect max size limit", () => { |
| 67 | + const smallCache = new MemoryCache({ maxSize: 3, cleanupInterval: 60_000 }); |
| 68 | + |
| 69 | + smallCache.set("key1", "value1", 60); |
| 70 | + smallCache.set("key2", "value2", 60); |
| 71 | + smallCache.set("key3", "value3", 60); |
| 72 | + expect(smallCache.size).toBe(3); |
| 73 | + |
| 74 | + smallCache.set("key4", "value4", 60); |
| 75 | + expect(smallCache.size).toBe(3); |
| 76 | + expect(smallCache.get("key4")).toBe("value4"); |
| 77 | + |
| 78 | + smallCache.dispose(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should evict expired entries before evicting by LRU", async () => { |
| 82 | + const smallCache = new MemoryCache({ maxSize: 2, cleanupInterval: 60_000 }); |
| 83 | + |
| 84 | + // biome-ignore lint/style/noMagicNumbers: Short TTL for test |
| 85 | + smallCache.set("expiring", "will expire", 0.05); |
| 86 | + smallCache.set("permanent", "stays", 60); |
| 87 | + |
| 88 | + // biome-ignore lint/style/noMagicNumbers: Wait for expiry |
| 89 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 90 | + |
| 91 | + smallCache.set("new", "entry", 60); |
| 92 | + |
| 93 | + expect(smallCache.get("expiring")).toBeNull(); |
| 94 | + expect(smallCache.get("permanent")).toBe("stays"); |
| 95 | + expect(smallCache.get("new")).toBe("entry"); |
| 96 | + |
| 97 | + smallCache.dispose(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should cleanup expired entries", async () => { |
| 101 | + // biome-ignore lint/style/noMagicNumbers: Short TTL for test |
| 102 | + cache.set("expiring1", "value1", 0.05); |
| 103 | + // biome-ignore lint/style/noMagicNumbers: Short TTL for test |
| 104 | + cache.set("expiring2", "value2", 0.05); |
| 105 | + cache.set("permanent", "value3", 60); |
| 106 | + |
| 107 | + expect(cache.size).toBe(3); |
| 108 | + |
| 109 | + // biome-ignore lint/style/noMagicNumbers: Wait for expiry |
| 110 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 111 | + |
| 112 | + cache.cleanup(); |
| 113 | + |
| 114 | + expect(cache.size).toBe(1); |
| 115 | + expect(cache.get("permanent")).toBe("value3"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should report size correctly", () => { |
| 119 | + expect(cache.size).toBe(0); |
| 120 | + cache.set("key1", "value1", 60); |
| 121 | + expect(cache.size).toBe(1); |
| 122 | + cache.set("key2", "value2", 60); |
| 123 | + expect(cache.size).toBe(2); |
| 124 | + cache.del("key1"); |
| 125 | + expect(cache.size).toBe(1); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should allow updating existing key without eviction", () => { |
| 129 | + const smallCache = new MemoryCache({ maxSize: 2, cleanupInterval: 60_000 }); |
| 130 | + |
| 131 | + smallCache.set("key1", "value1", 60); |
| 132 | + smallCache.set("key2", "value2", 60); |
| 133 | + expect(smallCache.size).toBe(2); |
| 134 | + |
| 135 | + smallCache.set("key1", "updated", 60); |
| 136 | + expect(smallCache.size).toBe(2); |
| 137 | + expect(smallCache.get("key1")).toBe("updated"); |
| 138 | + expect(smallCache.get("key2")).toBe("value2"); |
| 139 | + |
| 140 | + smallCache.dispose(); |
| 141 | + }); |
61 | 142 | }); |
62 | 143 |
|
63 | 144 | describe("RedisCache", () => { |
|
0 commit comments