RequestAsyncTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Feature\Request;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Result\Result;
  6. use Exception;
  7. use GuzzleHttp\Exception\RequestException;
  8. use PHPUnit\Framework\TestCase;
  9. use Psr\Http\Message\ResponseInterface;
  10. use AlibabaCloud\Client\Support\Stringy;
  11. /**
  12. * Class RequestAsyncTest
  13. *
  14. * @package AlibabaCloud\Client\Tests\Feature\Request
  15. * @coversDefaultClass \AlibabaCloud\Client\Request\RpcRequest
  16. */
  17. class RequestAsyncTest extends TestCase
  18. {
  19. /**
  20. * @throws ClientException
  21. * @throws Exception
  22. */
  23. public function testRpAsync()
  24. {
  25. AlibabaCloud::accessKeyClient(
  26. \getenv('ACCESS_KEY_ID'),
  27. \getenv('ACCESS_KEY_SECRET')
  28. )->asDefaultClient()->regionId('cn-hangzhou');
  29. $promise = AlibabaCloud::rpc()
  30. ->method('POST')
  31. ->product('Cdn')
  32. ->version('2014-11-11')
  33. ->action('DescribeCdnService')
  34. ->connectTimeout(25)
  35. ->timeout(30)
  36. ->requestAsync();
  37. $promise->then(
  38. static function (Result $result) {
  39. self::assertArrayHasKey('ChangingChargeType', $result);
  40. self::assertNotEmpty(
  41. 200,
  42. $result->getStatusCode()
  43. );
  44. return $result;
  45. },
  46. static function (RequestException $e) {
  47. self::assertEquals(
  48. 'POST',
  49. $e->getRequest()->getMethod()
  50. );
  51. }
  52. )->wait();
  53. }
  54. /**
  55. * @throws ClientException
  56. * @throws Exception
  57. */
  58. public function testRpAsyncTimeout()
  59. {
  60. AlibabaCloud::accessKeyClient(
  61. \getenv('ACCESS_KEY_ID'),
  62. \getenv('ACCESS_KEY_SECRET')
  63. )->asDefaultClient()->regionId('cn-hangzhou');
  64. $promise = AlibabaCloud::rpc()
  65. ->method('POST')
  66. ->product('Cdn')
  67. ->version('2014-11-11')
  68. ->action('DescribeCdnService')
  69. ->connectTimeout(0.001)
  70. ->timeout(30)
  71. ->requestAsync();
  72. $promise->then(
  73. static function (ResponseInterface $res) {
  74. self::assertNotEmpty(200, $res->getStatusCode());
  75. return $res;
  76. },
  77. static function (Exception $e) {
  78. self::assertTrue(Stringy::contains($e->getMessage(), 'cURL error'));
  79. }
  80. )->wait();
  81. }
  82. }