clone

a deno framework for local-first web apps that open instantly, work offline and sync when users choose

feat/141-effect-library
lofi / package / testdata / gset_hang_canary.ts
gset_hang_canary.ts 2.0K
1// Child process for the g-set cross-table write canary in2// package/schema/conformance_test.ts. Runs the writes that hang in3// jazz-tools@2.0.0-alpha.53 behind short races and force-exits, because a4// hung FFI write cannot be cancelled from inside a test.5import { createPolicyTestApp } from "jazz-tools/testing";6import { s } from "../schema/mod.ts";78const expectLike = (value: unknown) => ({9 toThrow() {10 if (typeof value === "function") value();11 },12 not: {13 toThrow() {14 if (typeof value === "function") value();15 },16 },17});1819const app = s.defineApp({20 parents: s.table({ name: s.string() }),21 tagged: s.table({ tags: s.array(s.string()).merge("g-set") as never }),22});23const permissions = s.definePermissions(app, ({ policy }) => {24 for (const table of [policy.parents, policy.tagged]) {25 table.allowInsert.always();26 table.allowRead.always();27 table.allowUpdate.always();28 table.allowDelete.always();29 }30});3132const testApp = await createPolicyTestApp(app, permissions, expectLike);33const db = testApp.as({ user_id: "canary", claims: {}, authMode: "local-first" });3435const race = (work: Promise<unknown>, label: string) =>36 Promise.race([37 work.then(() => `${label}:OK`, (error) => {38 const message = error instanceof Error ? error.message : String(error);39 return `${label}:ERROR:${message}`;40 }),41 new Promise<string>((resolve) => setTimeout(() => resolve(`${label}:HUNG`), 8_000)),42 ]);4344// Mode "sibling-first" writes a sibling table before the g-set table has45// ever been written (the shape that hangs in alpha.53). Mode "gset-first"46// seeds the g-set table, after which sibling writes succeed.47const mode = Deno.args[0] ?? "sibling-first";48if (mode === "gset-first") {49 console.log(50 await race(51 db.insert(app.tagged, { tags: ["a"] }).wait({ tier: "global" }),52 "gset-table-write",53 ),54 );55}56console.log(57 await race(58 db.insert(app.parents, { name: "w" }).wait({ tier: "global" }),59 "sibling-table-write",60 ),61);62Deno.exit(0);