| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.6.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Core\Retry;
- use Cake\Core\Retry\CommandRetry;
- use Cake\Core\Retry\RetryStrategyInterface;
- use Cake\TestSuite\TestCase;
- use Exception;
- /**
- * Tests for the CommandRetry class
- */
- class CommandRetryTest extends TestCase
- {
- /**
- * Simple retry test
- *
- * @return void
- */
- public function testRetry()
- {
- $count = 0;
- $exception = new Exception('this is failing');
- $action = function () use (&$count, $exception) {
- $count++;
- if ($count < 4) {
- throw $exception;
- }
- 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));
- }
- /**
- * Test attempts exceeded
- *
- * @return void
- */
- public function testExceedAttempts()
- {
- $exception = new Exception('this is failing');
- $action = function () use ($exception) {
- throw $exception;
- };
- $strategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock();
- $strategy
- ->expects($this->exactly(4))
- ->method('shouldRetry')
- ->will($this->returnCallback(function ($e) use ($exception) {
- return true;
- }));
- $retry = new CommandRetry($strategy, 3);
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('this is failing');
- $retry->run($action);
- }
- /**
- * Test that the strategy is respected
- *
- * @return void
- */
- public function testRespectStrategy()
- {
- $action = function () {
- 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);
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('this is failing');
- $retry->run($action);
- }
- }
|