[php-src] master: Add tests for destructors behaviour with GC
| From: | Michael Voříšek via Arnaud Le Blanc | Date: | Fri, 29 Mar 2024 15:42:56 +0000 |
| Subject: | [php-src] master: Add tests for destructors behaviour with GC | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-127877@lists.php.net to get a copy of this message | ||
Author: Michael Voříšek (mvorisek)
Committer: Arnaud Le Blanc (arnaud-lb)
Date: 2024-03-29T16:41:22+01:00
Commit: https://github.com/php/php-src/commit/a470110ff4ff7e239bdddf2005d68ed5923c18a2
Raw diff: https://github.com/php/php-src/commit/a470110ff4ff7e239bdddf2005d68ed5923c18a2.diff
Add tests for destructors behaviour with GC
Changed paths:
A Zend/tests/gc_048.phpt
A Zend/tests/gc_049.phpt
A Zend/tests/gc_050.phpt
Diff:
diff --git a/Zend/tests/gc_048.phpt b/Zend/tests/gc_048.phpt
new file mode 100644
index 000000000000..14cc5a2b0a04
--- /dev/null
+++ b/Zend/tests/gc_048.phpt
@@ -0,0 +1,58 @@
+--TEST--
+GC 048: Objects with destructor are collected without delay
+--FILE--
+<?php
+
+class CycleWithoutDestructor
+{
+ private \stdClass $cycleRef;
+
+ public function __construct()
+ {
+ $this->cycleRef = new \stdClass();
+ $this->cycleRef->x = $this;
+ }
+}
+
+class CycleWithDestructor extends CycleWithoutDestructor
+{
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ public function __destruct()
+ {
+ new CycleWithoutDestructor();
+ }
+}
+
+echo "---\n";
+
+$cycleWithoutDestructor = new CycleWithoutDestructor();
+$cycleWithoutDestructorWeak = \WeakReference::create($cycleWithoutDestructor);
+$cycleWithDestructor = new CycleWithDestructor();
+$cycleWithDestructorWeak = \WeakReference::create($cycleWithDestructor);
+gc_collect_cycles();
+
+echo "---\n";
+
+unset($cycleWithoutDestructor);
+var_dump($cycleWithoutDestructorWeak->get() !== null);
+gc_collect_cycles();
+var_dump($cycleWithoutDestructorWeak->get() !== null);
+
+echo "---\n";
+
+unset($cycleWithDestructor);
+var_dump($cycleWithDestructorWeak->get() !== null);
+gc_collect_cycles();
+var_dump($cycleWithDestructorWeak->get() !== null);
+--EXPECT--
+---
+---
+bool(true)
+bool(false)
+---
+bool(true)
+bool(false)
diff --git a/Zend/tests/gc_049.phpt b/Zend/tests/gc_049.phpt
new file mode 100644
index 000000000000..cba60b5ff5b5
--- /dev/null
+++ b/Zend/tests/gc_049.phpt
@@ -0,0 +1,68 @@
+--TEST--
+GC 049: Objects created during GC do not participate in the same collection
+--FILE--
+<?php
+
+class CycleWithDestructor
+{
+ private \Closure $destructorFx;
+
+ private \stdClass $cycleRef;
+
+ public function __construct(\Closure $destructorFx)
+ {
+ $this->destructorFx = $destructorFx;
+ $this->cycleRef = new \stdClass();
+ $this->cycleRef->x = $this;
+ }
+
+ public function __destruct()
+ {
+ ($this->destructorFx)();
+ }
+}
+
+$isSecondGcRerun = false; // https://github.com/php/php-src/commit/b58d74547f
+$createFx = static function () use (&$createFx, &$isSecondGcRerun): void {
+ $destructorFx = static function () use (&$createFx, &$isSecondGcRerun): void {
+ if (!gc_status()['running']) {
+ echo "gc shutdown\n";
+ return;
+ }
+
+ echo "gc" . ($isSecondGcRerun ? ' rerun' : '') .
"\n";
+
+ $isSecondGcRerun = !$isSecondGcRerun;
+
+ $createFx();
+ };
+
+ new CycleWithDestructor($destructorFx);
+};
+
+$createFx();
+gc_collect_cycles();
+gc_collect_cycles();
+gc_collect_cycles();
+echo "---\n";
+gc_collect_cycles();
+gc_collect_cycles();
+gc_collect_cycles();
+echo "---\n";
+?>
+--EXPECT--
+gc
+gc rerun
+gc
+gc rerun
+gc
+gc rerun
+---
+gc
+gc rerun
+gc
+gc rerun
+gc
+gc rerun
+---
+gc shutdown
diff --git a/Zend/tests/gc_050.phpt b/Zend/tests/gc_050.phpt
new file mode 100644
index 000000000000..858be7cbebd5
--- /dev/null
+++ b/Zend/tests/gc_050.phpt
@@ -0,0 +1,37 @@
+--TEST--
+GC 050: Destructor are never called twice
+--FILE--
+<?php
+
+class G
+{
+ public static $v;
+}
+
+class WithDestructor
+{
+ public function __destruct()
+ {
+ echo "d\n";
+
+ G::$v = $this;
+ }
+}
+
+$o = new WithDestructor();
+$weakO = \WeakReference::create($o);
+echo "---\n";
+unset($o);
+echo "---\n";
+var_dump($weakO->get() !== null); // verify if kept allocated
+G::$v = null;
+echo "---\n";
+var_dump($weakO->get() !== null); // verify if released
+?>
+--EXPECT--
+---
+d
+---
+bool(true)
+---
+bool(false)