2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-22 00:13:24 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
AssertionError,
|
2023-06-13 20:49:57 -04:00
|
|
|
assertNotEquals,
|
2023-03-22 00:13:24 -04:00
|
|
|
assertRejects,
|
|
|
|
assertThrows,
|
|
|
|
} from "./test_util.ts";
|
2024-02-12 21:05:10 -05:00
|
|
|
import { assertType, IsExact } from "@std/testing/types.ts";
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-06-13 20:49:57 -04:00
|
|
|
const sleep = (time: number) => new Promise((r) => setTimeout(r, time));
|
|
|
|
|
2023-03-22 15:46:26 -04:00
|
|
|
let isCI: boolean;
|
|
|
|
try {
|
|
|
|
isCI = Deno.env.get("CI") !== undefined;
|
|
|
|
} catch {
|
|
|
|
isCI = true;
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:56:00 -04:00
|
|
|
// Defined in test_util/src/lib.rs
|
|
|
|
Deno.env.set("DENO_KV_ACCESS_TOKEN", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
|
|
|
|
|
2023-03-22 06:49:29 -04:00
|
|
|
Deno.test({
|
2023-03-22 15:23:36 -04:00
|
|
|
name: "openKv :memory: no permissions",
|
2023-03-22 06:49:29 -04:00
|
|
|
permissions: {},
|
|
|
|
async fn() {
|
2023-03-22 15:23:36 -04:00
|
|
|
const db = await Deno.openKv(":memory:");
|
2023-03-22 06:49:29 -04:00
|
|
|
await db.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-03-22 15:23:36 -04:00
|
|
|
name: "openKv invalid filenames",
|
2023-03-22 06:49:29 -04:00
|
|
|
permissions: {},
|
|
|
|
async fn() {
|
|
|
|
await assertRejects(
|
2023-03-22 15:23:36 -04:00
|
|
|
async () => await Deno.openKv(""),
|
2023-03-22 06:49:29 -04:00
|
|
|
TypeError,
|
|
|
|
"Filename cannot be empty",
|
|
|
|
);
|
|
|
|
await assertRejects(
|
2023-03-22 15:23:36 -04:00
|
|
|
async () => await Deno.openKv(":foo"),
|
2023-03-22 06:49:29 -04:00
|
|
|
TypeError,
|
|
|
|
"Filename cannot start with ':' unless prefixed with './'",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-07-01 03:24:15 -04:00
|
|
|
function dbTest(name: string, fn: (db: Deno.Kv) => Promise<void> | void) {
|
2023-03-22 00:13:24 -04:00
|
|
|
Deno.test({
|
|
|
|
name,
|
2023-03-22 15:46:26 -04:00
|
|
|
// https://github.com/denoland/deno/issues/18363
|
|
|
|
ignore: Deno.build.os === "darwin" && isCI,
|
2023-03-22 00:13:24 -04:00
|
|
|
async fn() {
|
2023-10-31 07:13:57 -04:00
|
|
|
const db: Deno.Kv = await Deno.openKv(":memory:");
|
2023-03-22 00:13:24 -04:00
|
|
|
try {
|
|
|
|
await fn(db);
|
|
|
|
} finally {
|
2023-07-01 03:24:15 -04:00
|
|
|
db.close();
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-13 20:49:57 -04:00
|
|
|
function queueTest(name: string, fn: (db: Deno.Kv) => Promise<void>) {
|
|
|
|
Deno.test({
|
|
|
|
name,
|
|
|
|
// https://github.com/denoland/deno/issues/18363
|
|
|
|
ignore: Deno.build.os === "darwin" && isCI,
|
|
|
|
async fn() {
|
2023-10-31 07:13:57 -04:00
|
|
|
const db: Deno.Kv = await Deno.openKv(":memory:");
|
2023-06-13 20:49:57 -04:00
|
|
|
await fn(db);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
const ZERO_VERSIONSTAMP = "00000000000000000000";
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
dbTest("basic read-write-delete and versionstamps", async (db) => {
|
|
|
|
const result1 = await db.get(["a"]);
|
|
|
|
assertEquals(result1.key, ["a"]);
|
|
|
|
assertEquals(result1.value, null);
|
|
|
|
assertEquals(result1.versionstamp, null);
|
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
const setRes = await db.set(["a"], "b");
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(setRes.ok);
|
2023-10-31 07:13:57 -04:00
|
|
|
assert(setRes.versionstamp > ZERO_VERSIONSTAMP);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result2 = await db.get(["a"]);
|
|
|
|
assertEquals(result2.key, ["a"]);
|
|
|
|
assertEquals(result2.value, "b");
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(result2.versionstamp, setRes.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
const setRes2 = await db.set(["a"], "c");
|
|
|
|
assert(setRes2.ok);
|
|
|
|
assert(setRes2.versionstamp > setRes.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result3 = await db.get(["a"]);
|
|
|
|
assertEquals(result3.key, ["a"]);
|
|
|
|
assertEquals(result3.value, "c");
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(result3.versionstamp, setRes2.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
await db.delete(["a"]);
|
|
|
|
const result4 = await db.get(["a"]);
|
|
|
|
assertEquals(result4.key, ["a"]);
|
|
|
|
assertEquals(result4.value, null);
|
|
|
|
assertEquals(result4.versionstamp, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
const VALUE_CASES = [
|
|
|
|
{ name: "string", value: "hello" },
|
|
|
|
{ name: "number", value: 42 },
|
|
|
|
{ name: "bigint", value: 42n },
|
|
|
|
{ name: "boolean", value: true },
|
|
|
|
{ name: "null", value: null },
|
|
|
|
{ name: "undefined", value: undefined },
|
|
|
|
{ name: "Date", value: new Date(0) },
|
|
|
|
{ name: "Uint8Array", value: new Uint8Array([1, 2, 3]) },
|
|
|
|
{ name: "ArrayBuffer", value: new ArrayBuffer(3) },
|
|
|
|
{ name: "array", value: [1, 2, 3] },
|
|
|
|
{ name: "object", value: { a: 1, b: 2 } },
|
|
|
|
{ name: "nested array", value: [[1, 2], [3, 4]] },
|
|
|
|
{ name: "nested object", value: { a: { b: 1 } } },
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const { name, value } of VALUE_CASES) {
|
|
|
|
dbTest(`set and get ${name} value`, async (db) => {
|
|
|
|
await db.set(["a"], value);
|
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.key, ["a"]);
|
|
|
|
assertEquals(result.value, value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dbTest("set and get recursive object", async (db) => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const value: any = { a: undefined };
|
|
|
|
value.a = value;
|
|
|
|
await db.set(["a"], value);
|
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.key, ["a"]);
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const resultValue: any = result.value;
|
|
|
|
assert(resultValue.a === resultValue);
|
|
|
|
});
|
|
|
|
|
2023-04-29 11:43:07 -04:00
|
|
|
// invalid values (as per structured clone algorithm with _for storage_, NOT JSON)
|
|
|
|
const INVALID_VALUE_CASES = [
|
|
|
|
{ name: "function", value: () => {} },
|
|
|
|
{ name: "symbol", value: Symbol() },
|
|
|
|
{ name: "WeakMap", value: new WeakMap() },
|
|
|
|
{ name: "WeakSet", value: new WeakSet() },
|
|
|
|
{
|
|
|
|
name: "WebAssembly.Module",
|
|
|
|
value: new WebAssembly.Module(
|
|
|
|
new Uint8Array([0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00]),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SharedArrayBuffer",
|
|
|
|
value: new SharedArrayBuffer(3),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const { name, value } of INVALID_VALUE_CASES) {
|
|
|
|
dbTest(`set and get ${name} value (invalid)`, async (db) => {
|
|
|
|
await assertRejects(
|
|
|
|
async () => await db.set(["a"], value),
|
|
|
|
Error,
|
|
|
|
);
|
|
|
|
const res = await db.get(["a"]);
|
|
|
|
assertEquals(res.key, ["a"]);
|
|
|
|
assertEquals(res.value, null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
const keys = [
|
|
|
|
["a"],
|
|
|
|
["a", "b"],
|
|
|
|
["a", "b", "c"],
|
|
|
|
[1],
|
|
|
|
["a", 1],
|
|
|
|
["a", 1, "b"],
|
|
|
|
[1n],
|
|
|
|
["a", 1n],
|
|
|
|
["a", 1n, "b"],
|
|
|
|
[true],
|
|
|
|
["a", true],
|
|
|
|
["a", true, "b"],
|
|
|
|
[new Uint8Array([1, 2, 3])],
|
|
|
|
["a", new Uint8Array([1, 2, 3])],
|
|
|
|
["a", new Uint8Array([1, 2, 3]), "b"],
|
|
|
|
[1, 1n, true, new Uint8Array([1, 2, 3]), "a"],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
dbTest(`set and get ${Deno.inspect(key)} key`, async (db) => {
|
|
|
|
await db.set(key, "b");
|
|
|
|
const result = await db.get(key);
|
|
|
|
assertEquals(result.key, key);
|
|
|
|
assertEquals(result.value, "b");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const INVALID_KEYS = [
|
|
|
|
[null],
|
|
|
|
[undefined],
|
|
|
|
[],
|
|
|
|
[{}],
|
|
|
|
[new Date()],
|
|
|
|
[new ArrayBuffer(3)],
|
|
|
|
[new Uint8Array([1, 2, 3]).buffer],
|
|
|
|
[["a", "b"]],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const key of INVALID_KEYS) {
|
|
|
|
dbTest(`set and get invalid key ${Deno.inspect(key)}`, async (db) => {
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
// @ts-ignore - we are testing invalid keys
|
|
|
|
await db.set(key, "b");
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dbTest("compare and mutate", async (db) => {
|
|
|
|
await db.set(["t"], "1");
|
|
|
|
|
|
|
|
const currentValue = await db.get(["t"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assert(currentValue.versionstamp);
|
|
|
|
assert(currentValue.versionstamp > ZERO_VERSIONSTAMP);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
let res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.check({ key: ["t"], versionstamp: currentValue.versionstamp })
|
|
|
|
.set(currentValue.key, "2")
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-10-31 07:13:57 -04:00
|
|
|
assert(res.versionstamp > currentValue.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const newValue = await db.get(["t"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(newValue.versionstamp, res.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
assertEquals(newValue.value, "2");
|
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.check({ key: ["t"], versionstamp: currentValue.versionstamp })
|
|
|
|
.set(currentValue.key, "3")
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(!res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const newValue2 = await db.get(["t"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(newValue2.versionstamp, newValue.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
assertEquals(newValue2.value, "2");
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("compare and mutate not exists", async (db) => {
|
2023-03-30 14:57:21 -04:00
|
|
|
let res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.check({ key: ["t"], versionstamp: null })
|
|
|
|
.set(["t"], "1")
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-10-31 07:13:57 -04:00
|
|
|
assert(res.versionstamp > ZERO_VERSIONSTAMP);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const newValue = await db.get(["t"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(newValue.versionstamp, res.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
assertEquals(newValue.value, "1");
|
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.check({ key: ["t"], versionstamp: null })
|
|
|
|
.set(["t"], "2")
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(!res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
});
|
|
|
|
|
2023-04-26 13:14:01 -04:00
|
|
|
dbTest("atomic mutation helper (sum)", async (db) => {
|
|
|
|
await db.set(["t"], new Deno.KvU64(42n));
|
|
|
|
assertEquals((await db.get(["t"])).value, new Deno.KvU64(42n));
|
|
|
|
|
|
|
|
await db.atomic().sum(["t"], 1n).commit();
|
|
|
|
assertEquals((await db.get(["t"])).value, new Deno.KvU64(43n));
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation helper (min)", async (db) => {
|
|
|
|
await db.set(["t"], new Deno.KvU64(42n));
|
|
|
|
assertEquals((await db.get(["t"])).value, new Deno.KvU64(42n));
|
|
|
|
|
|
|
|
await db.atomic().min(["t"], 1n).commit();
|
|
|
|
assertEquals((await db.get(["t"])).value, new Deno.KvU64(1n));
|
|
|
|
|
|
|
|
await db.atomic().min(["t"], 2n).commit();
|
|
|
|
assertEquals((await db.get(["t"])).value, new Deno.KvU64(1n));
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation helper (max)", async (db) => {
|
|
|
|
await db.set(["t"], new Deno.KvU64(42n));
|
|
|
|
assertEquals((await db.get(["t"])).value, new Deno.KvU64(42n));
|
|
|
|
|
|
|
|
await db.atomic().max(["t"], 41n).commit();
|
|
|
|
assertEquals((await db.get(["t"])).value, new Deno.KvU64(42n));
|
|
|
|
|
|
|
|
await db.atomic().max(["t"], 43n).commit();
|
|
|
|
assertEquals((await db.get(["t"])).value, new Deno.KvU64(43n));
|
|
|
|
});
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
dbTest("compare multiple and mutate", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const setRes1 = await db.set(["t1"], "1");
|
|
|
|
const setRes2 = await db.set(["t2"], "2");
|
|
|
|
assert(setRes1.ok);
|
|
|
|
assert(setRes1.versionstamp > ZERO_VERSIONSTAMP);
|
|
|
|
assert(setRes2.ok);
|
|
|
|
assert(setRes2.versionstamp > ZERO_VERSIONSTAMP);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const currentValue1 = await db.get(["t1"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(currentValue1.versionstamp, setRes1.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
const currentValue2 = await db.get(["t2"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(currentValue2.versionstamp, setRes2.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.check({ key: ["t1"], versionstamp: currentValue1.versionstamp })
|
|
|
|
.check({ key: ["t2"], versionstamp: currentValue2.versionstamp })
|
|
|
|
.set(currentValue1.key, "3")
|
|
|
|
.set(currentValue2.key, "4")
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-10-31 07:13:57 -04:00
|
|
|
assert(res.versionstamp > setRes2.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const newValue1 = await db.get(["t1"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(newValue1.versionstamp, res.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
assertEquals(newValue1.value, "3");
|
|
|
|
const newValue2 = await db.get(["t2"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(newValue2.versionstamp, res.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
assertEquals(newValue2.value, "4");
|
|
|
|
|
|
|
|
// just one of the two checks failed
|
2023-03-30 14:57:21 -04:00
|
|
|
const res2 = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.check({ key: ["t1"], versionstamp: newValue1.versionstamp })
|
|
|
|
.check({ key: ["t2"], versionstamp: null })
|
|
|
|
.set(newValue1.key, "5")
|
|
|
|
.set(newValue2.key, "6")
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(!res2.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const newValue3 = await db.get(["t1"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(newValue3.versionstamp, res.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
assertEquals(newValue3.value, "3");
|
|
|
|
const newValue4 = await db.get(["t2"]);
|
2023-10-31 07:13:57 -04:00
|
|
|
assertEquals(newValue4.versionstamp, res.versionstamp);
|
2023-03-22 00:13:24 -04:00
|
|
|
assertEquals(newValue4.value, "4");
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation ordering (set before delete)", async (db) => {
|
|
|
|
await db.set(["a"], "1");
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.set(["a"], "2")
|
|
|
|
.delete(["a"])
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation ordering (delete before set)", async (db) => {
|
|
|
|
await db.set(["a"], "1");
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.delete(["a"])
|
|
|
|
.set(["a"], "2")
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, "2");
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=set", async (db) => {
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: "1", type: "set" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, "1");
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=set overwrite", async (db) => {
|
|
|
|
await db.set(["a"], "1");
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: "2", type: "set" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, "2");
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=delete", async (db) => {
|
|
|
|
await db.set(["a"], "1");
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], type: "delete" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=delete no exists", async (db) => {
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], type: "delete" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=sum", async (db) => {
|
|
|
|
await db.set(["a"], new Deno.KvU64(10n));
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "sum" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, new Deno.KvU64(11n));
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=sum no exists", async (db) => {
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "sum" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assert(result.value);
|
|
|
|
assertEquals(result.value, new Deno.KvU64(1n));
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=sum wrap around", async (db) => {
|
|
|
|
await db.set(["a"], new Deno.KvU64(0xffffffffffffffffn));
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(10n), type: "sum" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, new Deno.KvU64(9n));
|
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
const res2 = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({
|
|
|
|
key: ["a"],
|
|
|
|
value: new Deno.KvU64(0xffffffffffffffffn),
|
|
|
|
type: "sum",
|
|
|
|
})
|
|
|
|
.commit();
|
2023-03-30 14:57:21 -04:00
|
|
|
assert(res2);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result2 = await db.get(["a"]);
|
|
|
|
assertEquals(result2.value, new Deno.KvU64(8n));
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=sum wrong type in db", async (db) => {
|
|
|
|
await db.set(["a"], 1);
|
2023-06-27 04:41:29 -04:00
|
|
|
await assertRejects(
|
2023-03-22 00:13:24 -04:00
|
|
|
async () => {
|
|
|
|
await db.atomic()
|
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "sum" })
|
|
|
|
.commit();
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Failed to perform 'sum' mutation on a non-U64 value in the database",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=sum wrong type in mutation", async (db) => {
|
|
|
|
await db.set(["a"], new Deno.KvU64(1n));
|
2023-06-27 04:41:29 -04:00
|
|
|
await assertRejects(
|
2023-03-22 00:13:24 -04:00
|
|
|
async () => {
|
|
|
|
await db.atomic()
|
|
|
|
// @ts-expect-error wrong type is intentional
|
|
|
|
.mutate({ key: ["a"], value: 1, type: "sum" })
|
|
|
|
.commit();
|
|
|
|
},
|
|
|
|
TypeError,
|
2024-05-23 10:35:16 -04:00
|
|
|
"Cannot sum KvU64 with Number",
|
2023-03-22 00:13:24 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=min", async (db) => {
|
|
|
|
await db.set(["a"], new Deno.KvU64(10n));
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(5n), type: "min" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, new Deno.KvU64(5n));
|
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
const res2 = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(15n), type: "min" })
|
|
|
|
.commit();
|
2023-03-30 14:57:21 -04:00
|
|
|
assert(res2);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result2 = await db.get(["a"]);
|
|
|
|
assertEquals(result2.value, new Deno.KvU64(5n));
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=min no exists", async (db) => {
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "min" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assert(result.value);
|
|
|
|
assertEquals(result.value, new Deno.KvU64(1n));
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=min wrong type in db", async (db) => {
|
|
|
|
await db.set(["a"], 1);
|
2023-06-27 04:41:29 -04:00
|
|
|
await assertRejects(
|
2023-03-22 00:13:24 -04:00
|
|
|
async () => {
|
|
|
|
await db.atomic()
|
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "min" })
|
|
|
|
.commit();
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Failed to perform 'min' mutation on a non-U64 value in the database",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=min wrong type in mutation", async (db) => {
|
|
|
|
await db.set(["a"], new Deno.KvU64(1n));
|
2023-06-27 04:41:29 -04:00
|
|
|
await assertRejects(
|
2023-03-22 00:13:24 -04:00
|
|
|
async () => {
|
|
|
|
await db.atomic()
|
|
|
|
// @ts-expect-error wrong type is intentional
|
|
|
|
.mutate({ key: ["a"], value: 1, type: "min" })
|
|
|
|
.commit();
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Failed to perform 'min' mutation on a non-U64 operand",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=max", async (db) => {
|
|
|
|
await db.set(["a"], new Deno.KvU64(10n));
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(5n), type: "max" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assertEquals(result.value, new Deno.KvU64(10n));
|
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
const res2 = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(15n), type: "max" })
|
|
|
|
.commit();
|
2023-03-30 14:57:21 -04:00
|
|
|
assert(res2);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result2 = await db.get(["a"]);
|
|
|
|
assertEquals(result2.value, new Deno.KvU64(15n));
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=max no exists", async (db) => {
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "max" })
|
|
|
|
.commit();
|
2023-04-27 10:59:02 -04:00
|
|
|
assert(res.ok);
|
2023-03-22 00:13:24 -04:00
|
|
|
const result = await db.get(["a"]);
|
|
|
|
assert(result.value);
|
|
|
|
assertEquals(result.value, new Deno.KvU64(1n));
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=max wrong type in db", async (db) => {
|
|
|
|
await db.set(["a"], 1);
|
2023-06-27 04:41:29 -04:00
|
|
|
await assertRejects(
|
2023-03-22 00:13:24 -04:00
|
|
|
async () => {
|
|
|
|
await db.atomic()
|
|
|
|
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "max" })
|
|
|
|
.commit();
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Failed to perform 'max' mutation on a non-U64 value in the database",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("atomic mutation type=max wrong type in mutation", async (db) => {
|
|
|
|
await db.set(["a"], new Deno.KvU64(1n));
|
2023-06-27 04:41:29 -04:00
|
|
|
await assertRejects(
|
2023-03-22 00:13:24 -04:00
|
|
|
async () => {
|
|
|
|
await db.atomic()
|
|
|
|
// @ts-expect-error wrong type is intentional
|
|
|
|
.mutate({ key: ["a"], value: 1, type: "max" })
|
|
|
|
.commit();
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Failed to perform 'max' mutation on a non-U64 operand",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("KvU64 comparison", () => {
|
|
|
|
const a = new Deno.KvU64(1n);
|
|
|
|
const b = new Deno.KvU64(1n);
|
|
|
|
assertEquals(a, b);
|
|
|
|
assertThrows(() => {
|
|
|
|
assertEquals(a, new Deno.KvU64(2n));
|
|
|
|
}, AssertionError);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("KvU64 overflow", () => {
|
|
|
|
assertThrows(() => {
|
|
|
|
new Deno.KvU64(2n ** 64n);
|
|
|
|
}, RangeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("KvU64 underflow", () => {
|
|
|
|
assertThrows(() => {
|
|
|
|
new Deno.KvU64(-1n);
|
|
|
|
}, RangeError);
|
|
|
|
});
|
|
|
|
|
2023-05-03 07:44:00 -04:00
|
|
|
Deno.test("KvU64 unbox", () => {
|
2023-03-22 00:13:24 -04:00
|
|
|
const a = new Deno.KvU64(1n);
|
2023-05-03 07:44:00 -04:00
|
|
|
assertEquals(a.value, 1n);
|
2023-03-22 00:13:24 -04:00
|
|
|
});
|
|
|
|
|
2023-05-03 07:44:00 -04:00
|
|
|
Deno.test("KvU64 unbox with valueOf", () => {
|
2023-03-22 00:13:24 -04:00
|
|
|
const a = new Deno.KvU64(1n);
|
2023-05-03 07:44:00 -04:00
|
|
|
assertEquals(a.valueOf(), 1n);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("KvU64 auto-unbox", () => {
|
|
|
|
const a = new Deno.KvU64(1n);
|
|
|
|
assertEquals(a as unknown as bigint + 1n, 2n);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("KvU64 toString", () => {
|
|
|
|
const a = new Deno.KvU64(1n);
|
|
|
|
assertEquals(a.toString(), "1");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("KvU64 inspect", () => {
|
|
|
|
const a = new Deno.KvU64(1n);
|
|
|
|
assertEquals(Deno.inspect(a), "[Deno.KvU64: 1n]");
|
2023-03-22 00:13:24 -04:00
|
|
|
});
|
|
|
|
|
2023-03-30 16:52:31 -04:00
|
|
|
async function collect<T>(
|
|
|
|
iter: Deno.KvListIterator<T>,
|
|
|
|
): Promise<Deno.KvEntry<T>[]> {
|
|
|
|
const entries: Deno.KvEntry<T>[] = [];
|
2023-03-22 00:13:24 -04:00
|
|
|
for await (const entry of iter) {
|
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
async function setupData(db: Deno.Kv): Promise<string> {
|
|
|
|
const res = await db.atomic()
|
2023-03-22 00:13:24 -04:00
|
|
|
.set(["a"], -1)
|
|
|
|
.set(["a", "a"], 0)
|
|
|
|
.set(["a", "b"], 1)
|
|
|
|
.set(["a", "c"], 2)
|
|
|
|
.set(["a", "d"], 3)
|
|
|
|
.set(["a", "e"], 4)
|
|
|
|
.set(["b"], 99)
|
|
|
|
.set(["b", "a"], 100)
|
|
|
|
.commit();
|
2023-10-31 07:13:57 -04:00
|
|
|
assert(res.ok);
|
|
|
|
return res.versionstamp;
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
2023-03-24 08:06:27 -04:00
|
|
|
dbTest("get many", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-24 08:06:27 -04:00
|
|
|
const entries = await db.getMany([["b", "a"], ["a"], ["c"]]);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["b", "a"], value: 100, versionstamp },
|
|
|
|
{ key: ["a"], value: -1, versionstamp },
|
2023-03-24 08:06:27 -04:00
|
|
|
{ key: ["c"], value: null, versionstamp: null },
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
dbTest("list prefix", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(db.list({ prefix: ["a"] }));
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix empty", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
const entries = await collect(db.list({ prefix: ["c"] }));
|
|
|
|
assertEquals(entries.length, 0);
|
|
|
|
|
|
|
|
const entries2 = await collect(db.list({ prefix: ["a", "f"] }));
|
|
|
|
assertEquals(entries2.length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with start", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(db.list({ prefix: ["a"], start: ["a", "c"] }));
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with start empty", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
const entries = await collect(db.list({ prefix: ["a"], start: ["a", "f"] }));
|
|
|
|
assertEquals(entries.length, 0);
|
|
|
|
});
|
|
|
|
|
2024-02-05 11:27:03 -05:00
|
|
|
dbTest("list prefix with start equal to prefix", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
await assertRejects(
|
|
|
|
async () => await collect(db.list({ prefix: ["a"], start: ["a"] })),
|
|
|
|
TypeError,
|
|
|
|
"start key is not in the keyspace defined by prefix",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with start out of bounds", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
await assertRejects(
|
|
|
|
async () => await collect(db.list({ prefix: ["b"], start: ["a"] })),
|
|
|
|
TypeError,
|
|
|
|
"start key is not in the keyspace defined by prefix",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
dbTest("list prefix with end", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(db.list({ prefix: ["a"], end: ["a", "c"] }));
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with end empty", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
const entries = await collect(db.list({ prefix: ["a"], end: ["a", "a"] }));
|
|
|
|
assertEquals(entries.length, 0);
|
|
|
|
});
|
|
|
|
|
2024-02-05 11:27:03 -05:00
|
|
|
dbTest("list prefix with end equal to prefix", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
await assertRejects(
|
|
|
|
async () => await collect(db.list({ prefix: ["a"], end: ["a"] })),
|
|
|
|
TypeError,
|
|
|
|
"end key is not in the keyspace defined by prefix",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with end out of bounds", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
await assertRejects(
|
|
|
|
async () => await collect(db.list({ prefix: ["a"], end: ["b"] })),
|
|
|
|
TypeError,
|
|
|
|
"end key is not in the keyspace defined by prefix",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-03-22 16:53:16 -04:00
|
|
|
dbTest("list prefix with empty prefix", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const res = await db.set(["a"], 1);
|
2023-03-22 16:53:16 -04:00
|
|
|
const entries = await collect(db.list({ prefix: [] }));
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a"], value: 1, versionstamp: res.versionstamp },
|
2023-03-22 16:53:16 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
dbTest("list prefix reverse", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(db.list({ prefix: ["a"] }, { reverse: true }));
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix reverse with start", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(
|
|
|
|
db.list({ prefix: ["a"], start: ["a", "c"] }, { reverse: true }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix reverse with start empty", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
const entries = await collect(
|
|
|
|
db.list({ prefix: ["a"], start: ["a", "f"] }, { reverse: true }),
|
|
|
|
);
|
|
|
|
assertEquals(entries.length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix reverse with end", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(
|
|
|
|
db.list({ prefix: ["a"], end: ["a", "c"] }, { reverse: true }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix reverse with end empty", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
const entries = await collect(
|
|
|
|
db.list({ prefix: ["a"], end: ["a", "a"] }, { reverse: true }),
|
|
|
|
);
|
|
|
|
assertEquals(entries.length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix limit", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(db.list({ prefix: ["a"] }, { limit: 2 }));
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix limit reverse", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(
|
|
|
|
db.list({ prefix: ["a"] }, { limit: 2, reverse: true }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with small batch size", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(db.list({ prefix: ["a"] }, { batchSize: 2 }));
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with small batch size reverse", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(
|
|
|
|
db.list({ prefix: ["a"] }, { batchSize: 2, reverse: true }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with small batch size and limit", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(
|
|
|
|
db.list({ prefix: ["a"] }, { batchSize: 2, limit: 3 }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with small batch size and limit reverse", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const entries = await collect(
|
|
|
|
db.list({ prefix: ["a"] }, { batchSize: 2, limit: 3, reverse: true }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with manual cursor", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
const iterator = db.list({ prefix: ["a"] }, { limit: 2 });
|
|
|
|
const values = await collect(iterator);
|
|
|
|
assertEquals(values, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
const cursor = iterator.cursor;
|
|
|
|
assertEquals(cursor, "AmIA");
|
|
|
|
|
|
|
|
const iterator2 = db.list({ prefix: ["a"] }, { cursor });
|
|
|
|
const values2 = await collect(iterator2);
|
|
|
|
assertEquals(values2, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list prefix with manual cursor reverse", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const iterator = db.list({ prefix: ["a"] }, { limit: 2, reverse: true });
|
|
|
|
const values = await collect(iterator);
|
|
|
|
assertEquals(values, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
const cursor = iterator.cursor;
|
|
|
|
assertEquals(cursor, "AmQA");
|
|
|
|
|
|
|
|
const iterator2 = db.list({ prefix: ["a"] }, { cursor, reverse: true });
|
|
|
|
const values2 = await collect(iterator2);
|
|
|
|
assertEquals(values2, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list range", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const entries = await collect(
|
|
|
|
db.list({ start: ["a", "a"], end: ["a", "z"] }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list range reverse", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const entries = await collect(
|
|
|
|
db.list({ start: ["a", "a"], end: ["a", "z"] }, { reverse: true }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list range with limit", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const entries = await collect(
|
|
|
|
db.list({ start: ["a", "a"], end: ["a", "z"] }, { limit: 3 }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list range with limit reverse", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const entries = await collect(
|
|
|
|
db.list({ start: ["a", "a"], end: ["a", "z"] }, {
|
|
|
|
limit: 3,
|
|
|
|
reverse: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list range nesting", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const entries = await collect(db.list({ start: ["a"], end: ["a", "d"] }));
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a"], value: -1, versionstamp },
|
|
|
|
{ key: ["a", "a"], value: 0, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list range short", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const entries = await collect(
|
|
|
|
db.list({ start: ["a", "b"], end: ["a", "d"] }),
|
|
|
|
);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list range with manual cursor", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const iterator = db.list({ start: ["a", "b"], end: ["a", "z"] }, {
|
|
|
|
limit: 2,
|
|
|
|
});
|
|
|
|
const entries = await collect(iterator);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
const cursor = iterator.cursor;
|
|
|
|
const iterator2 = db.list({ start: ["a", "b"], end: ["a", "z"] }, {
|
|
|
|
cursor,
|
|
|
|
});
|
|
|
|
const entries2 = await collect(iterator2);
|
|
|
|
assertEquals(entries2, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list range with manual cursor reverse", async (db) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
const versionstamp = await setupData(db);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const iterator = db.list({ start: ["a", "b"], end: ["a", "z"] }, {
|
|
|
|
limit: 2,
|
|
|
|
reverse: true,
|
|
|
|
});
|
|
|
|
const entries = await collect(iterator);
|
|
|
|
assertEquals(entries, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "e"], value: 4, versionstamp },
|
|
|
|
{ key: ["a", "d"], value: 3, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
const cursor = iterator.cursor;
|
|
|
|
const iterator2 = db.list({ start: ["a", "b"], end: ["a", "z"] }, {
|
|
|
|
cursor,
|
|
|
|
reverse: true,
|
|
|
|
});
|
|
|
|
const entries2 = await collect(iterator2);
|
|
|
|
assertEquals(entries2, [
|
2023-10-31 07:13:57 -04:00
|
|
|
{ key: ["a", "c"], value: 2, versionstamp },
|
|
|
|
{ key: ["a", "b"], value: 1, versionstamp },
|
2023-03-22 00:13:24 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2024-02-05 11:27:03 -05:00
|
|
|
dbTest("list range with start greater than end", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
await assertRejects(
|
|
|
|
async () => await collect(db.list({ start: ["b"], end: ["a"] })),
|
|
|
|
TypeError,
|
|
|
|
"start key is greater than end key",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("list range with start equal to end", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
const entries = await collect(db.list({ start: ["a"], end: ["a"] }));
|
|
|
|
assertEquals(entries.length, 0);
|
|
|
|
});
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
dbTest("list invalid selector", async (db) => {
|
|
|
|
await setupData(db);
|
|
|
|
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await collect(
|
|
|
|
db.list({ prefix: ["a"], start: ["a", "b"], end: ["a", "c"] }),
|
|
|
|
);
|
|
|
|
}, TypeError);
|
|
|
|
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await collect(
|
|
|
|
// @ts-expect-error missing end
|
|
|
|
db.list({ start: ["a", "b"] }),
|
|
|
|
);
|
|
|
|
}, TypeError);
|
|
|
|
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await collect(
|
|
|
|
// @ts-expect-error missing start
|
|
|
|
db.list({ end: ["a", "b"] }),
|
|
|
|
);
|
|
|
|
}, TypeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("invalid versionstamp in atomic check rejects", async (db) => {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await db.atomic().check({ key: ["a"], versionstamp: "" }).commit();
|
|
|
|
}, TypeError);
|
|
|
|
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await db.atomic().check({ key: ["a"], versionstamp: "xx".repeat(10) })
|
|
|
|
.commit();
|
|
|
|
}, TypeError);
|
|
|
|
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await db.atomic().check({ key: ["a"], versionstamp: "aa".repeat(11) })
|
|
|
|
.commit();
|
|
|
|
}, TypeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("invalid mutation type rejects", async (db) => {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await db.atomic()
|
|
|
|
// @ts-expect-error invalid type + value combo
|
|
|
|
.mutate({ key: ["a"], type: "set" })
|
|
|
|
.commit();
|
|
|
|
}, TypeError);
|
|
|
|
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await db.atomic()
|
|
|
|
// @ts-expect-error invalid type + value combo
|
|
|
|
.mutate({ key: ["a"], type: "delete", value: "123" })
|
|
|
|
.commit();
|
|
|
|
}, TypeError);
|
|
|
|
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await db.atomic()
|
|
|
|
// @ts-expect-error invalid type
|
|
|
|
.mutate({ key: ["a"], type: "foobar" })
|
|
|
|
.commit();
|
|
|
|
}, TypeError);
|
|
|
|
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await db.atomic()
|
|
|
|
// @ts-expect-error invalid type
|
|
|
|
.mutate({ key: ["a"], type: "foobar", value: "123" })
|
|
|
|
.commit();
|
|
|
|
}, TypeError);
|
|
|
|
});
|
2023-03-22 16:53:16 -04:00
|
|
|
|
|
|
|
dbTest("key ordering", async (db) => {
|
|
|
|
await db.atomic()
|
|
|
|
.set([new Uint8Array(0x1)], 0)
|
|
|
|
.set(["a"], 0)
|
|
|
|
.set([1n], 0)
|
|
|
|
.set([3.14], 0)
|
|
|
|
.set([false], 0)
|
|
|
|
.set([true], 0)
|
|
|
|
.commit();
|
|
|
|
|
|
|
|
assertEquals((await collect(db.list({ prefix: [] }))).map((x) => x.key), [
|
|
|
|
[new Uint8Array(0x1)],
|
|
|
|
["a"],
|
|
|
|
[1n],
|
|
|
|
[3.14],
|
|
|
|
[false],
|
|
|
|
[true],
|
|
|
|
]);
|
|
|
|
});
|
2023-03-25 03:29:36 -04:00
|
|
|
|
|
|
|
dbTest("key size limit", async (db) => {
|
|
|
|
// 1 byte prefix + 1 byte suffix + 2045 bytes key
|
|
|
|
const lastValidKey = new Uint8Array(2046).fill(1);
|
|
|
|
const firstInvalidKey = new Uint8Array(2047).fill(1);
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
const res = await db.set([lastValidKey], 1);
|
2023-03-25 03:29:36 -04:00
|
|
|
|
|
|
|
assertEquals(await db.get([lastValidKey]), {
|
|
|
|
key: [lastValidKey],
|
|
|
|
value: 1,
|
2023-10-31 07:13:57 -04:00
|
|
|
versionstamp: res.versionstamp,
|
2023-03-25 03:29:36 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => await db.set([firstInvalidKey], 1),
|
|
|
|
TypeError,
|
|
|
|
"key too large for write (max 2048 bytes)",
|
|
|
|
);
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => await db.get([firstInvalidKey]),
|
|
|
|
TypeError,
|
|
|
|
"key too large for read (max 2049 bytes)",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("value size limit", async (db) => {
|
|
|
|
const lastValidValue = new Uint8Array(65536);
|
|
|
|
const firstInvalidValue = new Uint8Array(65537);
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
const res = await db.set(["a"], lastValidValue);
|
2023-03-25 03:29:36 -04:00
|
|
|
assertEquals(await db.get(["a"]), {
|
|
|
|
key: ["a"],
|
|
|
|
value: lastValidValue,
|
2023-10-31 07:13:57 -04:00
|
|
|
versionstamp: res.versionstamp,
|
2023-03-25 03:29:36 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => await db.set(["b"], firstInvalidValue),
|
|
|
|
TypeError,
|
|
|
|
"value too large (max 65536 bytes)",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("operation size limit", async (db) => {
|
|
|
|
const lastValidKeys: Deno.KvKey[] = new Array(10).fill(0).map((
|
|
|
|
_,
|
|
|
|
i,
|
|
|
|
) => ["a", i]);
|
|
|
|
const firstInvalidKeys: Deno.KvKey[] = new Array(11).fill(0).map((
|
|
|
|
_,
|
|
|
|
i,
|
|
|
|
) => ["a", i]);
|
2023-11-02 14:57:11 -04:00
|
|
|
const invalidCheckKeys: Deno.KvKey[] = new Array(101).fill(0).map((
|
|
|
|
_,
|
|
|
|
i,
|
|
|
|
) => ["a", i]);
|
2023-03-25 03:29:36 -04:00
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
const res = await db.getMany(lastValidKeys);
|
|
|
|
assertEquals(res.length, 10);
|
2023-03-25 03:29:36 -04:00
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => await db.getMany(firstInvalidKeys),
|
|
|
|
TypeError,
|
|
|
|
"too many ranges (max 10)",
|
|
|
|
);
|
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
const res2 = await collect(db.list({ prefix: ["a"] }, { batchSize: 1000 }));
|
|
|
|
assertEquals(res2.length, 0);
|
2023-03-25 03:29:36 -04:00
|
|
|
|
2023-06-27 04:41:29 -04:00
|
|
|
await assertRejects(
|
2023-03-30 14:57:21 -04:00
|
|
|
async () => await collect(db.list({ prefix: ["a"] }, { batchSize: 1001 })),
|
2023-03-25 03:29:36 -04:00
|
|
|
TypeError,
|
|
|
|
"too many entries (max 1000)",
|
|
|
|
);
|
|
|
|
|
|
|
|
// when batchSize is not specified, limit is used but is clamped to 500
|
|
|
|
assertEquals(
|
2023-03-30 14:57:21 -04:00
|
|
|
(await collect(db.list({ prefix: ["a"] }, { limit: 1001 }))).length,
|
2023-03-25 03:29:36 -04:00
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
const res3 = await db.atomic()
|
|
|
|
.check(...lastValidKeys.map((key) => ({
|
2023-03-25 03:29:36 -04:00
|
|
|
key,
|
|
|
|
versionstamp: null,
|
2023-03-30 14:57:21 -04:00
|
|
|
})))
|
|
|
|
.mutate(...lastValidKeys.map((key) => ({
|
2023-03-25 03:29:36 -04:00
|
|
|
key,
|
|
|
|
type: "set",
|
|
|
|
value: 1,
|
2023-03-30 14:57:21 -04:00
|
|
|
} satisfies Deno.KvMutation)))
|
|
|
|
.commit();
|
|
|
|
assert(res3);
|
2023-03-25 03:29:36 -04:00
|
|
|
|
|
|
|
await assertRejects(
|
2023-03-30 14:57:21 -04:00
|
|
|
async () => {
|
|
|
|
await db.atomic()
|
2023-11-02 14:57:11 -04:00
|
|
|
.check(...invalidCheckKeys.map((key) => ({
|
2023-03-30 14:57:21 -04:00
|
|
|
key,
|
|
|
|
versionstamp: null,
|
|
|
|
})))
|
|
|
|
.mutate(...lastValidKeys.map((key) => ({
|
|
|
|
key,
|
|
|
|
type: "set",
|
|
|
|
value: 1,
|
|
|
|
} satisfies Deno.KvMutation)))
|
|
|
|
.commit();
|
|
|
|
},
|
2023-03-25 03:29:36 -04:00
|
|
|
TypeError,
|
2023-11-02 14:57:11 -04:00
|
|
|
"too many checks (max 100)",
|
2023-03-25 03:29:36 -04:00
|
|
|
);
|
|
|
|
|
2023-08-26 21:26:09 -04:00
|
|
|
const validMutateKeys: Deno.KvKey[] = new Array(1000).fill(0).map((
|
|
|
|
_,
|
|
|
|
i,
|
|
|
|
) => ["a", i]);
|
|
|
|
const invalidMutateKeys: Deno.KvKey[] = new Array(1001).fill(0).map((
|
|
|
|
_,
|
|
|
|
i,
|
|
|
|
) => ["a", i]);
|
|
|
|
|
|
|
|
const res4 = await db.atomic()
|
|
|
|
.check(...lastValidKeys.map((key) => ({
|
|
|
|
key,
|
|
|
|
versionstamp: null,
|
|
|
|
})))
|
|
|
|
.mutate(...validMutateKeys.map((key) => ({
|
|
|
|
key,
|
|
|
|
type: "set",
|
|
|
|
value: 1,
|
|
|
|
} satisfies Deno.KvMutation)))
|
|
|
|
.commit();
|
|
|
|
assert(res4);
|
|
|
|
|
2023-03-25 03:29:36 -04:00
|
|
|
await assertRejects(
|
2023-03-30 14:57:21 -04:00
|
|
|
async () => {
|
|
|
|
await db.atomic()
|
|
|
|
.check(...lastValidKeys.map((key) => ({
|
|
|
|
key,
|
|
|
|
versionstamp: null,
|
|
|
|
})))
|
2023-08-26 21:26:09 -04:00
|
|
|
.mutate(...invalidMutateKeys.map((key) => ({
|
2023-03-30 14:57:21 -04:00
|
|
|
key,
|
|
|
|
type: "set",
|
|
|
|
value: 1,
|
|
|
|
} satisfies Deno.KvMutation)))
|
|
|
|
.commit();
|
|
|
|
},
|
2023-03-25 03:29:36 -04:00
|
|
|
TypeError,
|
2023-08-26 21:26:09 -04:00
|
|
|
"too many mutations (max 1000)",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("total mutation size limit", async (db) => {
|
|
|
|
const keys: Deno.KvKey[] = new Array(1000).fill(0).map((
|
|
|
|
_,
|
|
|
|
i,
|
|
|
|
) => ["a", i]);
|
|
|
|
|
|
|
|
const atomic = db.atomic();
|
|
|
|
for (const key of keys) {
|
|
|
|
atomic.set(key, "foo");
|
|
|
|
}
|
|
|
|
const res = await atomic.commit();
|
|
|
|
assert(res);
|
|
|
|
|
|
|
|
// Use bigger values to trigger "total mutation size too large" error
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
const value = new Array(3000).fill("a").join("");
|
|
|
|
const atomic = db.atomic();
|
|
|
|
for (const key of keys) {
|
|
|
|
atomic.set(key, value);
|
|
|
|
}
|
|
|
|
await atomic.commit();
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"total mutation size too large (max 819200 bytes)",
|
2023-03-25 03:29:36 -04:00
|
|
|
);
|
|
|
|
});
|
2023-03-30 16:52:31 -04:00
|
|
|
|
2023-09-07 03:07:04 -04:00
|
|
|
dbTest("total key size limit", async (db) => {
|
|
|
|
const longString = new Array(1100).fill("a").join("");
|
|
|
|
const keys: Deno.KvKey[] = new Array(80).fill(0).map(() => [longString]);
|
|
|
|
|
|
|
|
const atomic = db.atomic();
|
|
|
|
for (const key of keys) {
|
|
|
|
atomic.set(key, "foo");
|
|
|
|
}
|
|
|
|
await assertRejects(
|
|
|
|
() => atomic.commit(),
|
|
|
|
TypeError,
|
|
|
|
"total key size too large (max 81920 bytes)",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-04-11 10:36:01 -04:00
|
|
|
dbTest("keys must be arrays", async (db) => {
|
|
|
|
await assertRejects(
|
|
|
|
// @ts-expect-error invalid type
|
|
|
|
async () => await db.get("a"),
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
// @ts-expect-error invalid type
|
|
|
|
async () => await db.getMany(["a"]),
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
// @ts-expect-error invalid type
|
|
|
|
async () => await db.set("a", 1),
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
// @ts-expect-error invalid type
|
|
|
|
async () => await db.delete("a"),
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () =>
|
|
|
|
await db.atomic()
|
|
|
|
// @ts-expect-error invalid type
|
|
|
|
.mutate({ key: "a", type: "set", value: 1 } satisfies Deno.KvMutation)
|
|
|
|
.commit(),
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () =>
|
|
|
|
await db.atomic()
|
|
|
|
// @ts-expect-error invalid type
|
|
|
|
.check({ key: "a", versionstamp: null })
|
|
|
|
.set(["a"], 1)
|
|
|
|
.commit(),
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-05-03 17:08:42 -04:00
|
|
|
Deno.test("Deno.Kv constructor throws", () => {
|
|
|
|
assertThrows(() => {
|
|
|
|
new Deno.Kv();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-30 16:52:31 -04:00
|
|
|
// This function is never called, it is just used to check that all the types
|
|
|
|
// are behaving as expected.
|
|
|
|
async function _typeCheckingTests() {
|
|
|
|
const kv = new Deno.Kv();
|
|
|
|
|
|
|
|
const a = await kv.get(["a"]);
|
|
|
|
assertType<IsExact<typeof a, Deno.KvEntryMaybe<unknown>>>(true);
|
|
|
|
|
|
|
|
const b = await kv.get<string>(["b"]);
|
|
|
|
assertType<IsExact<typeof b, Deno.KvEntryMaybe<string>>>(true);
|
|
|
|
|
|
|
|
const c = await kv.getMany([["a"], ["b"]]);
|
|
|
|
assertType<
|
|
|
|
IsExact<typeof c, [Deno.KvEntryMaybe<unknown>, Deno.KvEntryMaybe<unknown>]>
|
|
|
|
>(true);
|
|
|
|
|
|
|
|
const d = await kv.getMany([["a"], ["b"]] as const);
|
|
|
|
assertType<
|
|
|
|
IsExact<typeof d, [Deno.KvEntryMaybe<unknown>, Deno.KvEntryMaybe<unknown>]>
|
|
|
|
>(true);
|
|
|
|
|
|
|
|
const e = await kv.getMany<[string, number]>([["a"], ["b"]]);
|
|
|
|
assertType<
|
|
|
|
IsExact<typeof e, [Deno.KvEntryMaybe<string>, Deno.KvEntryMaybe<number>]>
|
|
|
|
>(true);
|
|
|
|
|
|
|
|
const keys: Deno.KvKey[] = [["a"], ["b"]];
|
|
|
|
const f = await kv.getMany(keys);
|
|
|
|
assertType<IsExact<typeof f, Deno.KvEntryMaybe<unknown>[]>>(true);
|
|
|
|
|
|
|
|
const g = kv.list({ prefix: ["a"] });
|
|
|
|
assertType<IsExact<typeof g, Deno.KvListIterator<unknown>>>(true);
|
|
|
|
const h = await g.next();
|
|
|
|
assert(!h.done);
|
|
|
|
assertType<IsExact<typeof h.value, Deno.KvEntry<unknown>>>(true);
|
|
|
|
|
|
|
|
const i = kv.list<string>({ prefix: ["a"] });
|
|
|
|
assertType<IsExact<typeof i, Deno.KvListIterator<string>>>(true);
|
|
|
|
const j = await i.next();
|
|
|
|
assert(!j.done);
|
|
|
|
assertType<IsExact<typeof j.value, Deno.KvEntry<string>>>(true);
|
|
|
|
}
|
2023-06-13 20:49:57 -04:00
|
|
|
|
|
|
|
queueTest("basic listenQueue and enqueue", async (db) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
let dequeuedMessage: unknown = null;
|
|
|
|
const listener = db.listenQueue((msg) => {
|
|
|
|
dequeuedMessage = msg;
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
const res = await db.enqueue("test");
|
|
|
|
assert(res.ok);
|
|
|
|
assertNotEquals(res.versionstamp, null);
|
|
|
|
await promise;
|
|
|
|
assertEquals(dequeuedMessage, "test");
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const { name, value } of VALUE_CASES) {
|
|
|
|
queueTest(`listenQueue and enqueue ${name}`, async (db) => {
|
|
|
|
const numEnqueues = 10;
|
|
|
|
let count = 0;
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferreds: ReturnType<typeof Promise.withResolvers<unknown>>[] = [];
|
2023-06-13 20:49:57 -04:00
|
|
|
const listeners: Promise<void>[] = [];
|
2023-10-31 07:13:57 -04:00
|
|
|
listeners.push(db.listenQueue((msg: unknown) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferreds[count++].resolve(msg);
|
2023-06-13 20:49:57 -04:00
|
|
|
}));
|
|
|
|
try {
|
|
|
|
for (let i = 0; i < numEnqueues; i++) {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferreds.push(Promise.withResolvers<unknown>());
|
2023-06-13 20:49:57 -04:00
|
|
|
await db.enqueue(value);
|
|
|
|
}
|
2023-11-22 06:11:20 -05:00
|
|
|
const dequeuedMessages = await Promise.all(
|
|
|
|
deferreds.map(({ promise }) => promise),
|
|
|
|
);
|
2023-06-13 20:49:57 -04:00
|
|
|
for (let i = 0; i < numEnqueues; i++) {
|
|
|
|
assertEquals(dequeuedMessages[i], value);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
for (const listener of listeners) {
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
queueTest("queue mixed types", async (db) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
let deferred: ReturnType<typeof Promise.withResolvers<void>>;
|
2023-06-13 20:49:57 -04:00
|
|
|
let dequeuedMessage: unknown = null;
|
2023-10-31 07:13:57 -04:00
|
|
|
const listener = db.listenQueue((msg: unknown) => {
|
2023-06-13 20:49:57 -04:00
|
|
|
dequeuedMessage = msg;
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred.resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
for (const item of VALUE_CASES) {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
await db.enqueue(item.value);
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred.promise;
|
2023-06-13 20:49:57 -04:00
|
|
|
assertEquals(dequeuedMessage, item.value);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
queueTest("queue delay", async (db) => {
|
|
|
|
let dequeueTime: number | undefined;
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
let dequeuedMessage: unknown = null;
|
|
|
|
const listener = db.listenQueue((msg) => {
|
|
|
|
dequeueTime = Date.now();
|
|
|
|
dequeuedMessage = msg;
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
const enqueueTime = Date.now();
|
|
|
|
await db.enqueue("test", { delay: 1000 });
|
|
|
|
await promise;
|
|
|
|
assertEquals(dequeuedMessage, "test");
|
|
|
|
assert(dequeueTime !== undefined);
|
|
|
|
assert(dequeueTime - enqueueTime >= 1000);
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
queueTest("queue delay with atomic", async (db) => {
|
|
|
|
let dequeueTime: number | undefined;
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
let dequeuedMessage: unknown = null;
|
|
|
|
const listener = db.listenQueue((msg) => {
|
|
|
|
dequeueTime = Date.now();
|
|
|
|
dequeuedMessage = msg;
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
const enqueueTime = Date.now();
|
|
|
|
const res = await db.atomic()
|
|
|
|
.enqueue("test", { delay: 1000 })
|
|
|
|
.commit();
|
|
|
|
assert(res.ok);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
assertEquals(dequeuedMessage, "test");
|
|
|
|
assert(dequeueTime !== undefined);
|
|
|
|
assert(dequeueTime - enqueueTime >= 1000);
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
queueTest("queue delay and now", async (db) => {
|
|
|
|
let count = 0;
|
|
|
|
let dequeueTime: number | undefined;
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
let dequeuedMessage: unknown = null;
|
|
|
|
const listener = db.listenQueue((msg) => {
|
|
|
|
count += 1;
|
|
|
|
if (count == 2) {
|
|
|
|
dequeueTime = Date.now();
|
|
|
|
dequeuedMessage = msg;
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
const enqueueTime = Date.now();
|
|
|
|
await db.enqueue("test-1000", { delay: 1000 });
|
|
|
|
await db.enqueue("test");
|
|
|
|
await promise;
|
|
|
|
assertEquals(dequeuedMessage, "test-1000");
|
|
|
|
assert(dequeueTime !== undefined);
|
|
|
|
assert(dequeueTime - enqueueTime >= 1000);
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("queue negative delay", async (db) => {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await db.enqueue("test", { delay: -100 });
|
|
|
|
}, TypeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("queue nan delay", async (db) => {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await db.enqueue("test", { delay: Number.NaN });
|
|
|
|
}, TypeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
dbTest("queue large delay", async (db) => {
|
2023-09-22 12:40:35 -04:00
|
|
|
await db.enqueue("test", { delay: 30 * 24 * 60 * 60 * 1000 });
|
2023-06-13 20:49:57 -04:00
|
|
|
await assertRejects(async () => {
|
2023-09-22 12:40:35 -04:00
|
|
|
await db.enqueue("test", { delay: 30 * 24 * 60 * 60 * 1000 + 1 });
|
2023-06-13 20:49:57 -04:00
|
|
|
}, TypeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
queueTest("listenQueue with async callback", async (db) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
let dequeuedMessage: unknown = null;
|
|
|
|
const listener = db.listenQueue(async (msg) => {
|
|
|
|
dequeuedMessage = msg;
|
|
|
|
await sleep(100);
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
await db.enqueue("test");
|
|
|
|
await promise;
|
|
|
|
assertEquals(dequeuedMessage, "test");
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
queueTest("queue retries", async (db) => {
|
|
|
|
let count = 0;
|
|
|
|
const listener = db.listenQueue(async (_msg) => {
|
|
|
|
count += 1;
|
|
|
|
await sleep(10);
|
|
|
|
throw new TypeError("dequeue error");
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await db.enqueue("test");
|
|
|
|
await sleep(10000);
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should have been 1 attempt + 3 retries in the 10 seconds
|
|
|
|
assertEquals(4, count);
|
|
|
|
});
|
|
|
|
|
2023-12-13 01:51:23 -05:00
|
|
|
queueTest("queue retries with backoffSchedule", async (db) => {
|
|
|
|
let count = 0;
|
|
|
|
const listener = db.listenQueue((_msg) => {
|
|
|
|
count += 1;
|
|
|
|
throw new TypeError("dequeue error");
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await db.enqueue("test", { backoffSchedule: [1] });
|
|
|
|
await sleep(2000);
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should have been 1 attempt + 1 retry
|
|
|
|
assertEquals(2, count);
|
|
|
|
});
|
|
|
|
|
2023-06-13 20:49:57 -04:00
|
|
|
queueTest("multiple listenQueues", async (db) => {
|
|
|
|
const numListens = 10;
|
|
|
|
let count = 0;
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferreds: ReturnType<typeof Promise.withResolvers<void>>[] = [];
|
2023-06-13 20:49:57 -04:00
|
|
|
const dequeuedMessages: unknown[] = [];
|
|
|
|
const listeners: Promise<void>[] = [];
|
|
|
|
for (let i = 0; i < numListens; i++) {
|
|
|
|
listeners.push(db.listenQueue((msg) => {
|
|
|
|
dequeuedMessages.push(msg);
|
2023-11-22 06:11:20 -05:00
|
|
|
deferreds[count++].resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
for (let i = 0; i < numListens; i++) {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferreds.push(Promise.withResolvers<void>());
|
2023-06-13 20:49:57 -04:00
|
|
|
await db.enqueue("msg_" + i);
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferreds[i].promise;
|
2023-06-13 20:49:57 -04:00
|
|
|
const msg = dequeuedMessages[i];
|
|
|
|
assertEquals("msg_" + i, msg);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
for (let i = 0; i < numListens; i++) {
|
|
|
|
await listeners[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
queueTest("enqueue with atomic", async (db) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
let dequeuedMessage: unknown = null;
|
|
|
|
const listener = db.listenQueue((msg) => {
|
|
|
|
dequeuedMessage = msg;
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await db.set(["t"], "1");
|
|
|
|
|
|
|
|
let currentValue = await db.get(["t"]);
|
|
|
|
assertEquals("1", currentValue.value);
|
|
|
|
|
|
|
|
const res = await db.atomic()
|
|
|
|
.check(currentValue)
|
|
|
|
.set(currentValue.key, "2")
|
|
|
|
.enqueue("test")
|
|
|
|
.commit();
|
|
|
|
assert(res.ok);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
assertEquals("test", dequeuedMessage);
|
|
|
|
|
|
|
|
currentValue = await db.get(["t"]);
|
|
|
|
assertEquals("2", currentValue.value);
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
queueTest("enqueue with atomic nonce", async (db) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
let dequeuedMessage: unknown = null;
|
|
|
|
|
|
|
|
const nonce = crypto.randomUUID();
|
|
|
|
|
|
|
|
const listener = db.listenQueue(async (val) => {
|
|
|
|
const message = val as { msg: string; nonce: string };
|
|
|
|
const nonce = message.nonce;
|
|
|
|
const nonceValue = await db.get(["nonces", nonce]);
|
|
|
|
if (nonceValue.versionstamp === null) {
|
|
|
|
dequeuedMessage = message.msg;
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNotEquals(nonceValue.versionstamp, null);
|
|
|
|
const res = await db.atomic()
|
|
|
|
.check(nonceValue)
|
|
|
|
.delete(["nonces", nonce])
|
|
|
|
.set(["a", "b"], message.msg)
|
|
|
|
.commit();
|
|
|
|
if (res.ok) {
|
|
|
|
// Simulate an error so that the message has to be redelivered
|
|
|
|
throw new Error("injected error");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await db.atomic()
|
|
|
|
.check({ key: ["nonces", nonce], versionstamp: null })
|
|
|
|
.set(["nonces", nonce], true)
|
|
|
|
.enqueue({ msg: "test", nonce })
|
|
|
|
.commit();
|
|
|
|
assert(res.ok);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
assertEquals("test", dequeuedMessage);
|
|
|
|
|
|
|
|
const currentValue = await db.get(["a", "b"]);
|
|
|
|
assertEquals("test", currentValue.value);
|
|
|
|
|
|
|
|
const nonceValue = await db.get(["nonces", nonce]);
|
|
|
|
assertEquals(nonceValue.versionstamp, null);
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "queue persistence with inflight messages",
|
|
|
|
sanitizeOps: false,
|
|
|
|
sanitizeResources: false,
|
|
|
|
async fn() {
|
2023-06-14 19:27:59 -04:00
|
|
|
const filename = await Deno.makeTempFile({ prefix: "queue_db" });
|
2023-06-13 20:49:57 -04:00
|
|
|
try {
|
|
|
|
let db: Deno.Kv = await Deno.openKv(filename);
|
|
|
|
|
|
|
|
let count = 0;
|
2023-11-22 06:11:20 -05:00
|
|
|
let deferred = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
|
|
|
|
// Register long-running handler.
|
|
|
|
let listener = db.listenQueue(async (_msg) => {
|
|
|
|
count += 1;
|
|
|
|
if (count == 3) {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred.resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
2023-09-23 20:59:01 -04:00
|
|
|
await new Promise(() => {});
|
2023-06-13 20:49:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Enqueue 3 messages.
|
|
|
|
await db.enqueue("msg0");
|
|
|
|
await db.enqueue("msg1");
|
|
|
|
await db.enqueue("msg2");
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred.promise;
|
2023-06-13 20:49:57 -04:00
|
|
|
|
2023-06-26 09:10:27 -04:00
|
|
|
// Close the database and wait for the listener to finish.
|
2023-06-13 20:49:57 -04:00
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
|
2023-11-10 05:28:38 -05:00
|
|
|
// Wait at least MESSAGE_DEADLINE_TIMEOUT before reopening the database.
|
|
|
|
// This ensures that inflight messages are requeued immediately after
|
|
|
|
// the database is reopened.
|
|
|
|
// https://github.com/denoland/denokv/blob/efb98a1357d37291a225ed5cf1fc4ecc7c737fab/sqlite/backend.rs#L120
|
|
|
|
await sleep(6000);
|
|
|
|
|
2023-06-13 20:49:57 -04:00
|
|
|
// Now reopen the database.
|
|
|
|
db = await Deno.openKv(filename);
|
|
|
|
|
|
|
|
count = 0;
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
|
|
|
|
// Register a handler that will complete quickly.
|
|
|
|
listener = db.listenQueue((_msg) => {
|
|
|
|
count += 1;
|
|
|
|
if (count == 3) {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred.resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for the handlers to finish.
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred.promise;
|
2023-06-13 20:49:57 -04:00
|
|
|
assertEquals(3, count);
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
} finally {
|
2023-06-14 19:27:59 -04:00
|
|
|
try {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "queue persistence with delay messages",
|
|
|
|
async fn() {
|
2023-06-14 19:27:59 -04:00
|
|
|
const filename = await Deno.makeTempFile({ prefix: "queue_db" });
|
2023-06-13 20:49:57 -04:00
|
|
|
try {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
let db: Deno.Kv = await Deno.openKv(filename);
|
|
|
|
|
|
|
|
let count = 0;
|
2023-11-22 06:11:20 -05:00
|
|
|
let deferred = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
|
|
|
|
// Register long-running handler.
|
|
|
|
let listener = db.listenQueue((_msg) => {});
|
|
|
|
|
|
|
|
// Enqueue 3 messages into the future.
|
|
|
|
await db.enqueue("msg0", { delay: 10000 });
|
|
|
|
await db.enqueue("msg1", { delay: 10000 });
|
|
|
|
await db.enqueue("msg2", { delay: 10000 });
|
|
|
|
|
2023-06-26 09:10:27 -04:00
|
|
|
// Close the database and wait for the listener to finish.
|
2023-06-13 20:49:57 -04:00
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
|
|
|
|
// Now reopen the database.
|
|
|
|
db = await Deno.openKv(filename);
|
|
|
|
|
|
|
|
count = 0;
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred = Promise.withResolvers<void>();
|
2023-06-13 20:49:57 -04:00
|
|
|
|
|
|
|
// Register a handler that will complete quickly.
|
|
|
|
listener = db.listenQueue((_msg) => {
|
|
|
|
count += 1;
|
|
|
|
if (count == 3) {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred.resolve();
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for the handlers to finish.
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred.promise;
|
2023-06-13 20:49:57 -04:00
|
|
|
assertEquals(3, count);
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
} finally {
|
2023-06-14 19:27:59 -04:00
|
|
|
try {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2023-07-01 03:24:15 -04:00
|
|
|
|
2023-09-29 14:40:36 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "different kv instances for enqueue and queueListen",
|
|
|
|
async fn() {
|
|
|
|
const filename = await Deno.makeTempFile({ prefix: "queue_db" });
|
|
|
|
try {
|
|
|
|
const db0 = await Deno.openKv(filename);
|
|
|
|
const db1 = await Deno.openKv(filename);
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-09-29 14:40:36 -04:00
|
|
|
let dequeuedMessage: unknown = null;
|
|
|
|
const listener = db0.listenQueue((msg) => {
|
|
|
|
dequeuedMessage = msg;
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-09-29 14:40:36 -04:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
const res = await db1.enqueue("test");
|
|
|
|
assert(res.ok);
|
|
|
|
assertNotEquals(res.versionstamp, null);
|
|
|
|
await promise;
|
|
|
|
assertEquals(dequeuedMessage, "test");
|
|
|
|
} finally {
|
|
|
|
db0.close();
|
|
|
|
await listener;
|
|
|
|
db1.close();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-09-26 23:06:57 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "queue graceful close",
|
|
|
|
async fn() {
|
|
|
|
const db: Deno.Kv = await Deno.openKv(":memory:");
|
|
|
|
const listener = db.listenQueue((_msg) => {});
|
|
|
|
db.close();
|
|
|
|
await listener;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-12-13 01:51:23 -05:00
|
|
|
dbTest("invalid backoffSchedule", async (db) => {
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await db.enqueue("foo", { backoffSchedule: [1, 1, 1, 1, 1, 1] });
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"invalid backoffSchedule",
|
|
|
|
);
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await db.enqueue("foo", { backoffSchedule: [3600001] });
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"invalid backoffSchedule",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-07-01 03:24:15 -04:00
|
|
|
dbTest("atomic operation is exposed", (db) => {
|
|
|
|
assert(Deno.AtomicOperation);
|
|
|
|
const ao = db.atomic();
|
|
|
|
assert(ao instanceof Deno.AtomicOperation);
|
|
|
|
});
|
2023-08-17 06:53:55 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "racy open",
|
|
|
|
async fn() {
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
const filename = await Deno.makeTempFile({ prefix: "racy_open_db" });
|
|
|
|
try {
|
|
|
|
const [db1, db2, db3] = await Promise.all([
|
|
|
|
Deno.openKv(filename),
|
|
|
|
Deno.openKv(filename),
|
|
|
|
Deno.openKv(filename),
|
|
|
|
]);
|
|
|
|
db1.close();
|
|
|
|
db2.close();
|
|
|
|
db3.close();
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "racy write",
|
|
|
|
async fn() {
|
|
|
|
const filename = await Deno.makeTempFile({ prefix: "racy_write_db" });
|
|
|
|
const concurrency = 20;
|
|
|
|
const iterations = 5;
|
|
|
|
try {
|
|
|
|
const dbs = await Promise.all(
|
|
|
|
Array(concurrency).fill(0).map(() => Deno.openKv(filename)),
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
|
|
|
await Promise.all(
|
|
|
|
dbs.map((db) => db.atomic().sum(["counter"], 1n).commit()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assertEquals(
|
|
|
|
((await dbs[0].get(["counter"])).value as Deno.KvU64).value,
|
|
|
|
BigInt(concurrency * iterations),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
dbs.forEach((db) => db.close());
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2023-08-18 05:34:16 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "kv expiration",
|
|
|
|
async fn() {
|
|
|
|
const filename = await Deno.makeTempFile({ prefix: "kv_expiration_db" });
|
|
|
|
try {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
let db: Deno.Kv | null = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
db = await Deno.openKv(filename);
|
|
|
|
|
|
|
|
await db.set(["a"], 1, { expireIn: 1000 });
|
|
|
|
await db.set(["b"], 2, { expireIn: 1000 });
|
|
|
|
assertEquals((await db.get(["a"])).value, 1);
|
|
|
|
assertEquals((await db.get(["b"])).value, 2);
|
|
|
|
|
|
|
|
// Value overwrite should also reset expiration
|
|
|
|
await db.set(["b"], 2, { expireIn: 3600 * 1000 });
|
|
|
|
|
|
|
|
// Wait for expiration
|
|
|
|
await sleep(1000);
|
|
|
|
|
|
|
|
// Re-open to trigger immediate cleanup
|
|
|
|
db.close();
|
|
|
|
db = null;
|
|
|
|
db = await Deno.openKv(filename);
|
|
|
|
|
|
|
|
let ok = false;
|
|
|
|
for (let i = 0; i < 50; i++) {
|
|
|
|
await sleep(100);
|
|
|
|
if (
|
|
|
|
JSON.stringify(
|
|
|
|
(await db.getMany([["a"], ["b"]])).map((x) => x.value),
|
|
|
|
) === "[null,2]"
|
|
|
|
) {
|
|
|
|
ok = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
throw new Error("Values did not expire");
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (db) {
|
|
|
|
try {
|
|
|
|
db.close();
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "kv expiration with atomic",
|
|
|
|
async fn() {
|
|
|
|
const filename = await Deno.makeTempFile({ prefix: "kv_expiration_db" });
|
|
|
|
try {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
let db: Deno.Kv | null = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
db = await Deno.openKv(filename);
|
|
|
|
|
|
|
|
await db.atomic().set(["a"], 1, { expireIn: 1000 }).set(["b"], 2, {
|
|
|
|
expireIn: 1000,
|
|
|
|
}).commit();
|
|
|
|
assertEquals((await db.getMany([["a"], ["b"]])).map((x) => x.value), [
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Wait for expiration
|
|
|
|
await sleep(1000);
|
|
|
|
|
|
|
|
// Re-open to trigger immediate cleanup
|
|
|
|
db.close();
|
|
|
|
db = null;
|
|
|
|
db = await Deno.openKv(filename);
|
|
|
|
|
|
|
|
let ok = false;
|
|
|
|
for (let i = 0; i < 50; i++) {
|
|
|
|
await sleep(100);
|
|
|
|
if (
|
|
|
|
JSON.stringify(
|
|
|
|
(await db.getMany([["a"], ["b"]])).map((x) => x.value),
|
|
|
|
) === "[null,null]"
|
|
|
|
) {
|
|
|
|
ok = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
throw new Error("Values did not expire");
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (db) {
|
|
|
|
try {
|
|
|
|
db.close();
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await Deno.remove(filename);
|
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2023-08-22 01:56:00 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "remote backend",
|
|
|
|
async fn() {
|
|
|
|
const db = await Deno.openKv("http://localhost:4545/kv_remote_authorize");
|
|
|
|
try {
|
|
|
|
await db.set(["some-key"], 1);
|
|
|
|
const entry = await db.get(["some-key"]);
|
|
|
|
assertEquals(entry.value, null);
|
|
|
|
assertEquals(entry.versionstamp, null);
|
|
|
|
} finally {
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "remote backend invalid format",
|
|
|
|
async fn() {
|
|
|
|
const db = await Deno.openKv(
|
|
|
|
"http://localhost:4545/kv_remote_authorize_invalid_format",
|
|
|
|
);
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await db.set(["some-key"], 1);
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"Failed to parse metadata: ",
|
|
|
|
);
|
|
|
|
|
|
|
|
db.close();
|
2023-08-22 01:56:00 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "remote backend invalid version",
|
|
|
|
async fn() {
|
|
|
|
const db = await Deno.openKv(
|
|
|
|
"http://localhost:4545/kv_remote_authorize_invalid_version",
|
|
|
|
);
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await db.set(["some-key"], 1);
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"Failed to parse metadata: unsupported metadata version: 1000",
|
|
|
|
);
|
|
|
|
|
|
|
|
db.close();
|
2023-08-22 01:56:00 -04:00
|
|
|
},
|
|
|
|
});
|
2023-11-01 15:26:12 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true } },
|
|
|
|
async function kvExplicitResourceManagement() {
|
|
|
|
let kv2: Deno.Kv;
|
|
|
|
|
|
|
|
{
|
|
|
|
using kv = await Deno.openKv(":memory:");
|
|
|
|
kv2 = kv;
|
|
|
|
|
|
|
|
const res = await kv.get(["a"]);
|
|
|
|
assertEquals(res.versionstamp, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
await assertRejects(() => kv2.get(["a"]), Deno.errors.BadResource);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true } },
|
|
|
|
async function kvExplicitResourceManagementManualClose() {
|
|
|
|
using kv = await Deno.openKv(":memory:");
|
|
|
|
kv.close();
|
|
|
|
await assertRejects(() => kv.get(["a"]), Deno.errors.BadResource);
|
|
|
|
// calling [Symbol.dispose] after manual close is a no-op
|
|
|
|
},
|
|
|
|
);
|
2023-12-05 08:21:46 -05:00
|
|
|
|
|
|
|
dbTest("key watch", async (db) => {
|
|
|
|
const changeHistory: Deno.KvEntryMaybe<number>[] = [];
|
|
|
|
const watcher: ReadableStream<Deno.KvEntryMaybe<number>[]> = db.watch<
|
|
|
|
number[]
|
|
|
|
>([["key"]]);
|
|
|
|
|
|
|
|
const reader = watcher.getReader();
|
|
|
|
const expectedChanges = 2;
|
|
|
|
|
|
|
|
const work = (async () => {
|
|
|
|
for (let i = 0; i < expectedChanges; i++) {
|
|
|
|
const message = await reader.read();
|
|
|
|
if (message.done) {
|
|
|
|
throw new Error("Unexpected end of stream");
|
|
|
|
}
|
|
|
|
changeHistory.push(message.value[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
await reader.cancel();
|
|
|
|
})();
|
|
|
|
|
|
|
|
while (changeHistory.length !== 1) {
|
|
|
|
await sleep(100);
|
|
|
|
}
|
|
|
|
assertEquals(changeHistory[0], {
|
|
|
|
key: ["key"],
|
|
|
|
value: null,
|
|
|
|
versionstamp: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
const { versionstamp } = await db.set(["key"], 1);
|
|
|
|
while (changeHistory.length as number !== 2) {
|
|
|
|
await sleep(100);
|
|
|
|
}
|
|
|
|
assertEquals(changeHistory[1], {
|
|
|
|
key: ["key"],
|
|
|
|
value: 1,
|
|
|
|
versionstamp,
|
|
|
|
});
|
|
|
|
|
|
|
|
await work;
|
|
|
|
await reader.cancel();
|
|
|
|
});
|
2023-12-13 11:58:20 -05:00
|
|
|
|
|
|
|
dbTest("set with key versionstamp suffix", async (db) => {
|
|
|
|
const result1 = await Array.fromAsync(db.list({ prefix: ["a"] }));
|
|
|
|
assertEquals(result1, []);
|
|
|
|
|
|
|
|
const setRes1 = await db.set(["a", db.commitVersionstamp()], "b");
|
|
|
|
assert(setRes1.ok);
|
|
|
|
assert(setRes1.versionstamp > ZERO_VERSIONSTAMP);
|
|
|
|
|
|
|
|
const result2 = await Array.fromAsync(db.list({ prefix: ["a"] }));
|
|
|
|
assertEquals(result2.length, 1);
|
|
|
|
assertEquals(result2[0].key[1], setRes1.versionstamp);
|
|
|
|
assertEquals(result2[0].value, "b");
|
|
|
|
assertEquals(result2[0].versionstamp, setRes1.versionstamp);
|
|
|
|
|
|
|
|
const setRes2 = await db.atomic().set(["a", db.commitVersionstamp()], "c")
|
|
|
|
.commit();
|
|
|
|
assert(setRes2.ok);
|
|
|
|
assert(setRes2.versionstamp > setRes1.versionstamp);
|
|
|
|
|
|
|
|
const result3 = await Array.fromAsync(db.list({ prefix: ["a"] }));
|
|
|
|
assertEquals(result3.length, 2);
|
|
|
|
assertEquals(result3[1].key[1], setRes2.versionstamp);
|
|
|
|
assertEquals(result3[1].value, "c");
|
|
|
|
assertEquals(result3[1].versionstamp, setRes2.versionstamp);
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => await db.set(["a", db.commitVersionstamp(), "a"], "x"),
|
|
|
|
TypeError,
|
|
|
|
"expected string, number, bigint, ArrayBufferView, boolean",
|
|
|
|
);
|
|
|
|
});
|
2023-12-21 16:04:17 -05:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "watch should stop when db closed",
|
|
|
|
async fn() {
|
|
|
|
const db = await Deno.openKv(":memory:");
|
|
|
|
|
|
|
|
const watch = db.watch([["a"]]);
|
|
|
|
const completion = (async () => {
|
|
|
|
for await (const _item of watch) {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
db.close();
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
await completion;
|
|
|
|
},
|
|
|
|
});
|