From 46ed71cfc2d4a6a10ece28fdf3c37d1825df7c58 Mon Sep 17 00:00:00 2001
From: Dave Long <dave@longwaveconsulting.com>
Date: Fri, 16 Feb 2024 11:54:00 +0000
Subject: [PATCH] Issue #3420401 by alexpott, catch, Spokje, smustgrave,
 quietone, longwave: StandardPerformanceTest fails randomly on MySQL and
 consistently on Postgres

---
 .../StandardPerformanceTest.php               |  7 +++---
 .../PerformanceTestBase.php                   | 14 ++++++++++++
 .../Drupal/Tests/PerformanceTestTrait.php     | 22 +++++++++++++++++++
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php b/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php
index aef02c1f032d..7f9078a055b0 100644
--- a/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php
+++ b/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php
@@ -55,7 +55,7 @@ public function testAnonymous() {
       $this->drupalGet('');
     }, 'standardFrontPage');
     $this->assertNoJavaScript($performance_data);
-    $this->assertSame(68, $performance_data->getQueryCount());
+    $this->assertCountBetween(68, 69, $performance_data->getQueryCount());
     $this->assertSame(137, $performance_data->getCacheGetCount());
     $this->assertSame(47, $performance_data->getCacheSetCount());
     $this->assertSame(0, $performance_data->getCacheDeleteCount());
@@ -105,8 +105,7 @@ public function testLogin(): void {
       $this->submitLoginForm($account);
     });
 
-    $this->assertGreaterThanOrEqual(38, $performance_data->getQueryCount());
-    $this->assertLessThanOrEqual(40, $performance_data->getQueryCount());
+    $this->assertCountBetween(38, 43, $performance_data->getQueryCount());
     $this->assertSame(64, $performance_data->getCacheGetCount());
     $this->assertSame(1, $performance_data->getCacheSetCount());
     $this->assertSame(1, $performance_data->getCacheDeleteCount());
@@ -136,7 +135,7 @@ public function testLoginBlock(): void {
     $performance_data = $this->collectPerformanceData(function () use ($account) {
       $this->submitLoginForm($account);
     });
-    $this->assertSame(49, $performance_data->getQueryCount());
+    $this->assertCountBetween(49, 52, $performance_data->getQueryCount());
     $this->assertSame(85, $performance_data->getCacheGetCount());
     $this->assertSame(1, $performance_data->getCacheSetCount());
     $this->assertSame(1, $performance_data->getCacheDeleteCount());
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/PerformanceTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/PerformanceTestBase.php
index c656e225b50b..c3a4e6308738 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/PerformanceTestBase.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/PerformanceTestBase.php
@@ -4,6 +4,7 @@
 
 namespace Drupal\FunctionalJavascriptTests;
 
+use Drupal\Core\Database\Database;
 use Drupal\Tests\PerformanceTestTrait;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
@@ -28,6 +29,19 @@ protected function setUp(): void {
     $this->doSetUpTasks();
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function prepareEnvironment() {
+    parent::prepareEnvironment();
+    $db = Database::getConnection();
+    $test_file_name = (new \ReflectionClass($this))->getFileName();
+    $is_core_test = str_starts_with($test_file_name, DRUPAL_ROOT . DIRECTORY_SEPARATOR . 'core');
+    if ($db->databaseType() !== 'mysql' && $is_core_test) {
+      $this->markTestSkipped('Drupal core performance tests only run on MySQL');
+    }
+  }
+
   /**
    * {@inheritdoc}
    */
diff --git a/core/tests/Drupal/Tests/PerformanceTestTrait.php b/core/tests/Drupal/Tests/PerformanceTestTrait.php
index b1be194b132b..8e1d6b18b0ff 100644
--- a/core/tests/Drupal/Tests/PerformanceTestTrait.php
+++ b/core/tests/Drupal/Tests/PerformanceTestTrait.php
@@ -407,4 +407,26 @@ private function openTelemetryTracing(array $messages, string $service_name): vo
     }
   }
 
+  /**
+   * Asserts that a count is between a min and max inclusively.
+   *
+   * @param int $min
+   *   Minimum value.
+   * @param int $max
+   *   Maximum value.
+   * @param int $actual
+   *   The number to assert against.
+   *
+   * @return void
+   *
+   * @throws \PHPUnit\Framework\ExpectationFailedException
+   */
+  protected function assertCountBetween(int $min, int $max, int $actual) {
+    static::assertThat(
+      $actual,
+      static::logicalAnd(static::greaterThanOrEqual($min), static::lessThanOrEqual($max)),
+      "$actual is greater or equal to $min and is smaller or equal to $max",
+    );
+  }
+
 }
-- 
GitLab