RetryByClientTest.php 4.1 KB

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