File: RetryTest.php

package info (click to toggle)
php-nrk-predis 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 9,364 kB
  • sloc: php: 77,583; xml: 50; makefile: 12
file content (154 lines) | stat: -rw-r--r-- 4,597 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

/*
 * This file is part of the Predis package.
 *
 * (c) 2009-2020 Daniele Alessandri
 * (c) 2021-2025 Till Krüss
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Predis\Retry;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Predis\Connection\ConnectionException;
use Predis\Connection\NodeConnectionInterface;
use Predis\Connection\Resource\Exception\StreamInitException;
use Predis\Retry\Strategy\EqualBackoff;
use Predis\Retry\Strategy\ExponentialBackoff;
use Predis\Retry\Strategy\NoBackoff;
use Predis\Retry\Strategy\RetryStrategyInterface;
use RuntimeException;
use Throwable;

class RetryTest extends TestCase
{
    /**
     * @group disconnected
     * @throws Throwable
     */
    #[DataProvider('strategyProvider')]
    public function testCallWithRetry(
        RetryStrategyInterface $backoffStrategy,
        int $retries,
        float $expectedExecutionTime,
        float $delta
    ) {
        $retry = new Retry($backoffStrategy, $retries);
        $retriesCount = 0;

        $callable = function () use (&$retriesCount, $retries) {
            if ($retriesCount >= $retries) {
                return;
            }

            ++$retriesCount;
            throw new StreamInitException();
        };

        $startTime = microtime(true);
        $retry->callWithRetry($callable);
        $executionTime = microtime(true) - $startTime;

        $this->assertEquals($retriesCount, $retries);
        $this->assertEqualsWithDelta($expectedExecutionTime, $executionTime, $delta);

        $retry->updateRetriesCount(10);
        $this->assertEquals(10, $retry->getRetries());
    }

    /**
     * @group disconnected
     * @return void
     */
    public function testNoRetriesOnExcludedRetryableExceptions()
    {
        $retry = new Retry(new NoBackoff(), 3, [ConnectionException::class]);
        $retriesCount = 0;
        $callCount = 0;

        $doCallable = function () use (&$callCount) {
            ++$callCount;

            if ($callCount <= 3) {
                throw new RuntimeException();
            } elseif ($callCount <= 7) {
                throw new ConnectionException(
                    $this->getMockBuilder(NodeConnectionInterface::class)->getMock()
                );
            }
            throw new StreamInitException();
        };

        $failCallable = function () use (&$retriesCount) {
            ++$retriesCount;
        };

        // Ensures that no retries happens on excluded exception.
        while ($callCount < 3) {
            try {
                $retry->callWithRetry($doCallable, $failCallable);
            } catch (Throwable $e) {
                $this->assertInstanceOf(RuntimeException::class, $e);
                $this->assertEquals(0, $retriesCount);
            }
        }

        // Ensures that retries happens on specified exception.
        try {
            $retry->callWithRetry($doCallable, $failCallable);
        } catch (Throwable $e) {
            $this->assertInstanceOf(ConnectionException::class, $e);
            $this->assertEquals(3, $retriesCount);
        }

        $retry->updateCatchableExceptions([StreamInitException::class]);

        // Ensures that retries happens on updated catchable exceptions.
        try {
            $retry->callWithRetry($doCallable, $failCallable);
        } catch (Throwable $e) {
            $this->assertInstanceOf(StreamInitException::class, $e);
            $this->assertEquals(6, $retriesCount);
        }

        $this->assertEquals(11, $callCount);
    }

    public static function strategyProvider(): array
    {
        return [
            'NoBackoff' => [
                new NoBackoff(),
                3,
                1,
                1,
            ],
            'EqualBackoff' => [
                new EqualBackoff(0.3 * 1000000),
                3,
                0.9,
                0.1,
            ],
            'ExponentialBackoff - no jitter' => [
                new ExponentialBackoff(),
                3,
                0.112,
                0.08,
            ],
            'ExponentialBackoff - with jitter' => [
                new ExponentialBackoff(
                    RetryStrategyInterface::DEFAULT_BASE,
                    RetryStrategyInterface::DEFAULT_CAP,
                    true
                ),
                3,
                0.112,
                0.112, // Theoretically, jitter==0 might happen sequentially 3 times
            ],
        ];
    }
}