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
|
<?php
declare(strict_types=1);
namespace Cepis\Tests;
use Cepis\Client;
use Cepis\NetworkException;
use Cepis\RateLimitedException;
use Cepis\ServerErrorException;
final class RetryTest extends ServerTestCase
{
public function testRetriesOn500ThenSucceeds(): void
{
$this->server->respond('/01001000', [
self::json(['erro' => 'interno'], 500),
self::json(self::SE_ADDRESS),
]);
self::assertSame('São Paulo', $this->client->lookup('01001000')->localidade);
self::assertSame(2, $this->server->countCalls('/01001000'));
}
public function testExhaustsAttemptsThenThrows(): void
{
$this->server->respond('/01001000', [self::json(['erro' => 'interno'], 503)]);
try {
$this->client->lookup('01001000');
self::fail('expected ServerErrorException');
} catch (ServerErrorException $exception) {
self::assertSame(503, $exception->status);
self::assertSame(3, $this->server->countCalls('/01001000'));
}
}
public function testMaxAttemptsOneDisablesRetry(): void
{
$client = new Client(baseUrl: $this->server->url, timeout: 5.0, maxAttempts: 1);
$this->server->respond('/01001000', [self::raw(500, '{}')]);
try {
$client->lookup('01001000');
self::fail('expected ServerErrorException');
} catch (ServerErrorException) {
self::assertSame(1, $this->server->countCalls('/01001000'));
}
}
public function test429BecomesRateLimitedWithRetryAfter(): void
{
$this->server->respond('/01001000', [self::raw(429, '{}', ['Retry-After' => '7'])]);
try {
$this->client->lookup('01001000');
self::fail('expected RateLimitedException');
} catch (RateLimitedException $exception) {
self::assertNotNull($exception->retryAfter);
self::assertEqualsWithDelta(7.0, $exception->retryAfter, 1e-9);
}
}
public function testRetryAfterAcceptsAnHttpDate(): void
{
$date = gmdate('D, d M Y H:i:s \G\M\T', time() + 4);
$this->server->respond('/01001000', [self::raw(429, '{}', ['Retry-After' => $date])]);
try {
$this->client->lookup('01001000');
self::fail('expected RateLimitedException');
} catch (RateLimitedException $exception) {
self::assertNotNull($exception->retryAfter);
self::assertGreaterThan(0.0, $exception->retryAfter);
self::assertLessThanOrEqual(5.0, $exception->retryAfter);
}
}
public function testRetryAfterHonoursTheCap(): void
{
$this->server->respond('/01001000', [self::raw(429, '{}', ['Retry-After' => '3600'])]);
$started = self::monotonic();
try {
$this->client->lookup('01001000');
self::fail('expected RateLimitedException');
} catch (RateLimitedException) {
self::assertLessThan(5.0, self::monotonic() - $started);
}
}
public function testNetworkErrorWhenNothingListens(): void
{
$client = new Client(
baseUrl: 'http://127.0.0.1:1',
timeout: 1.0,
maxAttempts: 2,
backoffBase: 0.001,
backoffMax: 0.01,
);
$this->expectException(NetworkException::class);
$client->lookup('01001000');
}
public function testTotalTimeoutBoundsTheWholeCall(): void
{
$client = new Client(
baseUrl: $this->server->url,
timeout: 5.0,
maxAttempts: 50,
backoffBase: 0.05,
backoffMax: 0.05,
totalTimeout: 0.3,
);
$this->server->respond('/01001000', [self::raw(500, '{}')]);
$started = self::monotonic();
try {
$client->lookup('01001000');
self::fail('expected ServerErrorException');
} catch (ServerErrorException) {
self::assertLessThan(5.0, self::monotonic() - $started);
self::assertLessThan(50, $this->server->countCalls('/01001000'));
}
}
}
|