Skip to content

Commit ab133ec

Browse files
committed
Add AlarmServer durable object and test
Introduce an AlarmServer Durable Object and test to verify that alarms initialize the DO and call onAlarm without redundant blockConcurrencyWhile. Changes: add AlarmServer class (hibernate:true) with counter and alarmCount, implement onStart/onAlarm/onRequest; export AlarmServer in the test Env; import runDurableObjectAlarm and add a test that schedules an alarm, triggers it via runDurableObjectAlarm, and asserts onStart and alarmCount behavior; register AlarmServer in wrangler.jsonc. This ensures alarm-triggered initialization works as expected.
1 parent a807ab3 commit ab133ec

3 files changed

Lines changed: 79 additions & 2 deletions

File tree

packages/partyserver/src/tests/index.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
createExecutionContext,
3-
env
3+
env,
4+
runDurableObjectAlarm
45
// waitOnExecutionContext
56
} from "cloudflare:test";
67
import { describe, expect, it } from "vitest";
@@ -401,6 +402,43 @@ describe("Hibernating Server (setName handles initialization)", () => {
401402
});
402403
});
403404

405+
describe("Alarm (initialize without redundant blockConcurrencyWhile)", () => {
406+
it("properly initializes on alarm and calls onAlarm", async () => {
407+
// Use a single stub for the entire test so runDurableObjectAlarm
408+
// sees the same DO instance that has the alarm scheduled.
409+
const id = env.AlarmServer.idFromName("alarm-test1");
410+
const stub = env.AlarmServer.get(id);
411+
412+
// Initialize the DO and schedule an alarm in one request
413+
const res = await stub.fetch(
414+
new Request(
415+
"http://example.com/parties/alarm-server/alarm-test1?setAlarm=1",
416+
{
417+
headers: { "x-partykit-room": "alarm-test1" }
418+
}
419+
)
420+
);
421+
expect(await res.text()).toEqual("alarm set");
422+
423+
// Trigger the alarm
424+
const ran = await runDurableObjectAlarm(stub);
425+
expect(ran).toBe(true);
426+
427+
// Verify state: onStart called once, alarm was triggered once
428+
const stateRes = await stub.fetch(
429+
new Request("http://example.com/", {
430+
headers: { "x-partykit-room": "alarm-test1" }
431+
})
432+
);
433+
const state = (await stateRes.json()) as {
434+
counter: number;
435+
alarmCount: number;
436+
};
437+
expect(state.counter).toEqual(1);
438+
expect(state.alarmCount).toEqual(1);
439+
});
440+
});
441+
404442
describe("CORS", () => {
405443
it("returns CORS headers on OPTIONS preflight for matched routes", async () => {
406444
const ctx = createExecutionContext();

packages/partyserver/src/tests/worker.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type Env = {
1212
Stateful: DurableObjectNamespace<Stateful>;
1313
OnStartServer: DurableObjectNamespace<OnStartServer>;
1414
HibernatingOnStartServer: DurableObjectNamespace<HibernatingOnStartServer>;
15+
AlarmServer: DurableObjectNamespace<AlarmServer>;
1516
Mixed: DurableObjectNamespace<Mixed>;
1617
ConfigurableState: DurableObjectNamespace<ConfigurableState>;
1718
ConfigurableStateInMemory: DurableObjectNamespace<ConfigurableStateInMemory>;
@@ -103,6 +104,40 @@ export class HibernatingOnStartServer extends Server {
103104
}
104105
}
105106

107+
/**
108+
* Tests that alarm() properly initializes the server
109+
* without the redundant blockConcurrencyWhile wrapper.
110+
*/
111+
export class AlarmServer extends Server {
112+
static options = {
113+
hibernate: true
114+
};
115+
116+
counter = 0;
117+
alarmCount = 0;
118+
119+
async onStart() {
120+
this.counter++;
121+
}
122+
123+
onAlarm() {
124+
this.alarmCount++;
125+
}
126+
127+
async onRequest(request: Request): Promise<Response> {
128+
const url = new URL(request.url);
129+
if (url.searchParams.get("setAlarm")) {
130+
// Schedule alarm far in the future so it won't auto-fire
131+
await this.ctx.storage.setAlarm(Date.now() + 60_000);
132+
return new Response("alarm set");
133+
}
134+
return Response.json({
135+
counter: this.counter,
136+
alarmCount: this.alarmCount
137+
});
138+
}
139+
}
140+
106141
export class Mixed extends Server {
107142
static options = {
108143
hibernate: true

packages/partyserver/src/tests/wrangler.jsonc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
{
4747
"name": "HibernatingOnStartServer",
4848
"class_name": "HibernatingOnStartServer"
49+
},
50+
{
51+
"name": "AlarmServer",
52+
"class_name": "AlarmServer"
4953
}
5054
]
5155
},
@@ -68,7 +72,7 @@
6872
},
6973
{
7074
"tag": "v4",
71-
"new_classes": ["HibernatingOnStartServer"]
75+
"new_classes": ["HibernatingOnStartServer", "AlarmServer"]
7276
}
7377
]
7478
}

0 commit comments

Comments
 (0)