start.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. use Workerman\Events\Event;
  3. use Workerman\Events\Select;
  4. use Workerman\Events\Swow;
  5. use Workerman\Events\Swoole;
  6. use Workerman\Events\Fiber;
  7. use Workerman\Timer;
  8. use Workerman\Worker;
  9. require_once __DIR__ . '/../vendor/autoload.php';
  10. $phpunitDisplayOptions = [
  11. '--colors=always',
  12. '--display-deprecations',
  13. '--display-phpunit-deprecations',
  14. '--display-errors',
  15. '--display-notices',
  16. '--display-warnings',
  17. '--display-incomplete',
  18. '--display-skipped',
  19. ];
  20. if (DIRECTORY_SEPARATOR === '/' || (!extension_loaded('swow') && !class_exists(Revolt\EventLoop::class))) {
  21. create_test_worker(function () use ($phpunitDisplayOptions) {
  22. (new PHPUnit\TextUI\Application)->run([
  23. __DIR__ . '/../vendor/bin/phpunit',
  24. ...$phpunitDisplayOptions,
  25. __DIR__ . '/ChannelTest.php',
  26. __DIR__ . '/PoolTest.php',
  27. __DIR__ . '/BarrierTest.php',
  28. __DIR__ . '/ContextTest.php',
  29. __DIR__ . '/WaitGroupTest.php',
  30. ]);
  31. }, Select::class);
  32. }
  33. if (extension_loaded('event')) {
  34. create_test_worker(function () use ($phpunitDisplayOptions) {
  35. (new PHPUnit\TextUI\Application)->run([
  36. __DIR__ . '/../vendor/bin/phpunit',
  37. ...$phpunitDisplayOptions,
  38. __DIR__ . '/ChannelTest.php',
  39. __DIR__ . '/PoolTest.php',
  40. __DIR__ . '/BarrierTest.php',
  41. __DIR__ . '/ContextTest.php',
  42. __DIR__ . '/WaitGroupTest.php',
  43. ]);
  44. }, Event::class);
  45. }
  46. if (class_exists(Revolt\EventLoop::class) && (DIRECTORY_SEPARATOR === '/' || !extension_loaded('swow'))) {
  47. create_test_worker(function () use ($phpunitDisplayOptions) {
  48. (new PHPUnit\TextUI\Application)->run([
  49. __DIR__ . '/../vendor/bin/phpunit',
  50. ...$phpunitDisplayOptions,
  51. ...glob(__DIR__ . '/*Test.php')
  52. ]);
  53. }, Fiber::class);
  54. }
  55. if (extension_loaded('Swoole')) {
  56. create_test_worker(function () use ($phpunitDisplayOptions) {
  57. (new PHPUnit\TextUI\Application)->run([
  58. __DIR__ . '/../vendor/bin/phpunit',
  59. ...$phpunitDisplayOptions,
  60. ...glob(__DIR__ . '/*Test.php')
  61. ]);
  62. }, Swoole::class);
  63. }
  64. if (extension_loaded('Swow')) {
  65. create_test_worker(function () use ($phpunitDisplayOptions) {
  66. (new PHPUnit\TextUI\Application)->run([
  67. __DIR__ . '/../vendor/bin/phpunit',
  68. ...$phpunitDisplayOptions,
  69. ...glob(__DIR__ . '/*Test.php')
  70. ]);
  71. }, Swow::class);
  72. }
  73. function create_test_worker(Closure $callable, $eventLoopClass): void
  74. {
  75. $worker = new Worker();
  76. $worker->eventLoop = $eventLoopClass;
  77. $worker->onWorkerStart = function () use ($callable, $eventLoopClass) {
  78. $fp = fopen(__FILE__, 'r+');
  79. flock($fp, LOCK_EX);
  80. echo PHP_EOL . PHP_EOL. PHP_EOL . '[TEST EVENT-LOOP: ' . basename(str_replace('\\', '/', $eventLoopClass)) . ']' . PHP_EOL;
  81. try {
  82. $callable();
  83. } catch (Throwable $e) {
  84. echo $e;
  85. } finally {
  86. flock($fp, LOCK_UN);
  87. }
  88. Timer::repeat(1, function () use ($fp) {
  89. if (flock($fp, LOCK_EX | LOCK_NB)) {
  90. if(function_exists('posix_kill')) {
  91. posix_kill(posix_getppid(), SIGINT);
  92. } else {
  93. Worker::stopAll();
  94. }
  95. }
  96. });
  97. };
  98. }
  99. Worker::runAll();