Skip to content

Commit

Permalink
Introduced RetryChoiceListFactory to deal with parallelism issues (#91)…
Browse files Browse the repository at this point in the history
… (#92)

* Introduced RetryChoiceListFactory to deal with parallelism issues

* Fixes

* Fixes2

* Added exponential retry
  • Loading branch information
mnocon authored Oct 26, 2023
1 parent f7ea395 commit 138b37c
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/bundle/Subscriber/StartScenarioSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
use Behat\Behat\EventDispatcher\Event\BeforeScenarioTested;
use Behat\Behat\EventDispatcher\Event\ExampleTested;
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
use Facebook\WebDriver\Exception\UnknownErrorException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelInterface;

class StartScenarioSubscriber implements EventSubscriberInterface
{
private const RETRY_LIMIT = 2;

public const PRIORITY = -1000;

private KernelInterface $kernel;
Expand Down Expand Up @@ -50,6 +53,31 @@ public function resizeWindow(BeforeScenarioTested $event): void
$session->start();
}

$session->resizeWindow($this->width, $this->height);
$this->executeWithRetry(function () use ($session): void {
$session->resizeWindow($this->width, $this->height);
});
}

/**
* @template T
*
* @param callable(mixed ...$args): T $fn
*
* @return T
*/
private function executeWithRetry(callable $fn)
{
$counter = 0;
while (true) {
try {
return $fn();
} catch (UnknownErrorException $e) {
if ($counter > self::RETRY_LIMIT) {
throw $e;
}
++$counter;
usleep(100000 * 2 ** $counter);
}
}
}
}

0 comments on commit 138b37c

Please sign in to comment.