Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test suite to skip FD test when hitting memory limit #278

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions tests/TcpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,33 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded()

$connector = new TcpConnector($loop);

$ulimit = exec('ulimit -n 2>&1');
if ($ulimit < 1) {
/** @var string[] $_ */
/** @var int $exit */
$ulimit = exec('ulimit -n 2>&1', $_, $exit);
if ($exit !== 0 || $ulimit < 1) {
$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();
Expand Down