-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced RetryChoiceListFactory to deal with parallelism issues
- Loading branch information
Showing
7 changed files
with
290 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Bundle\Behat\Form; | ||
|
||
use ErrorException; | ||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
use Symfony\Component\Form\ChoiceList\View\ChoiceListView; | ||
|
||
final class RetryChoiceListFactory implements ChoiceListFactoryInterface | ||
{ | ||
private const RETRY_LIMIT = 2; | ||
|
||
private ChoiceListFactoryInterface $choiceListFactory; | ||
|
||
public function __construct(ChoiceListFactoryInterface $choiceListFactory) | ||
{ | ||
$this->choiceListFactory = $choiceListFactory; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public function createListFromChoices(iterable $choices, callable $value = null): ChoiceListInterface | ||
{ | ||
$filter = \func_num_args() > 2 ? func_get_arg(2) : null; | ||
|
||
return $this->executeWithRetry(function () use ($choices, $value, $filter) { | ||
return $this->choiceListFactory->createListFromChoices($choices, $value, $filter); | ||
}); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null): ChoiceListInterface | ||
{ | ||
$filter = \func_num_args() > 2 ? func_get_arg(2) : null; | ||
|
||
return $this->executeWithRetry(function () use ($loader, $value, $filter) { | ||
return $this->choiceListFactory->createListFromLoader($loader, $value, $filter); | ||
}); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public function createView( | ||
ChoiceListInterface $list, | ||
$preferredChoices = null, | ||
$label = null, | ||
callable $index = null, | ||
callable $groupBy = null, | ||
$attr = null | ||
): ChoiceListView { | ||
$labelTranslationParameters = \func_num_args() > 6 ? func_get_arg(6) : []; | ||
|
||
return $this->executeWithRetry(function () use ($list, $preferredChoices, $label, $index, $groupBy, $attr, $labelTranslationParameters) { | ||
return $this->choiceListFactory->createView( | ||
$list, | ||
$preferredChoices, | ||
$label, | ||
$index, | ||
$groupBy, | ||
$attr, | ||
$labelTranslationParameters | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param callable(mixed ...$args): T $fn | ||
* | ||
* @return T | ||
*/ | ||
private function executeWithRetry(callable $fn) | ||
{ | ||
$counter = 0; | ||
while (true) { | ||
try { | ||
return $fn(); | ||
} catch (ErrorException $e) { | ||
if ($counter > self::RETRY_LIMIT) { | ||
throw $e; | ||
} | ||
++$counter; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
|
||
Ibexa\Bundle\Behat\Form\RetryChoiceListFactory: | ||
decorates: 'form.choice_list_factory' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Bundle\Behat\Form; | ||
|
||
use ErrorException; | ||
use Ibexa\Bundle\Behat\Form\RetryChoiceListFactory; | ||
use Ibexa\Tests\Bundle\Behat\Form\Stub\UnstableChoiceListFactory; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
|
||
final class RetryChoiceListFactoryTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provider | ||
*/ | ||
public function testCreateListFromChoicesSuccess(int $numberOfFails): void | ||
{ | ||
$retryChoiceListFactory = new RetryChoiceListFactory(new UnstableChoiceListFactory($numberOfFails)); | ||
self::assertEquals([], $retryChoiceListFactory->createListFromChoices([], null)->getChoices()); | ||
} | ||
|
||
/** | ||
* @dataProvider provider | ||
*/ | ||
public function testCreateListFromLoaderSuccess(int $numberOfFails): void | ||
{ | ||
$retryChoiceListFactory = new RetryChoiceListFactory(new UnstableChoiceListFactory($numberOfFails)); | ||
self::assertEquals([], $retryChoiceListFactory->createListFromLoader($this->createMock(ChoiceLoaderInterface::class))->getChoices()); | ||
} | ||
|
||
/** | ||
* @dataProvider provider | ||
*/ | ||
public function testCreateViewSuccess(int $numberOfFails): void | ||
{ | ||
$retryChoiceListFactory = new RetryChoiceListFactory(new UnstableChoiceListFactory($numberOfFails)); | ||
self::assertEquals([], $retryChoiceListFactory->createView($this->createMock(ChoiceListInterface::class), null)->choices); | ||
} | ||
|
||
public function testCreateListFromChoicesFail(): void | ||
{ | ||
$retryChoiceListFactory = new RetryChoiceListFactory(new UnstableChoiceListFactory(4)); | ||
$this->expectException(ErrorException::class); | ||
$retryChoiceListFactory->createListFromChoices([], null); | ||
} | ||
|
||
public function testCreateListFromLoaderFail(): void | ||
{ | ||
$retryChoiceListFactory = new RetryChoiceListFactory(new UnstableChoiceListFactory(4)); | ||
$this->expectException(ErrorException::class); | ||
$retryChoiceListFactory->createListFromLoader($this->createMock(ChoiceLoaderInterface::class)); | ||
} | ||
|
||
public function testCreateViewFail(): void | ||
{ | ||
$retryChoiceListFactory = new RetryChoiceListFactory(new UnstableChoiceListFactory(4)); | ||
$this->expectException(ErrorException::class); | ||
$retryChoiceListFactory->createView($this->createMock(ChoiceListInterface::class), null); | ||
} | ||
|
||
/** | ||
* @return iterable<array<string,int>> | ||
*/ | ||
public static function provider(): iterable | ||
{ | ||
yield ['No failures' => 0]; | ||
yield ['One failure' => 1]; | ||
yield ['Two failures' => 2]; | ||
yield ['Three failures' => 3]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Bundle\Behat\Form\Stub; | ||
|
||
use ErrorException; | ||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; | ||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
use Symfony\Component\Form\ChoiceList\View\ChoiceListView; | ||
|
||
final class UnstableChoiceListFactory implements ChoiceListFactoryInterface | ||
{ | ||
private int $successfulCallAfterNthTry; | ||
|
||
private int $createListFromChoicesCounter = 0; | ||
|
||
private int $createListFromLoaderCounter = 0; | ||
|
||
private int $createViewCounter = 0; | ||
|
||
public function __construct(int $successfulCallAfterNthTry) | ||
{ | ||
$this->successfulCallAfterNthTry = $successfulCallAfterNthTry; | ||
} | ||
|
||
public function createListFromChoices(iterable $choices, callable $value = null) | ||
{ | ||
++$this->createListFromChoicesCounter; | ||
$this->failIfNeeded($this->createListFromChoicesCounter); | ||
|
||
return new ArrayChoiceList([]); | ||
} | ||
|
||
public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null) | ||
{ | ||
++$this->createListFromLoaderCounter; | ||
$this->failIfNeeded($this->createListFromLoaderCounter); | ||
|
||
return new ArrayChoiceList([]); | ||
} | ||
|
||
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null) | ||
{ | ||
++$this->createViewCounter; | ||
$this->failIfNeeded($this->createViewCounter); | ||
|
||
return new ChoiceListView([]); | ||
} | ||
|
||
private function failIfNeeded(int $callNumber): void | ||
{ | ||
if ($callNumber <= $this->successfulCallAfterNthTry) { | ||
throw new ErrorException(sprintf('Failing call: %d', $callNumber)); | ||
} | ||
} | ||
} |