-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mutex_array_initial.php
More file actions
69 lines (63 loc) · 2.54 KB
/
Copy pathtest_mutex_array_initial.php
File metadata and controls
69 lines (63 loc) · 2.54 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
65
66
67
68
69
<?php
/**
* Mutex::__construct(mixed $initial) — the stub declares `mixed`,
* so arrays and objects must be either stored verbatim or rejected
* with TypeException. Silently coercing to null is a data-loss bug.
*
* Reproducer for: handler's ValType match has a `_ => SharedValue::Null`
* catch-all that drops every type except Long/Double/Bool/String.
*/
header('Content-Type: text/plain');
use OxPHP\Shared\Mutex;
use OxPHP\Shared\TypeException;
// ── 1. Array initial state ─────────────────────────────────────────
$threw = null;
$stored = null;
try {
$m = new Mutex(['k' => 'v', 'n' => 42]);
$stored = $m->withLock(fn(&$s) => $s);
} catch (\Throwable $e) {
$threw = $e;
}
if ($threw !== null) {
// Acceptable fix: reject with TypeException pointing at the initial arg.
if (!($threw instanceof TypeException)) {
echo "FAIL: array \$initial raised ", $threw::class,
", expected TypeException or successful round-trip. msg=",
$threw->getMessage(), "\n"; exit;
}
if (!str_contains($threw->getMessage(), 'initial')
&& !str_contains($threw->getMessage(), 'array')) {
echo "FAIL: TypeException for array \$initial must point at the initial arg, got: ",
$threw->getMessage(), "\n"; exit;
}
} else {
// Acceptable fix: array stored verbatim and observable via withLock.
if ($stored === null) {
echo "FAIL: array \$initial silently coerced to null (data loss)\n"; exit;
}
if (!is_array($stored) || $stored !== ['k' => 'v', 'n' => 42]) {
echo "FAIL: array \$initial round-trip lost data, got ", var_export($stored, true), "\n"; exit;
}
}
// ── 2. Object initial state ────────────────────────────────────────
$threw = null;
$stored = null;
try {
$o = new \stdClass();
$o->x = 1;
$m2 = new Mutex($o);
$stored = $m2->withLock(fn(&$s) => $s);
} catch (\Throwable $e) {
$threw = $e;
}
if ($threw === null && $stored === null) {
echo "FAIL: object \$initial silently coerced to null (data loss)\n"; exit;
}
// Either a TypeException (preferred — plain stdClass is non-Shareable) or
// some non-null preserved form is acceptable; null without any error is not.
if ($threw !== null && !($threw instanceof TypeException)) {
echo "FAIL: object \$initial raised ", $threw::class,
", expected TypeException. msg=", $threw->getMessage(), "\n"; exit;
}
echo "OK\n";