Skip to content

Commit d643d0c

Browse files
committed
added test
1 parent 338e0d1 commit d643d0c

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
--TEST--
2+
Test that readonly properties behave as expected on thread-safe class descendents
3+
--DESCRIPTION--
4+
pthreads doesn't (yet) mirror the exact behaviour of readonly properties on standard objects
5+
for now, this test just ensures that the behaviour plays out as expected with lockless reads,
6+
since those use local cache without locks if possible.
7+
--SKIPIF--
8+
<?php if(PHP_VERSION_ID < 80100) die("skip readonly properties are only supported in 8.1 and up"); ?>
9+
--FILE--
10+
<?php
11+
12+
class T extends \ThreadedBase{
13+
public readonly int $a;
14+
15+
public readonly \ThreadedArray $array;
16+
17+
public readonly int $initiallyUninit;
18+
19+
public function __construct(){
20+
$this->a = 1;
21+
$this->array = new \ThreadedArray();
22+
}
23+
24+
public function unsetProperties() : void{
25+
try{
26+
unset($this->a);
27+
}catch(\Error $e){
28+
echo $e->getMessage() . PHP_EOL;
29+
}
30+
try{
31+
unset($this->array);
32+
}catch(\Error $e){
33+
echo $e->getMessage() . PHP_EOL;
34+
}
35+
try{
36+
unset($this->initiallyUninit);
37+
}catch(\Error $e){
38+
echo $e->getMessage() . PHP_EOL;
39+
}
40+
}
41+
42+
public function writeProperties() : void{
43+
try{
44+
$this->a = 2;
45+
}catch(\Error $e){
46+
echo $e->getMessage() . PHP_EOL;
47+
}
48+
try{
49+
$this->a++;
50+
}catch(\Error $e){
51+
echo $e->getMessage() . PHP_EOL;
52+
}
53+
try{
54+
$this->array = new \ThreadedArray();
55+
}catch(\Error $e){
56+
echo $e->getMessage() . PHP_EOL;
57+
}
58+
try{
59+
$this->initiallyUninit = 2;
60+
}catch(\Error $e){
61+
echo $e->getMessage() . PHP_EOL;
62+
}
63+
}
64+
65+
public function readProperties() : void{
66+
var_dump($this->a);
67+
var_dump($this->array);
68+
var_dump($this->initiallyUninit);
69+
}
70+
71+
public function issetProperties() : void{
72+
var_dump(isset($this->a));
73+
var_dump(isset($this->array));
74+
var_dump(isset($this->initiallyUninit));
75+
}
76+
}
77+
78+
function test(T $t) : void{
79+
//these must run first, to ensure they work on an unpopulated local cache
80+
$t->unsetProperties();
81+
$t->writeProperties();
82+
$t->issetProperties();
83+
$t->readProperties();
84+
}
85+
86+
echo "--- main thread start ---\n";
87+
test(new T());
88+
echo "--- main thread end ---\n";
89+
90+
//init properties from the main thread - ensure that the child thread receives an empty local cache
91+
$thread = new class(new T) extends \Thread{
92+
public function __construct(
93+
private T $t
94+
){}
95+
96+
public function run() : void{
97+
echo "--- child thread start ---\n";
98+
test($this->t);
99+
echo "--- child thread end ---\n";
100+
}
101+
};
102+
$thread->start();
103+
$thread->join();
104+
echo "OK\n";
105+
?>
106+
--EXPECTF--
107+
--- main thread start ---
108+
Cannot unset readonly property T::$a
109+
Cannot unset readonly property T::$array
110+
Cannot unset readonly property T::$initiallyUninit
111+
Cannot modify readonly property T::$a
112+
Cannot modify readonly property T::$a
113+
Cannot modify readonly property T::$array
114+
bool(true)
115+
bool(true)
116+
bool(true)
117+
int(1)
118+
object(ThreadedArray)#%d (0) {
119+
}
120+
int(2)
121+
--- main thread end ---
122+
--- child thread start ---
123+
Cannot unset readonly property T::$a
124+
Cannot unset readonly property T::$array
125+
Cannot unset readonly property T::$initiallyUninit
126+
Cannot modify readonly property T::$a
127+
Cannot modify readonly property T::$a
128+
Cannot modify readonly property T::$array
129+
bool(true)
130+
bool(true)
131+
bool(true)
132+
int(1)
133+
object(ThreadedArray)#%d (0) {
134+
}
135+
int(2)
136+
--- child thread end ---
137+
OK

0 commit comments

Comments
 (0)