AbstractTimerTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace React\Tests\EventLoop\Timer;
  3. use React\Tests\EventLoop\TestCase;
  4. abstract class AbstractTimerTest extends TestCase
  5. {
  6. abstract public function createLoop();
  7. public function testAddTimer()
  8. {
  9. // usleep is intentionally high
  10. $loop = $this->createLoop();
  11. $loop->addTimer(0.001, $this->expectCallableOnce());
  12. usleep(1000);
  13. $loop->tick();
  14. }
  15. public function testAddPeriodicTimer()
  16. {
  17. $loop = $this->createLoop();
  18. $loop->addPeriodicTimer(0.001, $this->expectCallableExactly(3));
  19. usleep(1000);
  20. $loop->tick();
  21. usleep(1000);
  22. $loop->tick();
  23. usleep(1000);
  24. $loop->tick();
  25. }
  26. public function testAddPeriodicTimerWithCancel()
  27. {
  28. $loop = $this->createLoop();
  29. $timer = $loop->addPeriodicTimer(0.001, $this->expectCallableExactly(2));
  30. usleep(1000);
  31. $loop->tick();
  32. usleep(1000);
  33. $loop->tick();
  34. $timer->cancel();
  35. usleep(1000);
  36. $loop->tick();
  37. }
  38. public function testAddPeriodicTimerCancelsItself()
  39. {
  40. $i = 0;
  41. $loop = $this->createLoop();
  42. $loop->addPeriodicTimer(0.001, function ($timer) use (&$i) {
  43. $i++;
  44. if ($i == 2) {
  45. $timer->cancel();
  46. }
  47. });
  48. usleep(1000);
  49. $loop->tick();
  50. usleep(1000);
  51. $loop->tick();
  52. usleep(1000);
  53. $loop->tick();
  54. $this->assertSame(2, $i);
  55. }
  56. public function testIsTimerActive()
  57. {
  58. $loop = $this->createLoop();
  59. $timer = $loop->addPeriodicTimer(0.001, function () {});
  60. $this->assertTrue($loop->isTimerActive($timer));
  61. $timer->cancel();
  62. $this->assertFalse($loop->isTimerActive($timer));
  63. }
  64. public function testMinimumIntervalOneMicrosecond()
  65. {
  66. $loop = $this->createLoop();
  67. $timer = $loop->addTimer(0, function () {});
  68. $this->assertEquals(0.000001, $timer->getInterval());
  69. }
  70. }