function WorkAllocatorTest::testLeastWeightedBin

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.

File

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

Class

WorkAllocatorTest
Unit tests for WorkAllocator.

Namespace

Drupal\Tests\Core\Test

Code

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()));
}

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