Skip to content

Commit ae08da7

Browse files
committed
feat(tests): adicionar teste para garantir que pool_sizes estão ordenados por capacidade
1 parent 1bb9e44 commit ae08da7

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Json/Pool/JsonBufferPool.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,29 @@ public static function getStatistics(): array
186186
// Sort pools by capacity for better readability
187187
ksort($poolsByCapacity);
188188

189+
// Sort pool_sizes by extracting numeric capacity for consistent ordering
190+
uksort(
191+
$poolSizes,
192+
function ($a, $b) {
193+
// Extract numeric capacity from formatted strings like "1.0KB (1024 bytes)"
194+
preg_match('/\((\d+) bytes\)/', $a, $matchesA);
195+
preg_match('/\((\d+) bytes\)/', $b, $matchesB);
196+
197+
$capacityA = isset($matchesA[1]) ? (int)$matchesA[1] : 0;
198+
$capacityB = isset($matchesB[1]) ? (int)$matchesB[1] : 0;
199+
200+
return $capacityA <=> $capacityB;
201+
}
202+
);
203+
189204
return [
190205
'reuse_rate' => round($reuseRate, 2),
191206
'total_operations' => $totalOperations,
192207
'current_usage' => self::$stats['current_usage'],
193208
'peak_usage' => self::$stats['peak_usage'],
194209
'total_buffers_pooled' => $totalBuffersInPools,
195210
'active_pool_count' => count(self::$pools),
196-
'pool_sizes' => $poolSizes, // Legacy format for backward compatibility
211+
'pool_sizes' => $poolSizes, // Legacy format sorted by capacity
197212
'pools_by_capacity' => array_values($poolsByCapacity), // Enhanced format
198213
'detailed_stats' => self::$stats
199214
];

tests/Json/Pool/JsonBufferPoolStatisticsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,47 @@ public function testStatisticsWithNoPools(): void
212212
$this->assertEquals(0.0, $stats['reuse_rate']);
213213
}
214214

215+
/**
216+
* Test that pool_sizes are sorted by capacity for consistency
217+
*/
218+
public function testPoolSizesSorting(): void
219+
{
220+
// Create buffers in reverse order to test sorting
221+
$buffer16KB = JsonBufferPool::getBuffer(16384);
222+
$buffer1KB = JsonBufferPool::getBuffer(1024);
223+
$buffer8KB = JsonBufferPool::getBuffer(8192);
224+
$buffer4KB = JsonBufferPool::getBuffer(4096);
225+
226+
JsonBufferPool::returnBuffer($buffer16KB);
227+
JsonBufferPool::returnBuffer($buffer1KB);
228+
JsonBufferPool::returnBuffer($buffer8KB);
229+
JsonBufferPool::returnBuffer($buffer4KB);
230+
231+
$stats = JsonBufferPool::getStatistics();
232+
$poolSizes = $stats['pool_sizes'];
233+
234+
// Get the keys (capacity strings) as an array
235+
$capacityKeys = array_keys($poolSizes);
236+
237+
// Expected order: smallest to largest
238+
$expectedOrder = [
239+
'1.0KB (1024 bytes)',
240+
'4.0KB (4096 bytes)',
241+
'8.0KB (8192 bytes)',
242+
'16.0KB (16384 bytes)'
243+
];
244+
245+
$this->assertEquals($expectedOrder, $capacityKeys, 'Pool sizes should be sorted by capacity');
246+
247+
// Also verify pools_by_capacity is sorted (already tested but good to be explicit)
248+
$poolsByCapacity = $stats['pools_by_capacity'];
249+
$capacities = array_column($poolsByCapacity, 'capacity_bytes');
250+
$sortedCapacities = $capacities;
251+
sort($sortedCapacities);
252+
253+
$this->assertEquals($sortedCapacities, $capacities, 'Pools by capacity should be sorted');
254+
}
255+
215256
/**
216257
* Test statistics consistency between formats
217258
*/

0 commit comments

Comments
 (0)