checkMemoryLimit example

class LargeQueryTest extends DriverSpecificDatabaseTestBase {

  /** * Tests truncation of messages when max_allowed_packet exception occurs. */
  public function testMaxAllowedPacketQueryTruncating(): void {
    // The max_allowed_packet value is configured per database instance.     // Retrieve the max_allowed_packet value from the current instance and     // check if PHP is configured with sufficient allowed memory to be able     // to generate a query larger than max_allowed_packet.     $max_allowed_packet = $this->connection->query('SELECT @@global.max_allowed_packet')->fetchField();
    if (!Environment::checkMemoryLimit($max_allowed_packet + (16 * 1024 * 1024))) {
      $this->markTestSkipped('The configured max_allowed_packet exceeds the php memory limit. Therefore the test is skipped.');
    }

    $long_name = str_repeat('a', $max_allowed_packet + 1);
    try {
      $this->connection->query('SELECT [name] FROM {test} WHERE [name] = :name', [':name' => $long_name]);
      $this->fail("An exception should be thrown for queries larger than 'max_allowed_packet'");
    }
    catch (DatabaseException $e) {
      // Close and re-open the connection. Otherwise we will run into error       // 2006 "MySQL server had gone away" afterwards.

  public function testCheckMemoryLimit($required$custom_memory_limit$expected) {
    $actual = Environment::checkMemoryLimit($required$custom_memory_limit);
    $this->assertEquals($expected$actual);
  }

  /** * Provides data for testCheckMemoryLimit(). * * @return array * An array of arrays, each containing the arguments for * \Drupal\Component\Utility\Environment::checkMemoryLimit(): * required and memory_limit, and the expected return value. */
  
private const MAX_EXECUTION_TIME_REQUIREMENT = 30;
    private const MEMORY_LIMIT_REQUIREMENT = '512M';
    private const OPCACHE_MEMORY_RECOMMENDATION = '256M';

    public function __construct(private readonly IniConfigReader $iniConfigReader)
    {
    }

    public function validateRequirements(RequirementsCheckCollection $checks): RequirementsCheckCollection
    {
        $checks->add($this->checkMaxExecutionTime());
        $checks->add($this->checkMemoryLimit());
        $checks->add($this->checkOpCache());

        return $checks;
    }

    private function checkMaxExecutionTime(): SystemCheck
    {
        $configuredValue = (int) $this->iniConfigReader->get('max_execution_time');

        return new SystemCheck(
            'max_execution_time',
            (
Home | Imprint | This part of the site doesn't use cookies.