Skip to content

Commit 12a880b

Browse files
authored
Merge pull request #278 from clue-labs/fds-tests
Improve test suite to skip FD test when hitting memory limit
2 parents ead9efb + 311d187 commit 12a880b

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

tests/TcpConnectorTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,33 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded()
8282

8383
$connector = new TcpConnector($loop);
8484

85-
$ulimit = exec('ulimit -n 2>&1');
86-
if ($ulimit < 1) {
85+
/** @var string[] $_ */
86+
/** @var int $exit */
87+
$ulimit = exec('ulimit -n 2>&1', $_, $exit);
88+
if ($exit !== 0 || $ulimit < 1) {
8789
$this->markTestSkipped('Unable to determine limit of open files (ulimit not available?)');
8890
}
8991

92+
$memory = ini_get('memory_limit');
93+
if ($memory === '-1') {
94+
$memory = PHP_INT_MAX;
95+
} elseif (preg_match('/^\d+G$/i', $memory)) {
96+
$memory = ((int) $memory) * 1024 * 1024 * 1024;
97+
} elseif (preg_match('/^\d+M$/i', $memory)) {
98+
$memory = ((int) $memory) * 1024 * 1024;
99+
} elseif (preg_match('/^\d+K$/i', $memory)) {
100+
$memory = ((int) $memory) * 1024;
101+
}
102+
103+
// each file descriptor takes ~600 bytes of memory, so skip test if this would exceed memory_limit
104+
if ($ulimit * 600 > $memory) {
105+
$this->markTestSkipped('Test requires ~' . round($ulimit * 600 / 1024 / 1024) . '/' . round($memory / 1024 / 1024) . ' MiB memory with ' . $ulimit . ' file descriptors');
106+
}
107+
90108
// dummy rejected promise to make sure autoloader has initialized all classes
91-
$foo = new Promise(function () { throw new \RuntimeException('dummy'); });
109+
class_exists('React\Socket\SocketServer', true);
110+
class_exists('PHPUnit\Framework\Error\Warning', true);
111+
new Promise(function () { throw new \RuntimeException('dummy'); });
92112

93113
// keep creating dummy file handles until all file descriptors are exhausted
94114
$fds = array();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy