|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Keboola\TableBackendUtils\Unit\Connection\Bigquery; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use Generator; |
| 9 | +use GuzzleHttp\Exception\RequestException; |
| 10 | +use GuzzleHttp\Psr7\Utils; |
| 11 | +use Keboola\TableBackendUtils\Connection\Bigquery\Retry; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Psr\Http\Message\RequestInterface; |
| 14 | +use Psr\Http\Message\ResponseInterface; |
| 15 | +use Psr\Log\NullLogger; |
| 16 | +use Throwable; |
| 17 | + |
| 18 | +class RetryTest extends TestCase |
| 19 | +{ |
| 20 | + private function getException(int $code, string $message = ''): Throwable |
| 21 | + { |
| 22 | + return new Exception($message, $code); |
| 23 | + } |
| 24 | + |
| 25 | + private function getRequestException(int $code, ?string $message = ''): Throwable |
| 26 | + { |
| 27 | + $response = $this->createMock(ResponseInterface::class); |
| 28 | + $response->method('getBody')->willReturn(Utils::streamFor($message)); |
| 29 | + return new RequestException( |
| 30 | + '', |
| 31 | + $this->createMock(RequestInterface::class), |
| 32 | + $response, |
| 33 | + $this->getException($code), |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return int[][] |
| 39 | + */ |
| 40 | + public function retryCodesProvider(): array |
| 41 | + { |
| 42 | + return [ |
| 43 | + [200], |
| 44 | + [210], |
| 45 | + [299], |
| 46 | + ]; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @dataProvider retryCodesProvider |
| 51 | + */ |
| 52 | + public function testSuccessResponse(int $statusCode): void |
| 53 | + { |
| 54 | + $fn = Retry::getRetryDecider(new NullLogger()); |
| 55 | + $ex = $this->getException($statusCode); |
| 56 | + $this->assertFalse($fn($ex)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return int[][] |
| 61 | + */ |
| 62 | + public function retryCodesErrorProvider(): array |
| 63 | + { |
| 64 | + return [ |
| 65 | + [429], |
| 66 | + [500], |
| 67 | + [503], |
| 68 | + ]; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @dataProvider retryCodesErrorProvider |
| 73 | + */ |
| 74 | + public function testRetryOnCodesResponse(int $statusCode): void |
| 75 | + { |
| 76 | + $fn = Retry::getRetryDecider(new NullLogger()); |
| 77 | + $ex = $this->getException($statusCode); |
| 78 | + $this->assertTrue($fn($ex)); |
| 79 | + } |
| 80 | + |
| 81 | + public function testNotJsonResponse(): void |
| 82 | + { |
| 83 | + $fn = Retry::getRetryDecider(new NullLogger()); |
| 84 | + $ex = $this->getException(418, 'not json'); |
| 85 | + $this->assertFalse($fn($ex)); |
| 86 | + } |
| 87 | + |
| 88 | + public function testNotExpectedContentResponse(): void |
| 89 | + { |
| 90 | + $fn = Retry::getRetryDecider(new NullLogger()); |
| 91 | + $ex = $this->getException(418, '{"data" : "test"}'); |
| 92 | + $this->assertFalse($fn($ex)); |
| 93 | + } |
| 94 | + |
| 95 | + public function responseContentProvider(): Generator |
| 96 | + { |
| 97 | + foreach (['Throwable', 'RequestException'] as $exceptionType) { |
| 98 | + yield 'not error response ' . ' ' . $exceptionType => [ |
| 99 | + '{"data" : "test"}', |
| 100 | + false, |
| 101 | + $exceptionType, |
| 102 | + ]; |
| 103 | + |
| 104 | + yield 'errors not array' . ' ' . $exceptionType => [ |
| 105 | + '{"error": { "errors" : "test" }}', |
| 106 | + false, |
| 107 | + $exceptionType, |
| 108 | + ]; |
| 109 | + |
| 110 | + yield 'errors empty array' . ' ' . $exceptionType => [ |
| 111 | + '{"error": { "errors" : [] }}', |
| 112 | + false, |
| 113 | + $exceptionType, |
| 114 | + ]; |
| 115 | + yield 'errors expected errors[0]' . ' ' . $exceptionType => [ |
| 116 | + '{"error": { "errors" : [{"test":"test"}] }}', |
| 117 | + false, |
| 118 | + $exceptionType, |
| 119 | + ]; |
| 120 | + |
| 121 | + yield 'errors no reason ' . $exceptionType => [ |
| 122 | + '{"error": { "errors" : [{"message":"bigquery.jobs.create"}] }}', |
| 123 | + true, |
| 124 | + $exceptionType, |
| 125 | + ]; |
| 126 | + |
| 127 | + yield 'errors no message ' . $exceptionType => [ |
| 128 | + '{"error": { "errors" : [{"reason":"userRateLimitExceeded"}] }}', |
| 129 | + true, |
| 130 | + $exceptionType, |
| 131 | + ]; |
| 132 | + |
| 133 | + yield 'unknown reason and message ' . $exceptionType => [ |
| 134 | + '{"error": { "errors" : [{"reason":"unknown","message": "unknown"}] }}', |
| 135 | + false, |
| 136 | + $exceptionType, |
| 137 | + ]; |
| 138 | + |
| 139 | + /** |
| 140 | + * @var array{error:array{ |
| 141 | + * code:int, |
| 142 | + * message:string, |
| 143 | + * status:string, |
| 144 | + * errors:array<array{ |
| 145 | + * message:string, |
| 146 | + * domain:string, |
| 147 | + * reason:string |
| 148 | + * }> |
| 149 | + * }} $json |
| 150 | + */ |
| 151 | + $json = json_decode(<<<EOD |
| 152 | +{ |
| 153 | + "error": { |
| 154 | + "code": 404, |
| 155 | + "message": "Not found: xxx", |
| 156 | + "errors": [ |
| 157 | + { |
| 158 | + "message": "Not found: xxx", |
| 159 | + "domain": "global", |
| 160 | + "reason": "notFound" |
| 161 | + } |
| 162 | + ], |
| 163 | + "status": "NOT_FOUND" |
| 164 | + } |
| 165 | +} |
| 166 | +EOD, true, 512, JSON_THROW_ON_ERROR); |
| 167 | + |
| 168 | + foreach ([ |
| 169 | + 'rateLimitExceeded', |
| 170 | + 'userRateLimitExceeded', |
| 171 | + 'backendError', |
| 172 | + 'jobRateLimitExceeded', |
| 173 | + ] as $reason) { |
| 174 | + $json['error']['errors'][0]['reason'] = $reason; |
| 175 | + yield 'retry on ' . $reason . ' ' . $exceptionType => [ |
| 176 | + json_encode($json, JSON_THROW_ON_ERROR), |
| 177 | + true, |
| 178 | + $exceptionType, |
| 179 | + ]; |
| 180 | + } |
| 181 | + |
| 182 | + $json['error']['errors'][0]['reason'] = 'unknown'; |
| 183 | + yield 'not retry on unknown reason ' . $exceptionType => [ |
| 184 | + json_encode($json, JSON_THROW_ON_ERROR), |
| 185 | + false, |
| 186 | + $exceptionType, |
| 187 | + ]; |
| 188 | + |
| 189 | + foreach ([ |
| 190 | + 'bigquery.jobs.create', |
| 191 | + //phpcs:ignore |
| 192 | + 'IAM setPolicy failed for Dataset xxxx:WORKSPACE_11111: Service account xxxx@xxxx.iam.gserviceaccount.com does not exist.', |
| 193 | + ] as $msg) { |
| 194 | + $json['error']['errors'][0]['reason'] = 'unknown'; |
| 195 | + $json['error']['errors'][0]['message'] = $msg; |
| 196 | + yield sprintf('retry on message "%s" "%s"', $msg, $exceptionType) => [ |
| 197 | + json_encode($json, JSON_THROW_ON_ERROR), |
| 198 | + true, |
| 199 | + $exceptionType, |
| 200 | + ]; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * @dataProvider responseContentProvider |
| 207 | + */ |
| 208 | + public function testResponseContent(string $json, bool $expectToRetry, string $exceptionType): void |
| 209 | + { |
| 210 | + $fn = Retry::getRetryDecider(new NullLogger()); |
| 211 | + $ex = $this->getException(418, $json); |
| 212 | + if ($exceptionType === 'RequestException') { |
| 213 | + $this->getRequestException(418, $json); |
| 214 | + } |
| 215 | + |
| 216 | + $this->assertSame($expectToRetry, $fn($ex)); |
| 217 | + } |
| 218 | +} |
0 commit comments