Skip to content

Commit e37a5c2

Browse files
committed
test(rivetkit): cover async websocket close db access
1 parent e887f7e commit e37a5c2

1 file changed

Lines changed: 41 additions & 16 deletions

File tree

rivetkit-typescript/packages/rivetkit/tests/driver/actor-sleep-db.test.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {
88
SLEEP_DB_TIMEOUT,
99
SLEEP_SCHEDULE_AFTER_ON_SLEEP_DELAY_MS,
1010
} from "../../fixtures/driver-test-suite/sleep-db";
11+
import {
12+
RAW_WS_HANDLER_DELAY,
13+
RAW_WS_HANDLER_SLEEP_TIMEOUT,
14+
} from "../../fixtures/driver-test-suite/sleep";
1115
import {
1216
describeDriverMatrix,
1317
SQLITE_DRIVER_MATRIX_OPTIONS,
@@ -252,8 +256,7 @@ describeDriverMatrix("Actor Sleep Db", (driverTestConfig) => {
252256
);
253257
});
254258

255-
// TODO(#4705): Root-cause sleep-shutdown disconnect ordering and re-enable this coverage.
256-
test.skip("onDisconnect can write to c.db during sleep shutdown", async (c) => {
259+
test("onDisconnect can write to c.db during sleep shutdown", async (c) => {
257260
const { client } = await setupDriverTest(c, driverTestConfig);
258261

259262
// Create actor with a connection
@@ -294,12 +297,11 @@ describeDriverMatrix("Actor Sleep Db", (driverTestConfig) => {
294297
expect(events).toContain("disconnect");
295298
});
296299

297-
// TODO(#4705): Root-cause raw WebSocket close-handler DB access during sleep shutdown and re-enable this coverage.
298-
test.skip("async websocket close handler can use c.db before sleep completes", async (c) => {
300+
test("async websocket close handler can use c.db before sleep completes", async (c) => {
299301
const { client } = await setupDriverTest(c, driverTestConfig);
300302

301303
const actor = client.sleepWithRawWsCloseDb.getOrCreate([
302-
"raw-ws-close-db",
304+
`raw-ws-close-db-${crypto.randomUUID()}`,
303305
]);
304306
const ws = await connectRawWebSocket(actor);
305307

@@ -313,22 +315,33 @@ describeDriverMatrix("Actor Sleep Db", (driverTestConfig) => {
313315
ws.close();
314316
});
315317

316-
await waitForAction(actor.getStatus, (status) => {
317-
expect(status.sleepCount).toBe(1);
318-
expect(status.startCount).toBe(2);
319-
});
318+
await waitFor(
319+
driverTestConfig,
320+
RAW_WS_HANDLER_DELAY + RAW_WS_HANDLER_SLEEP_TIMEOUT + 1_000,
321+
);
320322

321323
const entries = await actor.getLogEntries();
322324
const events = entries.map((entry: LogEntry) => entry.event);
325+
expect(events).toContain("close-start");
326+
expect(events).toContain("close-finish");
323327
expect(events).toContain("sleep");
328+
expect(events).toContain("wake");
329+
expect(events.indexOf("close-start")).toBeLessThan(
330+
events.indexOf("close-finish"),
331+
);
332+
expect(events.indexOf("close-finish")).toBeLessThan(
333+
events.indexOf("sleep"),
334+
);
335+
expect(events.indexOf("sleep")).toBeLessThan(
336+
events.lastIndexOf("wake"),
337+
);
324338
}, 30_000);
325339

326-
// TODO(#4705): Root-cause addEventListener close-handler DB access during sleep shutdown and re-enable this coverage.
327-
test.skip("async websocket addEventListener close handler can use c.db before sleep completes", async (c) => {
340+
test("async websocket addEventListener close handler can use c.db before sleep completes", async (c) => {
328341
const { client } = await setupDriverTest(c, driverTestConfig);
329342

330343
const actor = client.sleepWithRawWsCloseDbListener.getOrCreate([
331-
"raw-ws-close-db-listener",
344+
`raw-ws-close-db-listener-${crypto.randomUUID()}`,
332345
]);
333346
const ws = await connectRawWebSocket(actor);
334347

@@ -342,14 +355,26 @@ describeDriverMatrix("Actor Sleep Db", (driverTestConfig) => {
342355
ws.close();
343356
});
344357

345-
await waitForAction(actor.getStatus, (status) => {
346-
expect(status.sleepCount).toBe(1);
347-
expect(status.startCount).toBe(2);
348-
});
358+
await waitFor(
359+
driverTestConfig,
360+
RAW_WS_HANDLER_DELAY + RAW_WS_HANDLER_SLEEP_TIMEOUT + 1_000,
361+
);
349362

350363
const entries = await actor.getLogEntries();
351364
const events = entries.map((entry: LogEntry) => entry.event);
365+
expect(events).toContain("close-start");
366+
expect(events).toContain("close-finish");
352367
expect(events).toContain("sleep");
368+
expect(events).toContain("wake");
369+
expect(events.indexOf("close-start")).toBeLessThan(
370+
events.indexOf("close-finish"),
371+
);
372+
expect(events.indexOf("close-finish")).toBeLessThan(
373+
events.indexOf("sleep"),
374+
);
375+
expect(events.indexOf("sleep")).toBeLessThan(
376+
events.lastIndexOf("wake"),
377+
);
353378
}, 30_000);
354379

355380
test("broadcast works in onSleep", async (c) => {

0 commit comments

Comments
 (0)