-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pool_multiple_instances.php
More file actions
64 lines (51 loc) · 2.04 KB
/
Copy pathtest_pool_multiple_instances.php
File metadata and controls
64 lines (51 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Multiple independent Pool instances coexist.
*
* Verifies:
* - Each Pool gets a unique id() from the SharedRegistry.
* - Acquire/release on one pool does not affect the other's budget,
* idle count, or destroy accounting.
* - Dropping pool A runs destroy ONLY for A's resources.
*/
header('Content-Type: text/plain');
$destroyedA = [];
$destroyedB = [];
$poolA = new OxPHP\Shared\Pool(
fn(): object => (object) ['tag' => 'A-' . uniqid()],
function (object $r) use (&$destroyedA): void { $destroyedA[] = $r->tag; },
2,
);
$poolB = new OxPHP\Shared\Pool(
fn(): object => (object) ['tag' => 'B-' . uniqid()],
function (object $r) use (&$destroyedB): void { $destroyedB[] = $r->tag; },
2,
);
if ($poolA->id() === $poolB->id()) { echo "FAIL: pool ids must differ\n"; exit; }
// Populate one slot on each pool (same-thread reuse means successive
// acquire+release keeps the slot count at 1).
$ha = $poolA->acquire();
$aTag = $ha->get()->tag;
$ha->release();
$hb = $poolB->acquire();
$bTag = $hb->get()->tag;
$hb->release();
$sa = $poolA->stats();
if ($sa->size() !== 1 || $sa->idle() !== 1) {
echo "FAIL: poolA bookkeeping: size={$sa->size()} idle={$sa->idle()}\n"; exit;
}
$sb = $poolB->stats();
if ($sb->size() !== 1 || $sb->idle() !== 1) {
echo "FAIL: poolB bookkeeping: size={$sb->size()} idle={$sb->idle()}\n"; exit;
}
// Drop A only. destroy must fire for A's resource; B untouched.
unset($poolA);
if (count($destroyedA) !== 1) { echo "FAIL: A destroy count wrong: " . count($destroyedA) . "\n"; exit; }
if ($destroyedA[0] !== $aTag) { echo "FAIL: A destroyed wrong tag\n"; exit; }
if (count($destroyedB) !== 0) { echo "FAIL: B destroy must not fire from A drop\n"; exit; }
if ($poolB->stats()->size() !== 1) { echo "FAIL: poolB size must stay 1\n"; exit; }
// Now drop B.
unset($poolB);
if (count($destroyedB) !== 1) { echo "FAIL: B destroy count wrong: " . count($destroyedB) . "\n"; exit; }
if ($destroyedB[0] !== $bTag) { echo "FAIL: B destroyed wrong tag\n"; exit; }
echo "OK\n";