start.php 3.1 KB

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