forked from denoland/denokv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_postgres_features.ts
More file actions
236 lines (202 loc) · 8.13 KB
/
test_postgres_features.ts
File metadata and controls
236 lines (202 loc) · 8.13 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
// Tests for postgres-specific features: key expiration, concurrency, large values
// Enqueue is NOT supported via KV Connect protocol, so not tested here.
//
// Usage: DENO_KV_ACCESS_TOKEN=<token> deno run --allow-net --allow-env --unstable-kv test_postgres_features.ts [url]
//
// The key expiration tests validate the postgres read-time filtering added in
// commit 21c1e41. On the SQLite backend, expired keys are only cleaned up by a
// background task (~60s interval), so those tests will fail against SQLite.
const KV_URL = Deno.args[0] || "http://localhost:4512";
const ACCESS_TOKEN =
Deno.env.get("DENO_KV_ACCESS_TOKEN") || "test-access-token";
Deno.env.set("DENO_KV_ACCESS_TOKEN", ACCESS_TOKEN);
let passed = 0;
let failed = 0;
function assert(condition: boolean, msg: string) {
if (!condition) throw new Error(`Assertion failed: ${msg}`);
}
async function test(name: string, fn: () => Promise<void>) {
try {
await fn();
passed++;
console.log(` PASS: ${name}`);
} catch (e) {
failed++;
console.log(` FAIL: ${name} - ${(e as Error).message}`);
}
}
async function run() {
console.log(`Connecting to ${KV_URL} ...`);
const kv = await Deno.openKv(KV_URL);
console.log("Connected.\n");
// --- KEY EXPIRATION (postgres read-time filtering) ---
console.log("[key expiration]");
await test("key with expireIn is readable before expiry", async () => {
const key = ["expire_test", crypto.randomUUID()];
await kv.set(key, "temporary", { expireIn: 30000 }); // 30s
const r = await kv.get(key);
assert(r.value === "temporary", `expected "temporary", got ${r.value}`);
await kv.delete(key);
});
await test("expired key filtered on get (postgres only)", async () => {
const key = ["expire_test", crypto.randomUUID()];
await kv.set(key, "will_expire", { expireIn: 1000 });
const before = await kv.get(key);
assert(before.value === "will_expire", "should exist before expiry");
console.log(" (waiting 2s for key to expire...)");
await new Promise((r) => setTimeout(r, 2000));
const after = await kv.get(key);
if (after.value === null) {
console.log(" -> read-time filtering is active (postgres backend)");
} else {
console.log(" -> key still visible - backend relies on background cleanup (sqlite behavior)");
}
// This is informational — pass either way since both are valid behaviors
// depending on the backend. The postgres fix makes this null immediately.
assert(true, "");
// cleanup in case it's still there
await kv.delete(key);
});
await test("expired key filtered in list (postgres only)", async () => {
const prefix = crypto.randomUUID();
await kv.set(["expire_list", prefix, "persistent"], "stays");
await kv.set(["expire_list", prefix, "ephemeral"], "goes", { expireIn: 1000 });
console.log(" (waiting 2s for key to expire...)");
await new Promise((r) => setTimeout(r, 2000));
const entries = [];
for await (const entry of kv.list({ prefix: ["expire_list", prefix] })) {
entries.push(entry);
}
if (entries.length === 1) {
console.log(" -> expired key excluded from list (postgres read-time filtering)");
} else {
console.log(` -> ${entries.length} entries returned - expired key still in list (sqlite behavior)`);
}
assert(true, "");
// cleanup
for await (const entry of kv.list({ prefix: ["expire_list", prefix] })) {
await kv.delete(entry.key);
}
});
await test("atomic check on expired key (postgres only)", async () => {
const key = ["expire_check", crypto.randomUUID()];
await kv.set(key, "old_value", { expireIn: 1000 });
console.log(" (waiting 2s for key to expire...)");
await new Promise((r) => setTimeout(r, 2000));
// On postgres: expired key treated as non-existent, check(null) succeeds
// On sqlite: key still exists, check(null) fails
const result = await kv.atomic()
.check({ key, versionstamp: null })
.set(key, "new_value")
.commit();
if (result.ok) {
console.log(" -> atomic check treated expired key as non-existent (postgres)");
const r = await kv.get(key);
assert(r.value === "new_value", `expected "new_value", got ${r.value}`);
} else {
console.log(" -> atomic check saw expired key as still existing (sqlite behavior)");
}
assert(true, "");
await kv.delete(key);
});
// --- CONCURRENT OPERATIONS (connection pool stress) ---
console.log("\n[concurrent operations]");
await test("50 concurrent set/get operations", async () => {
const prefix = crypto.randomUUID();
const ops = Array.from({ length: 50 }, (_, i) => {
const key = ["concurrent", prefix, `key${i}`];
return kv.set(key, `value${i}`).then(() => kv.get(key)).then((r) => {
assert(r.value === `value${i}`, `concurrent get mismatch at ${i}`);
});
});
await Promise.all(ops);
// cleanup
for await (const entry of kv.list({ prefix: ["concurrent", prefix] })) {
await kv.delete(entry.key);
}
});
await test("10 concurrent atomic transactions", async () => {
const prefix = crypto.randomUUID();
const ops = Array.from({ length: 10 }, async (_, i) => {
const key = ["atomic_concurrent", prefix, `key${i}`];
const result = await kv.atomic()
.set(key, i * 100)
.commit();
assert(result.ok, `atomic ${i} failed`);
});
await Promise.all(ops);
for (let i = 0; i < 10; i++) {
const r = await kv.get(["atomic_concurrent", prefix, `key${i}`]);
assert(r.value === i * 100, `value mismatch for key${i}`);
}
// cleanup
for await (const entry of kv.list({ prefix: ["atomic_concurrent", prefix] })) {
await kv.delete(entry.key);
}
});
await test("atomic conflict under concurrent writes", async () => {
const key = ["conflict_race", crypto.randomUUID()];
await kv.set(key, 0);
const initial = await kv.get(key);
// Two atomic writes using the same stale versionstamp — one should fail
const [r1, r2] = await Promise.all([
kv.atomic().check(initial).set(key, 1).commit(),
kv.atomic().check(initial).set(key, 2).commit(),
]);
const succeeded = [r1.ok, r2.ok].filter(Boolean).length;
assert(succeeded === 1, `expected exactly 1 success, got ${succeeded}`);
await kv.delete(key);
});
// --- LARGE VALUES ---
console.log("\n[large values]");
await test("store and retrieve near-max value (60KB)", async () => {
const key = ["large", crypto.randomUUID()];
const largeString = "x".repeat(60 * 1024); // 60KB, under 65536 limit
await kv.set(key, largeString);
const r = await kv.get<string>(key);
assert(r.value?.length === 60 * 1024, `expected 60KB, got ${r.value?.length}`);
await kv.delete(key);
});
await test("value over 65536 bytes is rejected", async () => {
const key = ["large_reject", crypto.randomUUID()];
const tooLarge = "x".repeat(65537);
try {
await kv.set(key, tooLarge);
assert(false, "should have thrown for oversized value");
} catch (_e) {
// Expected
}
});
// --- MIXED KEY TYPES IN RANGE QUERIES ---
console.log("\n[range queries with mixed key types]");
await test("list with start/end range", async () => {
const prefix = crypto.randomUUID();
for (let i = 0; i < 10; i++) {
await kv.set(["range", prefix, `item_${String(i).padStart(2, "0")}`], i);
}
const entries = [];
for await (const entry of kv.list({
start: ["range", prefix, "item_03"],
end: ["range", prefix, "item_07"],
})) {
entries.push(entry);
}
assert(entries.length === 4, `expected 4 entries in range, got ${entries.length}`);
assert(entries[0].value === 3, `first should be 3`);
assert(entries[3].value === 6, `last should be 6`);
// cleanup
for await (const entry of kv.list({ prefix: ["range", prefix] })) {
await kv.delete(entry.key);
}
});
// --- SUMMARY ---
kv.close();
console.log("\n" + "=".repeat(40));
console.log(`Results: ${passed} passed, ${failed} failed, ${passed + failed} total`);
if (failed > 0) {
Deno.exit(1);
} else {
console.log("All tests passed!");
}
}
run();