@@ -283,4 +283,50 @@ public function testStatisticsConsistency(): void
283283 $ this ->assertEquals (count ($ stats ['pool_sizes ' ]), count ($ stats ['pools_by_capacity ' ]));
284284 $ this ->assertEquals ($ stats ['active_pool_count ' ], count ($ stats ['pools_by_capacity ' ]));
285285 }
286+
287+ /**
288+ * Test that active_pool_count only counts non-empty pools
289+ */
290+ public function testActivePoolCountOnlyCountsNonEmptyPools (): void
291+ {
292+ // Initial state - no pools
293+ $ stats = JsonBufferPool::getStatistics ();
294+ $ this ->assertEquals (0 , $ stats ['active_pool_count ' ]);
295+
296+ // Create buffers but don't return them (pools exist but are empty)
297+ $ buffer1KB = JsonBufferPool::getBuffer (1024 );
298+ $ buffer4KB = JsonBufferPool::getBuffer (4096 );
299+ $ buffer8KB = JsonBufferPool::getBuffer (8192 );
300+
301+ $ stats = JsonBufferPool::getStatistics ();
302+ $ this ->assertEquals (0 , $ stats ['active_pool_count ' ], 'Empty pools should not be counted as active ' );
303+
304+ // Return only some buffers to pools
305+ JsonBufferPool::returnBuffer ($ buffer1KB );
306+ JsonBufferPool::returnBuffer ($ buffer4KB );
307+
308+ $ stats = JsonBufferPool::getStatistics ();
309+ $ this ->assertEquals (2 , $ stats ['active_pool_count ' ], 'Only non-empty pools should be counted as active ' );
310+ $ this ->assertEquals (2 , $ stats ['total_buffers_pooled ' ], 'Should have 2 buffers in pools ' );
311+
312+ // Return the remaining buffer
313+ JsonBufferPool::returnBuffer ($ buffer8KB );
314+
315+ $ stats = JsonBufferPool::getStatistics ();
316+ $ this ->assertEquals (3 , $ stats ['active_pool_count ' ], 'All pools with buffers should be counted as active ' );
317+ $ this ->assertEquals (3 , $ stats ['total_buffers_pooled ' ], 'Should have 3 buffers in pools ' );
318+
319+ // Get all buffers back (making pools empty again)
320+ $ retrievedBuffer1 = JsonBufferPool::getBuffer (1024 );
321+ $ retrievedBuffer2 = JsonBufferPool::getBuffer (4096 );
322+ $ retrievedBuffer3 = JsonBufferPool::getBuffer (8192 );
323+
324+ $ stats = JsonBufferPool::getStatistics ();
325+ $ this ->assertEquals (
326+ 0 ,
327+ $ stats ['active_pool_count ' ],
328+ 'Empty pools should not be counted as active after retrieval '
329+ );
330+ $ this ->assertEquals (0 , $ stats ['total_buffers_pooled ' ], 'Should have 0 buffers in pools after retrieval ' );
331+ }
286332}
0 commit comments