Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Plugins/Shard.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ public function handleArguments(array $arguments): array
self::$timeBalanced = true;
self::$shardsOutdated = $newTests !== [];
} else {
$isInCurrentShard = fn (int $key): bool => $key % $total === ($index - 1);
$testsToRun = array_values(array_filter($tests, $isInCurrentShard, ARRAY_FILTER_USE_KEY));
$testsToRun = $this->distribute($tests, $index, $total);
}

self::$shard = [
Expand Down Expand Up @@ -444,6 +443,17 @@ private function loadShardsFile(): ?array
return $data['timings'];
}

/**
* @param list<string> $tests
* @return list<string>
*/
private function distribute(array $tests, int $index, int $total): array
{
$isInCurrentShard = fn (int $key): bool => $key % $total === ($index - 1);

return array_values(array_filter($tests, $isInCurrentShard, ARRAY_FILTER_USE_KEY));
}

/**
* @param list<string> $tests
* @param array<string, float> $timings
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Plugins/Shard.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,44 @@
});
});

describe('distribute', function (): void {
it('splits tests round-robin across shards', function (): void {
$output = new BufferedOutput;
$shard = new Shard($output);

$reflection = new ReflectionClass($shard);
$method = $reflection->getMethod('distribute');

$tests = ['A', 'B', 'C', 'D', 'E', 'F'];

expect($method->invoke($shard, $tests, 1, 3))->toBe(['A', 'D'])
->and($method->invoke($shard, $tests, 2, 3))->toBe(['B', 'E'])
->and($method->invoke($shard, $tests, 3, 3))->toBe(['C', 'F']);
});

it('returns an empty list when a shard gets no tests', function (): void {
$output = new BufferedOutput;
$shard = new Shard($output);

$reflection = new ReflectionClass($shard);
$method = $reflection->getMethod('distribute');

expect($method->invoke($shard, ['A', 'B'], 3, 4))->toBe([]);
});

it('puts every test in the single shard when total is 1', function (): void {
$output = new BufferedOutput;
$shard = new Shard($output);

$reflection = new ReflectionClass($shard);
$method = $reflection->getMethod('distribute');

$tests = ['A', 'B', 'C'];

expect($method->invoke($shard, $tests, 1, 1))->toBe($tests);
});
});

describe('ensureFilterLengthIsSafe', function (): void {
it('accepts filter within length limit', function (): void {
$output = new BufferedOutput;
Expand Down