Skip to content

Commit

Permalink
Improve test suite to skip FD test when hitting memory limit
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Dec 4, 2021
1 parent d132fde commit 45fe4c7
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tests/TcpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 45fe4c7

Please sign in to comment.