From 45fe4c75db5490ce9630dc57660944d5e9d99dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 7 Oct 2021 12:50:10 +0200 Subject: [PATCH] Improve test suite to skip FD test when hitting memory limit --- tests/TcpConnectorTest.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/TcpConnectorTest.php b/tests/TcpConnectorTest.php index 68dc3d76..784b29e4 100644 --- a/tests/TcpConnectorTest.php +++ b/tests/TcpConnectorTest.php @@ -87,8 +87,26 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded() $this->markTestSkipped('Unable to determine limit of open files (ulimit not available?)'); } + $memory = ini_get('memory_limit'); + if ($memory === '-1') { + $memory = PHP_INT_MAX; + } elseif (preg_match('/^\d+G$/i', $memory)) { + $memory = ((int) $memory) * 1024 * 1024 * 1024; + } elseif (preg_match('/^\d+M$/i', $memory)) { + $memory = ((int) $memory) * 1024 * 1024; + } elseif (preg_match('/^\d+K$/i', $memory)) { + $memory = ((int) $memory) * 1024; + } + + // each file descriptor takes ~600 bytes of memory, so skip test if this would exceed memory_limit + if ($ulimit * 600 > $memory) { + $this->markTestSkipped('Test requires ~' . round($ulimit * 600 / 1024 / 1024) . '/' . round($memory / 1024 / 1024) . ' MiB memory with ' . $ulimit . ' file descriptors'); + } + // dummy rejected promise to make sure autoloader has initialized all classes - $foo = new Promise(function () { throw new \RuntimeException('dummy'); }); + class_exists('React\Socket\SocketServer', true); + class_exists('PHPUnit\Framework\Error\Warning', true); + new Promise(function () { throw new \RuntimeException('dummy'); }); // keep creating dummy file handles until all file descriptors are exhausted $fds = array();