-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pool_destroy.php
More file actions
63 lines (54 loc) · 2.06 KB
/
Copy pathtest_pool_destroy.php
File metadata and controls
63 lines (54 loc) · 2.06 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
<?php
/**
* $destroy invocation on pool drop.
*
* Verifies that PoolInner::on_drop drains idle deques and calls
* the user-supplied $destroy callback for each pooled resource
* exactly once. Also covers the null-$destroy path: dropping a
* pool whose destroy callback is null must not crash.
*/
header('Content-Type: text/plain');
// ── Scenario 1: destroy is called once per idle resource on drop ──
$destroyed = [];
$pool = new OxPHP\Shared\Pool(
function (): object {
return (object) ['id' => uniqid('res_', true)];
},
function (object $r) use (&$destroyed): void {
$destroyed[] = $r->id;
},
2, // maxSize — force two distinct slots below
);
// Acquire both slots simultaneously so the second acquire mints a
// new resource (idle reuse would give us only one slot to destroy).
$h1 = $pool->acquire();
$h2 = $pool->acquire();
$id1 = $h1->get()->id;
$id2 = $h2->get()->id;
$h1->release();
$h2->release();
$s = $pool->stats();
if ($s->size() !== 2) { echo "FAIL: expected size 2, got {$s->size()}\n"; exit; }
if ($s->idle() !== 2) { echo "FAIL: expected idle 2, got {$s->idle()}\n"; exit; }
if (count($destroyed) !== 0) { echo "FAIL: destroy must not run before drop\n"; exit; }
// Drop the last ref → registry drops the Pool → on_drop drains idle
// deques → destroy runs for each slot. The $destroyed array is
// captured by reference so we can observe the invocations here.
unset($pool);
if (count($destroyed) !== 2) {
echo "FAIL: expected 2 destroy invocations, got " . count($destroyed) . "\n";
exit;
}
$seen = array_flip($destroyed);
if (!isset($seen[$id1])) { echo "FAIL: first resource not destroyed\n"; exit; }
if (!isset($seen[$id2])) { echo "FAIL: second resource not destroyed\n"; exit; }
// ── Scenario 2: null $destroy must not crash on drop ──────────────
$pool2 = new OxPHP\Shared\Pool(
fn(): object => new stdClass(),
null, // no destroy callback
1,
);
$h = $pool2->acquire();
$h->release();
unset($pool2); // must not crash; slot freed by zval_ptr_dtor alone
echo "OK\n";