@@ -589,20 +589,6 @@ describe("RunQueueConsumerPool", () => {
589589 } ) ;
590590
591591 describe ( "Edge cases" , ( ) => {
592- it ( "should handle undefined queue lengths gracefully" , async ( ) => {
593- pool = new RunQueueConsumerPool ( {
594- ...defaultOptions ,
595- scaling : { strategy : "smooth" } ,
596- } ) ;
597-
598- await pool . start ( ) ;
599-
600- expect ( ( ) => pool . updateQueueLength ( undefined ) ) . not . toThrow ( ) ;
601-
602- const metrics = pool . getMetrics ( ) ;
603- expect ( metrics . queueLength ) . toBeUndefined ( ) ;
604- } ) ;
605-
606592 it ( "should handle empty recent queue lengths" , async ( ) => {
607593 pool = new RunQueueConsumerPool ( {
608594 ...defaultOptions ,
@@ -688,5 +674,48 @@ describe("RunQueueConsumerPool", () => {
688674 advanceTimeAndProcessMetrics ( 1100 ) ;
689675 expect ( pool . size ) . toBeLessThanOrEqual ( 6 ) ;
690676 } ) ;
677+
678+ it ( "should scale down when no items are dequeued (zero queue length)" , async ( ) => {
679+ pool = new RunQueueConsumerPool ( {
680+ ...defaultOptions ,
681+ scaling : {
682+ strategy : "smooth" ,
683+ minConsumerCount : 1 ,
684+ maxConsumerCount : 10 ,
685+ scaleUpCooldownMs : 0 ,
686+ scaleDownCooldownMs : 0 ,
687+ disableJitter : true ,
688+ } ,
689+ } ) ;
690+
691+ await pool . start ( ) ;
692+ expect ( pool . size ) . toBe ( 1 ) ;
693+
694+ // Scale up first
695+ pool . updateQueueLength ( 20 ) ;
696+ advanceTimeAndProcessMetrics ( 1100 ) ;
697+ expect ( pool . size ) . toBeGreaterThan ( 1 ) ;
698+ const sizeAfterScaleUp = pool . size ;
699+
700+ // Now send multiple zero queue lengths to converge EWMA to 0
701+ // The EWMA needs time to converge due to exponential smoothing
702+ for ( let i = 0 ; i < 5 ; i ++ ) {
703+ pool . updateQueueLength ( 0 ) ;
704+ advanceTimeAndProcessMetrics ( 1100 ) ;
705+ }
706+
707+ // After multiple iterations with zero queue, should scale down but not to minimum yet
708+ expect ( pool . size ) . toBeLessThan ( sizeAfterScaleUp ) ;
709+ expect ( pool . size ) . toBeGreaterThan ( 1 ) ;
710+
711+ // Continue until we reach minimum
712+ for ( let i = 0 ; i < 5 ; i ++ ) {
713+ pool . updateQueueLength ( 0 ) ;
714+ advanceTimeAndProcessMetrics ( 1100 ) ;
715+ }
716+
717+ // Should eventually reach minimum
718+ expect ( pool . size ) . toBe ( 1 ) ;
719+ } ) ;
691720 } ) ;
692721} ) ;
0 commit comments