|
|
@@ -16,7 +16,6 @@ declare(strict_types=1);
|
|
|
namespace Cake\Test\TestCase\Core\Retry;
|
|
|
|
|
|
use Cake\Core\Retry\CommandRetry;
|
|
|
-use Cake\Core\Retry\RetryStrategyInterface;
|
|
|
use Cake\TestSuite\TestCase;
|
|
|
use Exception;
|
|
|
|
|
|
@@ -33,30 +32,18 @@ class CommandRetryTest extends TestCase
|
|
|
public function testRetry()
|
|
|
{
|
|
|
$count = 0;
|
|
|
- $exception = new Exception('this is failing');
|
|
|
- $action = function () use (&$count, $exception) {
|
|
|
- $count++;
|
|
|
-
|
|
|
- if ($count < 4) {
|
|
|
- throw $exception;
|
|
|
+ $action = function () use (&$count) {
|
|
|
+ if ($count < 2) {
|
|
|
+ ++$count;
|
|
|
+ throw new Exception('this is failing');
|
|
|
}
|
|
|
|
|
|
return $count;
|
|
|
};
|
|
|
|
|
|
- $strategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock();
|
|
|
- $strategy
|
|
|
- ->expects($this->exactly(3))
|
|
|
- ->method('shouldRetry')
|
|
|
- ->will($this->returnCallback(function ($e, $c) use ($exception, &$count) {
|
|
|
- $this->assertSame($e, $exception);
|
|
|
- $this->assertEquals($c + 1, $count);
|
|
|
-
|
|
|
- return true;
|
|
|
- }));
|
|
|
-
|
|
|
- $retry = new CommandRetry($strategy, 5);
|
|
|
- $this->assertEquals(4, $retry->run($action));
|
|
|
+ $strategy = new \TestApp\Database\Retry\TestRetryStrategy(true);
|
|
|
+ $retry = new CommandRetry($strategy, 2);
|
|
|
+ $this->assertEquals(2, $retry->run($action));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -66,20 +53,18 @@ class CommandRetryTest extends TestCase
|
|
|
*/
|
|
|
public function testExceedAttempts()
|
|
|
{
|
|
|
- $exception = new Exception('this is failing');
|
|
|
- $action = function () use ($exception) {
|
|
|
- throw $exception;
|
|
|
- };
|
|
|
+ $count = 0;
|
|
|
+ $action = function () use (&$count) {
|
|
|
+ if ($count < 2) {
|
|
|
+ ++$count;
|
|
|
+ throw new Exception('this is failing');
|
|
|
+ }
|
|
|
|
|
|
- $strategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock();
|
|
|
- $strategy
|
|
|
- ->expects($this->exactly(4))
|
|
|
- ->method('shouldRetry')
|
|
|
- ->will($this->returnCallback(function ($e) {
|
|
|
- return true;
|
|
|
- }));
|
|
|
+ return $count;
|
|
|
+ };
|
|
|
|
|
|
- $retry = new CommandRetry($strategy, 3);
|
|
|
+ $strategy = new \TestApp\Database\Retry\TestRetryStrategy(true);
|
|
|
+ $retry = new CommandRetry($strategy, 1);
|
|
|
$this->expectException(Exception::class);
|
|
|
$this->expectExceptionMessage('this is failing');
|
|
|
$retry->run($action);
|
|
|
@@ -92,19 +77,13 @@ class CommandRetryTest extends TestCase
|
|
|
*/
|
|
|
public function testRespectStrategy()
|
|
|
{
|
|
|
- $action = function () {
|
|
|
+ $count = 0;
|
|
|
+ $action = function () use (&$count) {
|
|
|
throw new Exception('this is failing');
|
|
|
};
|
|
|
|
|
|
- $strategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock();
|
|
|
- $strategy
|
|
|
- ->expects($this->once())
|
|
|
- ->method('shouldRetry')
|
|
|
- ->will($this->returnCallback(function () {
|
|
|
- return false;
|
|
|
- }));
|
|
|
-
|
|
|
- $retry = new CommandRetry($strategy, 3);
|
|
|
+ $strategy = new \TestApp\Database\Retry\TestRetryStrategy(false);
|
|
|
+ $retry = new CommandRetry($strategy, 2);
|
|
|
$this->expectException(Exception::class);
|
|
|
$this->expectExceptionMessage('this is failing');
|
|
|
$retry->run($action);
|