@@ -294,6 +294,66 @@ describe("NodeRuntime resource budgets", () => {
294294 expect ( out ) . toContain ( "blocked:true" ) ;
295295 expect ( out ) . toContain ( "created:3" ) ;
296296 } ) ;
297+
298+ it ( "cleared timers free slots for new ones" , async ( ) => {
299+ const capture = createConsoleCapture ( ) ;
300+ proc = createTestNodeRuntime ( {
301+ onStdio : capture . onStdio ,
302+ resourceBudgets : { maxTimers : 6 } ,
303+ } ) ;
304+
305+ const result = await proc . exec ( `
306+ // Fill all 6 slots
307+ const ids = [];
308+ for (let i = 0; i < 6; i++) ids.push(setInterval(() => {}, 60000));
309+ // Cap reached — next one should throw
310+ let blocked = false;
311+ try { setInterval(() => {}, 60000); } catch(e) { blocked = true; }
312+ // Clear half
313+ for (let i = 0; i < 3; i++) clearInterval(ids[i]);
314+ // Now 3 slots are free — create 3 more
315+ let created = 0;
316+ for (let i = 0; i < 3; i++) {
317+ try { setInterval(() => {}, 60000); created++; } catch(e) {}
318+ }
319+ // 7th total new one should be blocked again
320+ let blocked2 = false;
321+ try { setInterval(() => {}, 60000); } catch(e) { blocked2 = true; }
322+ console.log('blocked:' + blocked);
323+ console.log('created:' + created);
324+ console.log('blocked2:' + blocked2);
325+ ` ) ;
326+
327+ expect ( result . code ) . toBe ( 0 ) ;
328+ const out = capture . stdout ( ) ;
329+ expect ( out ) . toContain ( "blocked:true" ) ;
330+ expect ( out ) . toContain ( "created:3" ) ;
331+ expect ( out ) . toContain ( "blocked2:true" ) ;
332+ } ) ;
333+
334+ it ( "normal code with fewer than 100 timers works fine" , async ( ) => {
335+ const capture = createConsoleCapture ( ) ;
336+ proc = createTestNodeRuntime ( {
337+ onStdio : capture . onStdio ,
338+ } ) ;
339+
340+ const result = await proc . exec ( `
341+ let count = 0;
342+ for (let i = 0; i < 50; i++) {
343+ setTimeout(() => {}, 60000);
344+ count++;
345+ }
346+ for (let i = 0; i < 30; i++) {
347+ setInterval(() => {}, 60000);
348+ count++;
349+ }
350+ console.log('timers:' + count);
351+ ` ) ;
352+
353+ expect ( result . code ) . toBe ( 0 ) ;
354+ const out = capture . stdout ( ) ;
355+ expect ( out ) . toContain ( "timers:80" ) ;
356+ } ) ;
297357 } ) ;
298358
299359 // -----------------------------------------------------------------------
0 commit comments