Issues (55)

tests/Unit/Middleware/RetryMiddlewareTest.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * This file is part of the tarantool/client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Tests\Unit\Middleware;
15
16
use PHPUnit\Framework\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
use Tarantool\Client\Exception\CommunicationFailed;
19
use Tarantool\Client\Exception\ConnectionFailed;
20
use Tarantool\Client\Handler\Handler;
21
use Tarantool\Client\Middleware\RetryMiddleware;
22
use Tarantool\Client\Request\Request;
23
use Tarantool\PhpUnit\Client\TestDoubleFactory;
24
25
final class RetryMiddlewareTest extends TestCase
26
{
27
    /**
28
     * @var Request|MockObject
29
     */
30
    private $request;
31
32
    /**
33
     * @var Handler|MockObject
34
     */
35
    private $handler;
36
37
    protected function setUp() : void
38
    {
39
        $this->request = $this->createMock(Request::class);
40
        $this->handler = $this->createMock(Handler::class);
41
    }
42
43
    public function testSuccessfulRetry() : void
44
    {
45
        $response = TestDoubleFactory::createEmptyResponse();
46
47
        $this->handler->expects(self::exactly(3))->method('handle')
48
            ->will(self::onConsecutiveCalls(
49
                self::throwException(new ConnectionFailed('unsuccessful op #1')),
50
                self::throwException(new CommunicationFailed('unsuccessful op #2')),
51
                $response
52
            ));
53
54
        $middleware = RetryMiddleware::custom(static function () : int {
55
            return 0;
56
        });
57
58
        self::assertSame($response, $middleware->process($this->request, $this->handler));
59
    }
60
61
    public function testUnsuccessfulRetries() : void
62
    {
63
        $this->handler->expects(self::exactly(4))->method('handle')
64
            ->willThrowException(new ConnectionFailed());
65
66
        $middleware = RetryMiddleware::custom(static function (int $retries) : ?int {
67
            return $retries <= 3 ? 0 : null;
68
        });
69
70
        $this->expectException(ConnectionFailed::class);
71
        $middleware->process($this->request, $this->handler);
72
    }
73
74
    public function testInfiniteRetriesBreak() : void
75
    {
76
        $this->handler->method('handle')
0 ignored issues
show
The method method() does not exist on Tarantool\Client\Handler\Handler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $this->handler->/** @scrutinizer ignore-call */ 
77
                        method('handle')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            ->willThrowException(new ConnectionFailed());
78
79
        $totalRetries = 0;
80
        $middleware = RetryMiddleware::custom(static function (int $retries) use (&$totalRetries) : int {
81
            $totalRetries = $retries;
82
            // always returning a value other than null
83
            // leads to an infinite retry loop
84
            return 0;
85
        });
86
87
        try {
88
            $middleware->process($this->request, $this->handler);
89
            self::fail(sprintf('"%s" exception was not thrown', ConnectionFailed::class));
90
        } catch (ConnectionFailed $e) {
91
            self::assertSame(10, $totalRetries);
92
        }
93
    }
94
}
95