Skip to content

Commit 14cc449

Browse files
committed
Add test for setInactivityTimeout
1 parent 0b73b6a commit 14cc449

File tree

1 file changed

+87
-49
lines changed
  • src/workerd/server/tests/container-client

1 file changed

+87
-49
lines changed

src/workerd/server/tests/container-client/test.js

Lines changed: 87 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,37 @@ export class DurableObjectExample extends DurableObject {
1212
}
1313
assert.strictEqual(container.running, false);
1414

15-
// Start container with valid configuration
16-
await container.start({
17-
entrypoint: ['node', 'nonexistant.js'],
18-
});
15+
// Start container with invalid entrypoint
16+
{
17+
container.start({
18+
entrypoint: ['node', 'nonexistant.js'],
19+
});
1920

20-
let exitCode = undefined;
21-
await container.monitor().catch((err) => {
22-
exitCode = err.exitCode;
23-
});
21+
let exitCode = undefined;
22+
await container.monitor().catch((err) => {
23+
exitCode = err.exitCode;
24+
});
25+
26+
assert.strictEqual(typeof exitCode, 'number');
27+
assert.notEqual(0, exitCode);
28+
}
29+
30+
// Start container with valid entrypoint and stop it
31+
{
32+
container.start();
33+
34+
await scheduler.wait(500);
35+
36+
let exitCode = undefined;
37+
const monitor = container.monitor().catch((err) => {
38+
exitCode = err.exitCode;
39+
});
40+
await container.destroy();
41+
await monitor;
2442

25-
assert.strictEqual(typeof exitCode, 'number');
26-
assert.notEqual(0, exitCode);
43+
assert.strictEqual(typeof exitCode, 'number');
44+
assert.equal(137, exitCode);
45+
}
2746
}
2847

2948
async testBasics() {
@@ -36,7 +55,7 @@ export class DurableObjectExample extends DurableObject {
3655
assert.strictEqual(container.running, false);
3756

3857
// Start container with valid configuration
39-
await container.start({
58+
container.start({
4059
env: { A: 'B', C: 'D', L: 'F' },
4160
enableInternet: true,
4261
});
@@ -68,7 +87,7 @@ export class DurableObjectExample extends DurableObject {
6887
);
6988
throw e;
7089
}
71-
await scheduler.wait(1000);
90+
await scheduler.wait(500);
7291
}
7392
}
7493

@@ -81,28 +100,41 @@ export class DurableObjectExample extends DurableObject {
81100
assert.strictEqual(container.running, false);
82101
}
83102

84-
async leaveRunning() {
85-
// Start container and leave it running
103+
async testSetInactivityTimeout() {
86104
const container = this.ctx.container;
87-
if (!container.running) {
88-
await container.start({
89-
entrypoint: ['leave-running'],
90-
});
105+
if (container.running) {
106+
let monitor = container.monitor().catch((_err) => {});
107+
await container.destroy();
108+
await monitor;
91109
}
110+
assert.strictEqual(container.running, false);
111+
112+
container.start();
92113

93114
assert.strictEqual(container.running, true);
94-
}
95115

96-
async checkRunning() {
97-
// Check container was started using leaveRunning()
98-
const container = this.ctx.container;
116+
// Wait for container to be running
117+
await scheduler.wait(500);
99118

100-
// Let's guard in case the test assumptions are wrong.
101-
if (!container.running) {
102-
return;
103-
}
119+
await assert.rejects(
120+
() => container.setInactivityTimeout(0),
121+
(err) => {
122+
assert.strictEqual(err.name, 'TypeError');
123+
assert.match(
124+
err.message,
125+
/setInactivityTimeout\(\) cannot be called with a durationMs <= 0/
126+
);
127+
return true;
128+
}
129+
);
104130

105-
await container.destroy();
131+
await container.setInactivityTimeout(1000);
132+
}
133+
134+
// Assert that the container is running
135+
async expectRunning(running) {
136+
assert.strictEqual(this.ctx.container.running, running);
137+
await this.ctx.container.destroy();
106138
}
107139

108140
async abort() {
@@ -130,7 +162,7 @@ export class DurableObjectExample extends DurableObject {
130162

131163
async startAlarm(start, ms) {
132164
if (start && !this.ctx.container.running) {
133-
await this.ctx.container.start();
165+
this.ctx.container.start();
134166
}
135167
await this.ctx.storage.setAlarm(Date.now() + ms);
136168
}
@@ -148,7 +180,7 @@ export class DurableObjectExample extends DurableObject {
148180
const { container } = this.ctx;
149181

150182
if (!container.running) {
151-
await container.start({
183+
container.start({
152184
env: { WS_ENABLED: 'true' },
153185
enableInternet: true,
154186
});
@@ -256,25 +288,6 @@ export const testExitCode = {
256288
},
257289
};
258290

259-
// Test container persistence across durable object instances
260-
export const testAlreadyRunning = {
261-
async test(_ctrl, env) {
262-
const id = env.MY_CONTAINER.idFromName('testAlreadyRunning');
263-
let stub = env.MY_CONTAINER.get(id);
264-
265-
await stub.leaveRunning();
266-
267-
await assert.rejects(() => stub.abort(), {
268-
name: 'Error',
269-
message: 'Application called abort() to reset Durable Object.',
270-
});
271-
272-
// Recreate stub to get a new instance
273-
stub = env.MY_CONTAINER.get(id);
274-
await stub.checkRunning();
275-
},
276-
};
277-
278291
// Test WebSocket functionality
279292
export const testWebSockets = {
280293
async test(_ctrl, env) {
@@ -284,7 +297,7 @@ export const testWebSockets = {
284297
},
285298
};
286299

287-
// // Test alarm functionality with containers
300+
// Test alarm functionality with containers
288301
export const testAlarm = {
289302
async test(_ctrl, env) {
290303
// Test that we can recover the use_containers flag correctly in setAlarm
@@ -302,6 +315,9 @@ export const testAlarm = {
302315
retries++;
303316
}
304317

318+
// Wait for container to start
319+
await scheduler.wait(500);
320+
305321
// Set alarm for future and abort
306322
await stub.startAlarm(false, 1000);
307323

@@ -318,3 +334,25 @@ export const testAlarm = {
318334
await stub.checkAlarmAbortConfirmation();
319335
},
320336
};
337+
338+
export const testSetInactivityTimeout = {
339+
async test(_ctrl, env) {
340+
{
341+
const stub = env.MY_CONTAINER.getByName('testSetInactivityTimeout');
342+
343+
await stub.testSetInactivityTimeout();
344+
345+
await assert.rejects(() => stub.abort(), {
346+
name: 'Error',
347+
message: 'Application called abort() to reset Durable Object.',
348+
});
349+
}
350+
351+
{
352+
const stub = env.MY_CONTAINER.getByName('testSetInactivityTimeout');
353+
354+
// Container should still be running after DO exited
355+
await stub.expectRunning(true);
356+
}
357+
},
358+
};

0 commit comments

Comments
 (0)