diff --git a/core/lib/Drupal/Core/EventSubscriber/ExceptionTestSiteSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ExceptionTestSiteSubscriber.php index c6fbdd0..a091b4e 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ExceptionTestSiteSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ExceptionTestSiteSubscriber.php @@ -74,10 +74,8 @@ public function on500(GetResponseForExceptionEvent $event) { */ public function onResponse(FilterResponseEvent $event) { if ($this->canAddHeaders()) { - $response = $event->getResponse(); - $memory_usage = memory_get_peak_usage(TRUE); - $response->headers->add([ - 'X-Drupal-Memory-Peak' => round($memory_usage / 1024 / 1024, 2), + $event->getResponse()->headers->add([ + 'X-Drupal-Memory-Peak' => memory_get_peak_usage(TRUE), ]); } } diff --git a/core/modules/simpletest/src/Tests/SimpleTestTest.php b/core/modules/simpletest/src/Tests/SimpleTestTest.php index 6b4d076..8d08883 100644 --- a/core/modules/simpletest/src/Tests/SimpleTestTest.php +++ b/core/modules/simpletest/src/Tests/SimpleTestTest.php @@ -221,8 +221,9 @@ function stubTest() { // This causes \Drupal\simpletest\WebTestBase::curlHeaderCallback() to fail. // Set an absurdly low limit and get a page. $old_limit = $this->memoryUsageTrigger; - $this->memoryUsageTrigger = 1; - $this->drupalGet($user->url()); + $this->memoryUsageTrigger = '1M'; + // Getting the front page will result in a redirect to /user/2. + $this->drupalGet('/'); $this->memoryUsageTrigger = $old_limit; } @@ -262,9 +263,13 @@ function confirmStubTestResults() { $this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); - $this->assertAssertion("exceeded acceptable maximum (1MB)", 'Other', 'Fail', 'WebTestBase.php', 'Drupal\simpletest\WebTestBase->curlExec()'); + // Check that we have two exceeded memory errors. One for the frontpage and + // one for the user page which the request is redirected to. + $base_url = rtrim(\Drupal::request()->getSchemeAndHttpHost() . '/' . \Drupal::request()->getBaseUrl(), '/'); + $this->assertAssertion("exceeded acceptable maximum (1M) on url $base_url/", 'Other', 'Fail', 'WebTestBase.php', 'Drupal\simpletest\WebTestBase->curlExec()'); + $this->assertAssertion("exceeded acceptable maximum (1M) on url $base_url/user/2", 'Other', 'Fail', 'WebTestBase.php', 'Drupal\simpletest\WebTestBase->curlExec()'); - $this->assertEqual('17 passes, 4 fails, 2 exceptions, 4 debug messages', $this->childTestResults['summary']); + $this->assertEqual('17 passes, 5 fails, 2 exceptions, 4 debug messages', $this->childTestResults['summary']); $this->testIds[] = $test_id = $this->getTestIdFromResults(); $this->assertTrue($test_id, 'Found test ID in results.'); diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index 44ce470..84a9411 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -11,6 +11,7 @@ use Drupal\Component\FileCache\FileCacheFactory; use Drupal\Component\Serialization\Json; use Drupal\Component\Serialization\Yaml; +use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\UrlHelper; @@ -71,9 +72,9 @@ * in it's tests. The check can be disabled for a specific tests by setting it * to FALSE. * - * @var int|FALSE + * @var string|FALSE */ - protected $memoryUsageTrigger = 64; + protected $memoryUsageTrigger = '64M'; /** * The handle of the current cURL connection. @@ -1413,10 +1414,10 @@ protected function curlHeaderCallback($curlHandler, $header) { // the header. call_user_func_array(array(&$this, 'error'), unserialize(urldecode($matches[1]))); } - if ($this->memoryUsageTrigger && preg_match('/^X-Drupal-Memory-Peak: (.*)$/', $header, $matches) && (float) $matches[1] >= $this->memoryUsageTrigger) { - $this->fail(SafeMarkup::format('Child site memory usage (@siteMB) exceeded acceptable maximum (@maxMB) on url @url', [ - '@url' => $this->getUrl(), - '@site' => $matches[1], + if ($this->memoryUsageTrigger && preg_match('/^X-Drupal-Memory-Peak: (.*)$/', $header, $matches) && (float) $matches[1] >= Bytes::toInt($this->memoryUsageTrigger)) { + $this->fail(SafeMarkup::format('Child site memory usage (@site) exceeded acceptable maximum (@max) on url @url', [ + '@url' => curl_getinfo($curlHandler, CURLINFO_EFFECTIVE_URL), + '@site' => format_size($matches[1]), '@max' => $this->memoryUsageTrigger, ])); }