BarrierTest.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. declare(strict_types=1);
  3. namespace tests;
  4. use PHPUnit\Framework\TestCase;
  5. use Workerman\Coroutine\Barrier;
  6. use Workerman\Coroutine;
  7. use Workerman\Timer;
  8. /**
  9. * Class FiberBarrierTest
  10. *
  11. * Tests for the Fiber Barrier implementation.
  12. */
  13. class BarrierTest extends TestCase
  14. {
  15. /**
  16. * Test that the barrier is set to null after calling wait.
  17. */
  18. public function testWaitSetsBarrierToNull()
  19. {
  20. $barrier = Barrier::create();
  21. $results = [0];
  22. Coroutine::create(function () use ($barrier, &$results) {
  23. Timer::sleep(0.1);
  24. $results[] = 1;
  25. });
  26. Coroutine::create(function () use ($barrier, &$results) {
  27. Timer::sleep(0.2);
  28. $results[] = 2;
  29. });
  30. Coroutine::create(function () use ($barrier, &$results) {
  31. Timer::sleep(0.3);
  32. $results[] = 3;
  33. });
  34. Barrier::wait($barrier);
  35. $this->assertNull($barrier, 'Barrier should be null after wait is called.');
  36. $this->assertEquals([0, 1, 2, 3], $results, 'All coroutines should have been executed.');
  37. }
  38. }