This commit is contained in:
gaofeng
2026-06-17 09:23:18 +08:00
commit 81ade70944
1638 changed files with 213697 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
<?php
namespace UnitTests\Retry;
use AlibabaCloud\Oss\V2\Retry;
class BackoffTest extends \PHPUnit\Framework\TestCase
{
const ATTEMPTED_CELLING = 64;
public function testEqualJitterBackoff()
{
$basedelay = 1.0;
$maxdelay = 20.0;
$r = new Retry\EqualJitterBackoff($basedelay, $maxdelay);
for ($x = 0; $x <= self::ATTEMPTED_CELLING * 2; $x++) {
$delay = $r->backoffDelay($x, new \Exception());
$this->assertLessThan( $maxdelay + 1, $delay);
$this->assertGreaterThan(0, $delay);
}
$delay = $r->backoffDelay($x, null);
$this->assertLessThan( $maxdelay + 1, $delay);
$this->assertGreaterThan(0, $delay);
}
public function testFullJitterBackoff()
{
$basedelay = 1.0;
$maxdelay = 20.0;
$r = new Retry\FullJitterBackoff($basedelay, $maxdelay);
for ($x = 0; $x <= self::ATTEMPTED_CELLING * 2; $x++) {
$delay = $r->backoffDelay($x, new \Exception());
$this->assertLessThan( $maxdelay + 1, $delay);
$this->assertGreaterThan(0, $delay);
}
$delay = $r->backoffDelay($x, null);
$this->assertLessThan( $maxdelay + 1, $delay);
$this->assertGreaterThan(0, $delay);
}
public function testFixedDelayBackoff()
{
$maxdelay = 20.0;
$r = new Retry\FixedDelayBackoff($maxdelay);
for ($x = 0; $x <= self::ATTEMPTED_CELLING * 2; $x++) {
$delay = $r->backoffDelay($x, new \Exception());
$this->assertEquals($maxdelay, $delay);
}
$delay = $r->backoffDelay($x, null);
$this->assertEquals($maxdelay, $delay);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace UnitTests\Retry;
use AlibabaCloud\Oss\V2\Retry;
use AlibabaCloud\Oss\V2\Exception;
use GuzzleHttp;
class ErrorRetryableTest extends \PHPUnit\Framework\TestCase
{
const ATTEMPTED_CELLING = 64;
public function testHTTPStatusCodeRetryable()
{
$r = new Retry\HTTPStatusCodeRetryable();
$this->assertEquals(false, $r->isErrorRetryable(new \Exception()));
$this->assertEquals(false, $r->isErrorRetryable(self::genStatusCodeError(403)));
$this->assertEquals(false, $r->isErrorRetryable(self::genStatusCodeError(405)));
$this->assertEquals(true, $r->isErrorRetryable(self::genStatusCodeError(401)));
$this->assertEquals(true, $r->isErrorRetryable(self::genStatusCodeError(408)));
$this->assertEquals(true, $r->isErrorRetryable(self::genStatusCodeError(429)));
$this->assertEquals(true, $r->isErrorRetryable(self::genStatusCodeError(500)));
$this->assertEquals(true, $r->isErrorRetryable(self::genStatusCodeError(501)));
$this->assertEquals(true, $r->isErrorRetryable(self::genStatusCodeError(599)));
}
public function testServiceErrorCodeRetryable()
{
$r = new Retry\ServiceErrorCodeRetryable();
$this->assertEquals(false, $r->isErrorRetryable(new \Exception()));
$this->assertEquals(false, $r->isErrorRetryable(self::genServiceCodeError('123')));
$this->assertEquals(true, $r->isErrorRetryable(self::genServiceCodeError('RequestTimeTooSkewed')));
$this->assertEquals(true, $r->isErrorRetryable(self::genServiceCodeError('BadRequest')));
}
public function testClientErrorCodeRetryable()
{
$r = new Retry\ClientErrorRetryable();
$this->assertEquals(false, $r->isErrorRetryable(new \Exception()));
$this->assertEquals(false, $r->isErrorRetryable(self::genServiceCodeError('123')));
$this->assertEquals(true, $r->isErrorRetryable(new Exception\CredentialsException('')));
$this->assertEquals(true, $r->isErrorRetryable(new Exception\InconsistentExecption('1', '2')));
$request = new GuzzleHttp\Psr7\Request('GET', 'http://foo.com');
$respons = new GuzzleHttp\Psr7\Response();
$this->assertEquals(true, $r->isErrorRetryable(new GuzzleHttp\Exception\BadResponseException('', $request, $respons)));
$this->assertEquals(true, $r->isErrorRetryable(new GuzzleHttp\Exception\ConnectException('', $request)));
$this->assertEquals(true, $r->isErrorRetryable(new GuzzleHttp\Exception\RequestException('', $request)));
}
public static function genStatusCodeError(int $statusCode)
{
return new Exception\ServiceException(
[
'status_code' => $statusCode,
]
);
}
public static function genServiceCodeError(string $code)
{
return new Exception\ServiceException(
[
'code' => $code,
]
);
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace UnitTests\Retry;
use AlibabaCloud\Oss\V2\Retry;
use AlibabaCloud\Oss\V2\Defaults;
use AlibabaCloud\Oss\V2\Exception;
use GuzzleHttp;
class RetryerTest extends \PHPUnit\Framework\TestCase
{
const ATTEMPTED_CELLING = 64;
public function testNopRetryer()
{
$r = new Retry\NopRetryer();
$this->assertEquals(false, $r->isErrorRetryable(new \Exception('')));
$this->assertEquals(1, $r->getMaxAttempts());
try {
$r->retryDelay(1, new \Exception(''));
$this->assertTrue(false, 'should not here');
} catch (\Exception $e) {
$this->assertEquals('NotImplemented', $e->getMessage());
}
}
public function testStandardRetryer()
{
//default
$r = new Retry\StandardRetryer();
$this->assertEquals(Defaults::MAX_ATTEMPTS, $r->getMaxAttempts());
$this->assertEquals(false, $r->isErrorRetryable(new \Exception()));
$this->assertEquals(false, $r->isErrorRetryable(ErrorRetryableTest::genStatusCodeError(403)));
$this->assertEquals(false, $r->isErrorRetryable(ErrorRetryableTest::genStatusCodeError(405)));
$this->assertEquals(true, $r->isErrorRetryable(ErrorRetryableTest::genStatusCodeError(401)));
$this->assertEquals(true, $r->isErrorRetryable(ErrorRetryableTest::genStatusCodeError(408)));
$this->assertEquals(true, $r->isErrorRetryable(ErrorRetryableTest::genStatusCodeError(429)));
$this->assertEquals(true, $r->isErrorRetryable(ErrorRetryableTest::genStatusCodeError(500)));
$this->assertEquals(true, $r->isErrorRetryable(ErrorRetryableTest::genStatusCodeError(501)));
$this->assertEquals(true, $r->isErrorRetryable(ErrorRetryableTest::genStatusCodeError(599)));
$this->assertEquals(false, $r->isErrorRetryable(ErrorRetryableTest::genServiceCodeError('123')));
$this->assertEquals(true, $r->isErrorRetryable(ErrorRetryableTest::genServiceCodeError('RequestTimeTooSkewed')));
$this->assertEquals(true, $r->isErrorRetryable(ErrorRetryableTest::genServiceCodeError('BadRequest')));
$this->assertEquals(true, $r->isErrorRetryable(new Exception\ServiceException([
'status_code' => 403,
'code' => 'RequestTimeTooSkewed',
])));
$this->assertEquals(true, $r->isErrorRetryable(new Exception\CredentialsException('')));
$this->assertEquals(true, $r->isErrorRetryable(new Exception\InconsistentExecption('1', '2')));
$request = new GuzzleHttp\Psr7\Request('GET', 'http://foo.com');
$respons = new GuzzleHttp\Psr7\Response();
$this->assertEquals(true, $r->isErrorRetryable(new GuzzleHttp\Exception\BadResponseException('', $request, $respons)));
$this->assertEquals(true, $r->isErrorRetryable(new GuzzleHttp\Exception\ConnectException('', $request)));
$this->assertEquals(true, $r->isErrorRetryable(new GuzzleHttp\Exception\RequestException('', $request)));
for ($x = 0; $x <= self::ATTEMPTED_CELLING * 2; $x++) {
$delay = $r->retryDelay($x, new \Exception());
$this->assertLessThan(Defaults::MAX_BACKOFF_S + 1, $delay);
$this->assertGreaterThan(0, $delay);
}
$delay = $r->retryDelay($x, null);
$this->assertLessThan(Defaults::MAX_BACKOFF_S + 1, $delay);
$this->assertGreaterThan(0, $delay);
}
}