-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmod.test.ts
More file actions
255 lines (228 loc) · 7.18 KB
/
Copy pathmod.test.ts
File metadata and controls
255 lines (228 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { assert, assertEquals, assertNotEquals } from "@std/assert";
import { caches as lruCache } from "./lrucache.ts";
import { caches as headersCache } from "./headerscache.ts";
const MAX_CACHE_SIZE = 1073741824;
const NOT_IMPLEMENTED = () => {
throw new Error("Not Implemented");
};
const testCacheStorage = (
map: Map<RequestInfo | URL, Response>,
): CacheStorage => {
const getUrl = (request: RequestInfo | URL) =>
(request as Request).url ?? request.toString();
return (
{
delete: () => {
map.clear();
return Promise.resolve(true);
},
has: NOT_IMPLEMENTED,
keys: NOT_IMPLEMENTED,
match: NOT_IMPLEMENTED,
open: () => {
return (Promise.resolve({
add: NOT_IMPLEMENTED,
addAll: NOT_IMPLEMENTED,
delete: (request) => {
return Promise.resolve(Boolean(map.delete(getUrl(request))));
},
keys: NOT_IMPLEMENTED,
match: (request) => {
return Promise.resolve(map.get(getUrl(request)));
},
matchAll: NOT_IMPLEMENTED,
put: (request, response) => {
map.set(getUrl(request), response);
return Promise.resolve();
},
} as Cache));
},
}
);
};
const createRequest = (i: number) => new Request(`https://example.com/${i}`);
const CACHE_NAME = "test";
const baseTest = async (cacheStorageUT: CacheStorage) => {
const cache = await headersCache(lruCache(cacheStorageUT)).open(CACHE_NAME);
const response = () =>
new Response("Hello, World!", {
headers: { "Content-length": `${MAX_CACHE_SIZE / 2}` },
});
for (let i = 0; i < 5; i++) {
const request = createRequest(i);
await cache.put(request, response());
assert(cache.match(request));
}
for (let i = 0; i < 3; i++) {
const request = createRequest(i);
const response = await cache.match(request);
assertEquals(response, undefined);
}
for (let i = 3; i < 5; i++) {
const request = createRequest(i);
const response = await cache.match(request);
assertNotEquals(response, undefined);
}
};
Deno.test({
name: "lru_cache_adapter",
sanitizeResources: false,
sanitizeOps: false,
}, async (t) => {
await t.step(
"test base scenario",
async () => {
await baseTest(testCacheStorage(new Map()));
},
);
});
Deno.test({
name: "test one resource",
sanitizeResources: false,
sanitizeOps: false,
}, async (t) => {
const testMap = new Map();
const cache = await headersCache(lruCache(testCacheStorage(testMap))).open(
CACHE_NAME,
);
await t.step(
"test one resource without content-length",
async () => {
await cache.put(createRequest(0), new Response("Hello, World!"));
const responseWithDiscoveredLength = await cache.match(createRequest(0));
assertEquals(
responseWithDiscoveredLength?.headers.get("content-length"),
"13",
);
},
);
await t.step(
"test one resource with content-length",
async () => {
await cache.put(
createRequest(1),
new Response("Hello, World!", {
headers: { "content-length": "100" },
}),
);
const responseWithContentLength = await cache.match(createRequest(1));
assertEquals(
responseWithContentLength?.headers.get("content-length"),
"100",
);
},
);
});
Deno.test({
name: "webstandard_cache_with_adapter",
sanitizeResources: false,
sanitizeOps: false,
}, async (t) => {
await t.step(
"test base scenario",
async () => {
caches.delete(CACHE_NAME);
await baseTest(caches);
},
);
});
// TODO TESTAR O CENARIO ONDE O RESPONSE N TEM LENGTH
// ---------------------------------------------------------------------------
// Lazy re-index tests
// ---------------------------------------------------------------------------
const STALE_TTL_PERIOD_MS = parseInt(
Deno.env.get("STALE_TTL_PERIOD") ?? "30000",
);
Deno.test({
name: "lru_cache_lazy_reindex: valid entry is served after LRU restart",
sanitizeResources: false,
sanitizeOps: false,
}, async () => {
const disk = new Map<RequestInfo | URL, Response>();
const storage1 = testCacheStorage(disk);
// --- first LRU lifetime: put an entry ---
const cache1 = await headersCache(lruCache(storage1)).open(CACHE_NAME);
const futureExpires = new Date(Date.now() + 60_000).toUTCString();
await cache1.put(
createRequest(100),
new Response("cached-body", {
headers: {
"content-length": "11",
expires: futureExpires,
},
}),
);
// --- simulate pod restart: new LRU over the *same* disk map ---
const storage2 = testCacheStorage(disk);
const cache2 = await headersCache(lruCache(storage2)).open(CACHE_NAME);
const response = await cache2.match(createRequest(100));
assertNotEquals(response, undefined);
});
Deno.test({
name: "lru_cache_lazy_reindex: truly expired entry is evicted on access",
sanitizeResources: false,
sanitizeOps: false,
}, async () => {
const disk = new Map<RequestInfo | URL, Response>();
const storage1 = testCacheStorage(disk);
const cache1 = await headersCache(lruCache(storage1)).open(CACHE_NAME);
// Expired well before now, even accounting for STALE_TTL_PERIOD
const pastExpires = new Date(
Date.now() - STALE_TTL_PERIOD_MS - 60_000,
).toUTCString();
await cache1.put(
createRequest(200),
new Response("old-body", {
headers: {
"content-length": "8",
expires: pastExpires,
},
}),
);
// --- simulate pod restart ---
const storage2 = testCacheStorage(disk);
const cache2 = await headersCache(lruCache(storage2)).open(CACHE_NAME);
const response = await cache2.match(createRequest(200));
assertEquals(response, undefined);
});
Deno.test({
name: "lru_cache_lazy_reindex: entry missing from disk is a miss",
sanitizeResources: false,
sanitizeOps: false,
}, async () => {
// Empty disk — nothing was ever written
const disk = new Map<RequestInfo | URL, Response>();
const storage = testCacheStorage(disk);
const cache = await headersCache(lruCache(storage)).open(CACHE_NAME);
const response = await cache.match(createRequest(300));
assertEquals(response, undefined);
});
Deno.test({
name:
"lru_cache_lazy_reindex: re-indexed entry stays accessible on subsequent accesses",
sanitizeResources: false,
sanitizeOps: false,
}, async () => {
const disk = new Map<RequestInfo | URL, Response>();
const storage1 = testCacheStorage(disk);
const cache1 = await headersCache(lruCache(storage1)).open(CACHE_NAME);
const futureExpires = new Date(Date.now() + 60_000).toUTCString();
await cache1.put(
createRequest(400),
new Response("repeat-body", {
headers: {
"content-length": "11",
expires: futureExpires,
},
}),
);
// --- simulate pod restart ---
const storage2 = testCacheStorage(disk);
const cache2 = await headersCache(lruCache(storage2)).open(CACHE_NAME);
// First access triggers lazy re-index
const first = await cache2.match(createRequest(400));
assertNotEquals(first, undefined);
// Second access should hit the LRU directly (no re-index needed)
const second = await cache2.match(createRequest(400));
assertNotEquals(second, undefined);
});