Skip to content

Commit eb947ac

Browse files
committed
feat(tests): adicionar testes para alinhamento de capacidade no JsonBufferPool
1 parent 36c6ec9 commit eb947ac

File tree

2 files changed

+212
-4
lines changed

2 files changed

+212
-4
lines changed

src/Json/Pool/JsonBufferPool.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public static function getBuffer(?int $capacity = null): JsonBuffer
8484
$capacity = $capacity ?? self::$config['default_capacity'];
8585
$poolKey = self::getPoolKey($capacity);
8686

87+
// Extract normalized capacity from pool key to ensure buffer creation alignment
88+
$normalizedCapacity = self::getNormalizedCapacity($capacity);
89+
8790
if (!isset(self::$pools[$poolKey])) {
8891
self::$pools[$poolKey] = [];
8992
}
@@ -99,8 +102,8 @@ public static function getBuffer(?int $capacity = null): JsonBuffer
99102
return $buffer;
100103
}
101104

102-
// Create new buffer
103-
$buffer = new JsonBuffer($capacity);
105+
// Create new buffer with normalized capacity to match pool key
106+
$buffer = new JsonBuffer($normalizedCapacity);
104107
self::$stats['allocations']++;
105108
self::$stats['current_usage']++;
106109

@@ -383,16 +386,25 @@ private static function validateConfiguration(array $config): void
383386
}
384387

