RetryByClientTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request;
  3. use Exception;
  4. use AlibabaCloud\Client\Support\Stringy;
  5. use PHPUnit\Framework\TestCase;
  6. use AlibabaCloud\Client\AlibabaCloud;
  7. use AlibabaCloud\Client\Exception\ClientException;
  8. /**
  9. * Class RetryByClientTest
  10. *
  11. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request
  12. * @coversDefaultClass \AlibabaCloud\Client\Request\RpcRequest
  13. */
  14. class RetryByClientTest extends TestCase
  15. {
  16. protected function setUp()
  17. {
  18. AlibabaCloud::forgetHistory();
  19. AlibabaCloud::rememberHistory();
  20. }
  21. /**
  22. * @throws ClientException
  23. * @throws Exception
  24. */
  25. public function testNoRetry()
  26. {
  27. AlibabaCloud::accessKeyClient(
  28. \getenv('ACCESS_KEY_ID'),
  29. \getenv('ACCESS_KEY_SECRET')
  30. )->asDefaultClient()->regionId('cn-hangzhou');
  31. try {
  32. AlibabaCloud::rpc()
  33. ->method('POST')
  34. ->product('Cdn')
  35. ->version('2014-11-11')
  36. ->action('DescribeCdnService')
  37. ->connectTimeout(0.001)
  38. ->timeout(0.001)
  39. ->request();
  40. } catch (Exception $exception) {
  41. self::assertTrue(Stringy::contains($exception->getMessage(), 'timed'));
  42. self::assertEquals(1, AlibabaCloud::countHistory());
  43. }
  44. }
  45. /**
  46. * @throws ClientException
  47. * @throws Exception
  48. */
  49. public function testRetryWithTimeout()
  50. {
  51. AlibabaCloud::accessKeyClient(
  52. \getenv('ACCESS_KEY_ID'),
  53. \getenv('ACCESS_KEY_SECRET')
  54. )->asDefaultClient()->regionId('cn-hangzhou');
  55. try {
  56. AlibabaCloud::rpc()
  57. ->method('POST')
  58. ->product('Cdn')
  59. ->version('2014-11-11')
  60. ->action('DescribeCdnService')
  61. ->connectTimeout(0.001)
  62. ->timeout(0.001)
  63. ->retryByClient(3, ['timed'])
  64. ->request();
  65. } catch (Exception $exception) {
  66. self::assertTrue(Stringy::contains($exception->getMessage(), 'timed'));
  67. self::assertEquals(4, AlibabaCloud::countHistory());
  68. }
  69. }
  70. /**
  71. * @throws ClientException
  72. * @throws Exception
  73. */
  74. public function testRetryWithCode()
  75. {
  76. AlibabaCloud::accessKeyClient(
  77. \getenv('ACCESS_KEY_ID'),
  78. \getenv('ACCESS_KEY_SECRET')
  79. )->asDefaultClient()->regionId('cn-hangzhou');
  80. try {
  81. AlibabaCloud::rpc()
  82. ->method('POST')
  83. ->product('Cdn')
  84. ->version('2014-11-11')
  85. ->action('DescribeCdnService')
  86. ->connectTimeout(0.001)
  87. ->timeout(0.001)
  88. ->retryByClient(3, [], [0])
  89. ->request();
  90. } catch (Exception $exception) {
  91. self::assertTrue(Stringy::contains($exception->getMessage(), 'timed'));
  92. self::assertEquals(4, AlibabaCloud::countHistory());
  93. }
  94. }
  95. /**
  96. * @throws ClientException
  97. * @throws Exception
  98. */
  99. public function testRetryWithFalse()
  100. {
  101. AlibabaCloud::accessKeyClient(
  102. \getenv('ACCESS_KEY_ID'),
  103. \getenv('ACCESS_KEY_SECRET')
  104. )->asDefaultClient()->regionId('cn-hangzhou');
  105. try {
  106. AlibabaCloud::rpc()
  107. ->method('POST')
  108. ->product('Cdn')
  109. ->version('2014-11-11')
  110. ->action('DescribeCdnService')
  111. ->connectTimeout(0.001)
  112. ->timeout(0.001)
  113. ->retryByClient(3, [])
  114. ->request();
  115. } catch (Exception $exception) {
  116. self::assertTrue(Stringy::contains($exception->getMessage(), 'timed'));
  117. self::assertEquals(1, AlibabaCloud::countHistory());
  118. }
  119. }
  120. }