@@ -100,7 +100,7 @@ export class DurableObjectExample extends DurableObject {
100100 assert . strictEqual ( container . running , false ) ;
101101 }
102102
103- async testSetInactivityTimeout ( ) {
103+ async testSetInactivityTimeout ( timeout ) {
104104 const container = this . ctx . container ;
105105 if ( container . running ) {
106106 let monitor = container . monitor ( ) . catch ( ( _err ) => { } ) ;
@@ -126,7 +126,18 @@ export class DurableObjectExample extends DurableObject {
126126 ) ;
127127 }
128128
129- await container . setInactivityTimeout ( 1000 ) ;
129+ if ( timeout > 0 ) {
130+ await container . setInactivityTimeout ( timeout ) ;
131+ }
132+ }
133+
134+ async start ( ) {
135+ assert . strictEqual ( this . ctx . container . running , false ) ;
136+ this . ctx . container . start ( ) ;
137+ assert . strictEqual ( this . ctx . container . running , true ) ;
138+
139+ // Wait for container to be running
140+ await scheduler . wait ( 500 ) ;
130141 }
131142
132143 // Assert that the container is running
@@ -333,19 +344,48 @@ export const testAlarm = {
333344 } ,
334345} ;
335346
347+ export const testContainerShutdown = {
348+ async test ( _ , env ) {
349+ {
350+ const stub = env . MY_CONTAINER . getByName ( 'testContainerShutdown' ) ;
351+ await stub . start ( ) ;
352+ await assert . rejects ( ( ) => stub . abort ( ) , {
353+ name : 'Error' ,
354+ message : 'Application called abort() to reset Durable Object.' ,
355+ } ) ;
356+ }
357+
358+ // Wait for the container to be shutdown after the DO aborts
359+ await scheduler . wait ( 500 ) ;
360+
361+ {
362+ const stub = env . MY_CONTAINER . getByName ( 'testContainerShutdown' ) ;
363+
364+ // Container should not be running after DO exited
365+ await stub . expectRunning ( false ) ;
366+ }
367+ } ,
368+ } ;
369+
336370export const testSetInactivityTimeout = {
337371 async test ( _ctrl , env ) {
338372 {
339373 const stub = env . MY_CONTAINER . getByName ( 'testSetInactivityTimeout' ) ;
340374
341- await stub . testSetInactivityTimeout ( ) ;
375+ await stub . testSetInactivityTimeout ( 3000 ) ;
342376
343377 await assert . rejects ( ( ) => stub . abort ( ) , {
344378 name : 'Error' ,
345379 message : 'Application called abort() to reset Durable Object.' ,
346380 } ) ;
347381 }
348382
383+ // Here we wait to ensure that if setInactivityTimeout *doesn't* work, the
384+ // container has enough time to shutdown after the DO is aborted. If we
385+ // don't wait then ctx.container.running will always be true, even without
386+ // setInactivityTimeout, because the container won't have stoped yet.
387+ await scheduler . wait ( 500 ) ;
388+
349389 {
350390 const stub = env . MY_CONTAINER . getByName ( 'testSetInactivityTimeout' ) ;
351391
0 commit comments