385388
/**
386-
* Get pool key for given capacity
389+
* Get normalized capacity (next power of 2)
387390
*/
388-
private static function getPoolKey(int $capacity): string
391+
private static function getNormalizedCapacity(int $capacity): int
389392
{
390393
// Normalize to power of 2 for efficient pooling
391394
$normalizedCapacity = 1;
392395
while ($normalizedCapacity < $capacity) {
393396
$normalizedCapacity <<= 1;
394397
}
395398

399+
return $normalizedCapacity;
400+
}
401+
402+
/**
403+
* Get pool key for given capacity
404+
*/
405+
private static function getPoolKey(int $capacity): string
406+
{
407+
$normalizedCapacity = self::getNormalizedCapacity($capacity);
396408
return "buffer_{$normalizedCapacity}";
397409
}
398410

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PivotPHP\Core\Tests\Json\Pool;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use PivotPHP\Core\Json\Pool\JsonBufferPool;
9+
use ReflectionClass;
10+
11+
/**
12+
* Test JsonBufferPool capacity alignment between pool keys and actual buffers
13+
*/
14+
class JsonBufferPoolCapacityAlignmentTest extends TestCase
15+
{
16+
protected function setUp(): void
17+
{
18+
// Clear pools and reset configuration before each test
19+
JsonBufferPool::clearPools();
20+
JsonBufferPool::resetConfiguration();
21+
}
22+
23+
protected function tearDown(): void
24+
{
25+
// Clear pools and reset configuration after each test
26+
JsonBufferPool::clearPools();
27+
JsonBufferPool::resetConfiguration();
28+
}
29+
30+
/**
31+
* Test that buffer capacity matches normalized pool key capacity
32+
*/
33+
public function testBufferCapacityMatchesPoolKey(): void
34+
{
35+
// Test various capacities that should be normalized
36+
$testCases = [
37+
// [requested_capacity, expected_normalized_capacity]
38+
[1000, 1024], // 1000 -> 1024 (next power of 2)
39+
[1024, 1024], // 1024 -> 1024 (already power of 2)
40+
[2000, 2048], // 2000 -> 2048
41+
[512, 512], // 512 -> 512 (already power of 2)
42+
[513, 1024], // 513 -> 1024
43+
[100, 128], // 100 -> 128
44+
[1, 1], // 1 -> 1 (minimum)
45+
];
46+
47+
foreach ($testCases as [$requestedCapacity, $expectedCapacity]) {
48+
// Get buffer from pool
49+
$buffer = JsonBufferPool::getBuffer($requestedCapacity);
50+
51+
// Verify that the actual buffer capacity matches the expected normalized capacity
52+
$this->assertEquals(
53+
$expectedCapacity,
54+
$buffer->getCapacity(),
55+
"Buffer requested with capacity {$requestedCapacity} should have " .
56+
"normalized capacity {$expectedCapacity}"
57+
);
58+
59+
// Return buffer to pool
60+
JsonBufferPool::returnBuffer($buffer);
61+
}
62+
}
63+
64+
/**
65+
* Test that returned buffers can be properly reused
66+
*/
67+
public function testBufferReuseWithNormalizedCapacity(): void
68+
{
69+
// Request buffer with non-power-of-2 capacity
70+
$originalBuffer = JsonBufferPool::getBuffer(1000);
71+
$expectedCapacity = 1024; // Should be normalized to 1024
72+
73+
$this->assertEquals($expectedCapacity, $originalBuffer->getCapacity());
74+
75+
// Return buffer to pool
76+
JsonBufferPool::returnBuffer($originalBuffer);
77+
78+
// Request buffer with the same non-power-of-2 capacity
79+
$reusedBuffer = JsonBufferPool::getBuffer(1000);
80+
81+
// Should get the same buffer back (reused)
82+
$this->assertSame($originalBuffer, $reusedBuffer);
83+
$this->assertEquals($expectedCapacity, $reusedBuffer->getCapacity());
84+
85+
// Also test requesting with the exact normalized capacity
86+
JsonBufferPool::returnBuffer($reusedBuffer);
87+
$exactCapacityBuffer = JsonBufferPool::getBuffer(1024);
88+
89+
// Should get the same buffer back
90+
$this->assertSame($originalBuffer, $exactCapacityBuffer);
91+
$this->assertEquals($expectedCapacity, $exactCapacityBuffer->getCapacity());
92+
}
93+
94+
/**
95+
* Test pool statistics accuracy with normalized capacities
96+
*/
97+
public function testPoolStatisticsWithNormalizedCapacities(): void
98+
{
99+
// Request buffers with various capacities
100+
$buffer1 = JsonBufferPool::getBuffer(1000); // -> 1024
101+
$buffer2 = JsonBufferPool::getBuffer(1024); // -> 1024 (same pool)
102+
$buffer3 = JsonBufferPool::getBuffer(2000); // -> 2048 (different pool)
103+
104+
// All buffers should have normalized capacities
105+
$this->assertEquals(1024, $buffer1->getCapacity());
106+
$this->assertEquals(1024, $buffer2->getCapacity());
107+
$this->assertEquals(2048, $buffer3->getCapacity());
108+
109+
$stats = JsonBufferPool::getStatistics();
110+
$this->assertEquals(3, $stats['detailed_stats']['allocations']);
111+
$this->assertEquals(0, $stats['detailed_stats']['reuses']); // No reuse yet
112+
113+
// Return buffers to pool
114+
JsonBufferPool::returnBuffer($buffer1);
115+
JsonBufferPool::returnBuffer($buffer2);
116+
JsonBufferPool::returnBuffer($buffer3);
117+
118+
// Request again - should reuse
119+
$reusedBuffer1 = JsonBufferPool::getBuffer(1000); // Should reuse from 1024 pool
120+
$reusedBuffer2 = JsonBufferPool::getBuffer(2000); // Should reuse from 2048 pool
121+
122+
$stats = JsonBufferPool::getStatistics();
123+
$this->assertEquals(3, $stats['detailed_stats']['allocations']); // No new allocations
124+
$this->assertEquals(2, $stats['detailed_stats']['reuses']); // 2 reuses
125+
126+
// Verify we got the right buffers back
127+
$this->assertEquals(1024, $reusedBuffer1->getCapacity());
128+
$this->assertEquals(2048, $reusedBuffer2->getCapacity());
129+
}
130+
131+
/**
132+
* Test that pool keys are consistent for the same normalized capacity
133+
*/
134+
public function testPoolKeyConsistency(): void
135+
{
136+
$reflection = new ReflectionClass(JsonBufferPool::class);
137+
$getPoolKeyMethod = $reflection->getMethod('getPoolKey');
138+
$getPoolKeyMethod->setAccessible(true);
139+
140+
// Test that different requested capacities that normalize to the same value
141+
// generate the same pool key
142+
$key1000 = $getPoolKeyMethod->invoke(null, 1000);
143+
$key1024 = $getPoolKeyMethod->invoke(null, 1024);
144+
$key900 = $getPoolKeyMethod->invoke(null, 900);
145+
146+
// All should normalize to 1024
147+
$this->assertEquals('buffer_1024', $key1000);
148+
$this->assertEquals('buffer_1024', $key1024);
149+
$this->assertEquals('buffer_1024', $key900);
150+
151+
// Different normalized capacity should have different key
152+
$key2000 = $getPoolKeyMethod->invoke(null, 2000);
153+
$this->assertEquals('buffer_2048', $key2000);
154+
$this->assertNotEquals($key1000, $key2000);
155+
}
156+
157+
/**
158+
* Test edge case with capacity 1 (minimum)
159+
*/
160+
public function testMinimumCapacityAlignment(): void
161+
{
162+
$buffer = JsonBufferPool::getBuffer(1);
163+
$this->assertEquals(1, $buffer->getCapacity());
164+
165+
JsonBufferPool::returnBuffer($buffer);
166+
167+
// Should be able to reuse
168+
$reusedBuffer = JsonBufferPool::getBuffer(1);
169+
$this->assertSame($buffer, $reusedBuffer);
170+
}
171+
172+
/**
173+
* Test that buffer creation and return cycle maintains capacity consistency
174+
*/
175+
public function testCapacityConsistencyThroughCycle(): void
176+
{
177+
$originalCapacity = 1500; // Will be normalized to 2048
178+
$expectedCapacity = 2048;
179+
180+
// Create and return buffer multiple times
181+
for ($i = 0; $i < 5; $i++) {
182+
$buffer = JsonBufferPool::getBuffer($originalCapacity);
183+
$this->assertEquals(
184+
$expectedCapacity,
185+
$buffer->getCapacity(),
186+
"Iteration {$i}: Buffer capacity should remain consistent"
187+
);
188+
189+
JsonBufferPool::returnBuffer($buffer);
190+
}
191+
192+
// Final check - should still get normalized capacity
193+
$finalBuffer = JsonBufferPool::getBuffer($originalCapacity);
194+
$this->assertEquals($expectedCapacity, $finalBuffer->getCapacity());
195+
}
196+
}

0 commit comments

Comments
 (0)