TestSwool.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. define('APP_ID', 'test');
  4. define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
  5. require_once(BASE_ROOT_PATH . '/global.php');
  6. require_once(BASE_CORE_PATH . '/lrlz.php');
  7. require_once(BASE_ROOT_PATH . '/fooder.php');
  8. class TestSwool extends TestCase
  9. {
  10. public static function setUpBeforeClass() : void
  11. {
  12. Base::run_util();
  13. }
  14. public function testCurl()
  15. {
  16. Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
  17. Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_CURL);
  18. $n = 100;
  19. $i = 0;
  20. while($n--)
  21. {
  22. go(function () use($i) {
  23. Log::record("start {$i}",Log::DEBUG);
  24. $ch = curl_init();
  25. curl_setopt($ch, CURLOPT_URL, "http://www.xinhuanet.com/");
  26. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  27. curl_setopt($ch, CURLOPT_HEADER, 0);
  28. $output = curl_exec($ch);
  29. if ($output === FALSE) {
  30. Log::record("index = {$i} CURL Error:" . curl_error($ch),Log::DEBUG);
  31. }
  32. curl_close($ch);
  33. Log::record("index={$i} length=" . strlen($output) . " bytes",Log::DEBUG);
  34. });
  35. $i++;
  36. Coroutine::sleep(.1);
  37. }
  38. swoole_event_wait();
  39. }
  40. }