class WorkAllocatorTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Test/WorkAllocatorTest.php \Drupal\Tests\Core\Test\WorkAllocatorTest

Unit tests for WorkAllocator.

@phpstan-import-type TestClassInfo from \Drupal\Core\Test\PhpUnitTestDiscovery

Attributes

#[CoversClass(WorkAllocator::class)] #[Group('Test')]

Hierarchy

Expanded class hierarchy of WorkAllocatorTest

File

core/tests/Drupal/Tests/Core/Test/WorkAllocatorTest.php, line 17

Namespace

Drupal\Tests\Core\Test
View source
class WorkAllocatorTest extends UnitTestCase {
  
  /**
   * Tests sorting tests into a single bin.
   */
  public function testSingleBin() : void {
    $path = __DIR__ . '/../../../../fixtures/test_runner/work_allocator';
    $allocator = new WorkAllocator(json_decode(file_get_contents($path . '/simple_in.json'), TRUE));
    $this->assertEquals(json_decode(file_get_contents($path . '/simple_out.json'), TRUE), $allocator->getAllocatedList());
  }
  
  /**
   * Tests that each test goes into the bin with the lowest total weight.
   *
   * The test classes only need to be existing classes, as the allocator uses
   * their names but never loads them.
   */
  public function testLeastWeightedBin() : void {
    $a = \ArrayObject::class;
    $b = \DateTime::class;
    $c = \Exception::class;
    $d = \SplQueue::class;
    $e = \SplStack::class;
    $tests = [
      $a => static::testClassInfo($a, 3),
      $b => static::testClassInfo($b, 3),
      $c => static::testClassInfo($c, 2),
      $d => static::testClassInfo($d, 1),
      $e => static::testClassInfo($e, 1, TRUE),
    ];
    $grouped = [
      'group' => $tests,
      '#slow' => [
        $e => $tests[$e],
      ],
    ];
    // The #slow test weighs 1 × 8, so the order is e (8), a (3), b (3), c (2),
    // d (1), with ties broken by class name. e goes to bin 1, a to bin 2, b
    // to bin 2 (3 < 8), c to bin 2 (6 < 8), then d to bin 1 (8 = 8, the first
    // bin wins).
    $bin1 = new WorkAllocator($grouped, 2, 1);
    $this->assertSame([
      $e,
      $d,
    ], array_keys($bin1->getAllocatedList()));
    $this->assertSame([
      1,
      2,
    ], array_column($bin1->getAllocatedList(), 'worker_sequence'));
    $bin2 = new WorkAllocator($grouped, 2, 2);
    $this->assertSame([
      $a,
      $b,
      $c,
    ], array_keys($bin2->getAllocatedList()));
    $this->assertSame([
      $e,
      $a,
      $b,
      $c,
      $d,
    ], array_keys($bin1->getSortedList()));
  }
  
  /**
   * Tests that every test is allocated to exactly one bin.
   */
  public function testEveryTestAllocatedOnce() : void {
    $grouped = json_decode(file_get_contents(__DIR__ . '/../../../../fixtures/test_runner/work_allocator/complex_in.json'), TRUE);
    $expected = array_keys(array_merge(...array_values($grouped)));
    $allocated = [];
    for ($bin = 1; $bin <= 8; $bin++) {
      $allocated = array_merge($allocated, array_keys((new WorkAllocator($grouped, 8, $bin))->getAllocatedList()));
    }
    sort($expected);
    sort($allocated);
    $this->assertSame($expected, $allocated);
  }
  
  /**
   * Builds test class information for a kernel test.
   *
   * @param class-string $class
   *   The test class.
   * @param positive-int $testsCount
   *   The number of test cases.
   * @param bool $slow
   *   Whether the test is in the '#slow' group.
   *
   * @return TestClassInfo
   *   The test class information.
   */
  protected static function testClassInfo(string $class, int $testsCount, bool $slow = FALSE) : array {
    return [
      'name' => $class,
      'group' => 'group',
      'groups' => $slow ? [
        'group',
        '#slow',
      ] : [
        'group',
      ],
      'type' => 'kernel',
      'description' => '',
      'file' => '',
      'tests_count' => $testsCount,
    ];
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalTestCase::$root protected property The Drupal root directory.
DrupalTestCase::getDrupalRoot Deprecated protected function Returns the Drupal root directory. 1
DrupalTestCase::setUpRoot final protected function Ensure that the $root property is set initially.
DrupalTestCase::skipTestWithAttribute final protected function Supports skipping tests with the Skip attribute.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
ForwardCompatibilityTrait::expectExceptionMessageIs protected function Expects an exactly matching exception message.
ForwardCompatibilityTrait::expectExceptionMessageIsOrContains protected function Expects an exception message containing a specified string.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 363
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.
WorkAllocatorTest::testClassInfo protected static function Builds test class information for a kernel test.
WorkAllocatorTest::testEveryTestAllocatedOnce public function Tests that every test is allocated to exactly one bin.
WorkAllocatorTest::testLeastWeightedBin public function Tests that each test goes into the bin with the lowest total weight.
WorkAllocatorTest::testSingleBin public function Tests sorting tests into a single bin.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.