Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-nocheck
import { actor, queue } from "rivetkit";
import { db } from "@/common/database/mod";
import type { registry } from "./registry-static";

export const destroyObserver = actor({
Expand Down Expand Up @@ -88,6 +89,27 @@ export const destroyActor = actor({
},
});

export const destroyDbActor = actor({
state: { value: 0 },
db: db({
onMigrate: async (db) => {
await db.execute("CREATE TABLE IF NOT EXISTS entries (value TEXT)");
},
}),
actions: {
getValue: (c) => {
return c.state.value;
},
destroy: (c) => {
c.destroy();
},
setValueThenDestroy: (c, newValue: number) => {
c.state.value = newValue;
c.destroy();
},
},
});

export const destroyAbortSignalActor = actor({
state: {
abortEventCount: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { dbStressActor } from "./db-stress";
import {
destroyAbortSignalActor,
destroyActor,
destroyDbActor,
destroyObserver,
} from "./destroy";
import { customTimeoutActor, errorHandlingActor } from "./error-handling";
Expand Down Expand Up @@ -311,6 +312,7 @@ export const registry = setup({
destroyActor,
destroyObserver,
destroyAbortSignalActor,
destroyDbActor,
// From hibernation.ts
hibernationActor,
hibernationSleepWindowActor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4846,7 +4846,6 @@ export function buildNativeFactory(
}
} finally {
resolveNativeDestroy(runtime, ctx);
await actorCtx.closeDatabase();
clearNativeRuntimeState(runtime, ctx);
await actorCtx.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,5 +433,56 @@ describeDriverMatrix("Actor Destroy", (driverTestConfig) => {
expect(await handle.getValue()).toBe(0);
expect(await handle.resolve()).toBe(await recreated.resolve());
});

type DriverTestClient = Awaited<
ReturnType<typeof setupDriverTest>
>["client"];

async function startDbActor(client: DriverTestClient, name: string) {
const actorKey = `test-destroy-db-${name}-${crypto.randomUUID()}`;
const handle = client.destroyDbActor.getOrCreate([actorKey]);
expect(await handle.getValue()).toBe(0);
const actorId = await handle.resolve();
return { actorKey, actorId, handle };
}

async function expectDbActorDestroyed(
client: DriverTestClient,
{ actorKey, actorId }: { actorKey: string; actorId: string },
) {
// Poll because actor lookup only turns into not_found once destroy teardown fully completes.
await vi.waitFor(async () => {
let actorRunning = false;
try {
await client.destroyDbActor.getForId(actorId).getValue();
actorRunning = true;
} catch (error) {
expectActorNotFound(error);
}
expect(actorRunning, "actor still running").toBeFalsy();
});

const recreated = client.destroyDbActor.getOrCreate([actorKey]);
expect(await recreated.getValue()).toBe(0);
expect(await recreated.resolve()).not.toBe(actorId);
}

test("actor with a database destroys with clean state", async (c) => {
const { client } = await setupDriverTest(c, driverTestConfig);
const dbActor = await startDbActor(client, "clean-state");

await dbActor.handle.destroy();

await expectDbActorDestroyed(client, dbActor);
});

test("actor with a database destroys after an unsaved state change", async (c) => {
const { client } = await setupDriverTest(c, driverTestConfig);
const dbActor = await startDbActor(client, "unsaved-state");

await dbActor.handle.setValueThenDestroy(7);

await expectDbActorDestroyed(client, dbActor);
});
});
});