Skip to content

Commit

Permalink
Merge pull request #15 from pestphp/fix/implement-timeout-calculation
Browse files Browse the repository at this point in the history
Calculate mutation test timeout based on the initial test suite duration
  • Loading branch information
gehrisandro authored Sep 13, 2024
2 parents d83a5e1 + be5e789 commit c46c13e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/MutationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use ParaTest\Options;
use Pest\Mutate\Event\Facade;
use Pest\Mutate\Plugins\Mutate;
use Pest\Mutate\Repositories\ConfigurationRepository;
use Pest\Mutate\Repositories\TelemetryRepository;
use Pest\Mutate\Support\Configuration\Configuration;
use Pest\Mutate\Support\MutationTestResult;
use Pest\Support\Container;
Expand Down Expand Up @@ -103,10 +103,11 @@ public function start(array $coveredLines, Configuration $configuration, array $

private function calculateTimeout(): int
{
// TODO: calculate a reasonable timeout
return Container::getInstance()->get(ConfigurationRepository::class)->mergedConfiguration()->parallel ? // @phpstan-ignore-line
10 :
3;
$initialTestSuiteDuration = Container::getInstance()->get(TelemetryRepository::class) // @phpstan-ignore-line
->getInitialTestSuiteDuration()
->seconds();

return $initialTestSuiteDuration + max(5, $initialTestSuiteDuration * 0.2);
}

public function hasFinished(): bool
Expand Down
22 changes: 22 additions & 0 deletions src/Repositories/TelemetryRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Pest\Mutate\Repositories;

use PHPUnit\Event\Telemetry\Duration;

class TelemetryRepository
{
private Duration $initialTestSuiteDuration;

public function initialTestSuiteDuration(Duration $duration): void
{
$this->initialTestSuiteDuration = $duration;
}

public function getInitialTestSuiteDuration(): Duration
{
return $this->initialTestSuiteDuration;
}
}
10 changes: 9 additions & 1 deletion src/Subscribers/EnsureInitialTestRunWasSuccessful.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Pest\Mutate\Subscribers;

use Pest\Mutate\Repositories\TelemetryRepository;
use Pest\Support\Container;
use PHPUnit\Event\Application\Finished;
use PHPUnit\Event\Application\FinishedSubscriber;

Expand All @@ -12,5 +14,11 @@
*/
final class EnsureInitialTestRunWasSuccessful implements FinishedSubscriber
{
public function notify(Finished $event): void {}
public function notify(Finished $event): void
{
// @phpstan-ignore-next-line
Container::getInstance()->get(TelemetryRepository::class)->initialTestSuiteDuration(
$event->telemetryInfo()->durationSinceStart()
);
}
}

0 comments on commit c46c13e

Please sign in to comment.