Skip to content

Commit 311d187

Browse files
committed
Improve test suite to skip FD test when hitting memory limit
1 parent d132fde commit 311d187

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)