PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/React/Tests/EventLoop/AbstractLoopTest.php

https://github.com/jurajseffer/react
PHP | 207 lines | 149 code | 51 blank | 7 comment | 0 complexity | b529a9ba9e4e72e516e0f91d215090cb MD5 | raw file
  1. <?php
  2. namespace React\Tests\EventLoop;
  3. use React\Tests\Socket\TestCase;
  4. abstract class AbstractLoopTest extends TestCase
  5. {
  6. protected $loop;
  7. public function setUp()
  8. {
  9. $this->loop = $this->createLoop();
  10. }
  11. abstract public function createLoop();
  12. public function testAddReadStream()
  13. {
  14. $input = fopen('php://temp', 'r+');
  15. $this->loop->addReadStream($input, $this->expectCallableExactly(2));
  16. fwrite($input, "foo\n");
  17. rewind($input);
  18. $this->loop->tick();
  19. fwrite($input, "bar\n");
  20. rewind($input);
  21. $this->loop->tick();
  22. }
  23. public function testAddWriteStream()
  24. {
  25. $input = fopen('php://temp', 'r+');
  26. $this->loop->addWriteStream($input, $this->expectCallableExactly(2));
  27. $this->loop->tick();
  28. $this->loop->tick();
  29. }
  30. public function testRemoveReadStreamInstantly()
  31. {
  32. $input = fopen('php://temp', 'r+');
  33. $this->loop->addReadStream($input, $this->expectCallableNever());
  34. $this->loop->removeReadStream($input);
  35. fwrite($input, "bar\n");
  36. rewind($input);
  37. $this->loop->tick();
  38. }
  39. public function testRemoveReadStreamAfterReading()
  40. {
  41. $input = fopen('php://temp', 'r+');
  42. $this->loop->addReadStream($input, $this->expectCallableOnce());
  43. fwrite($input, "foo\n");
  44. rewind($input);
  45. $this->loop->tick();
  46. $this->loop->removeReadStream($input);
  47. fwrite($input, "bar\n");
  48. rewind($input);
  49. $this->loop->tick();
  50. }
  51. public function testRemoveWriteStreamInstantly()
  52. {
  53. $input = fopen('php://temp', 'r+');
  54. $this->loop->addWriteStream($input, $this->expectCallableNever());
  55. $this->loop->removeWriteStream($input);
  56. $this->loop->tick();
  57. }
  58. public function testRemoveWriteStreamAfterWriting()
  59. {
  60. $input = fopen('php://temp', 'r+');
  61. $this->loop->addWriteStream($input, $this->expectCallableOnce());
  62. $this->loop->tick();
  63. $this->loop->removeWriteStream($input);
  64. $this->loop->tick();
  65. }
  66. public function testRemoveStreamInstantly()
  67. {
  68. $input = fopen('php://temp', 'r+');
  69. $this->loop->addReadStream($input, $this->expectCallableNever());
  70. $this->loop->addWriteStream($input, $this->expectCallableNever());
  71. $this->loop->removeStream($input);
  72. fwrite($input, "bar\n");
  73. rewind($input);
  74. $this->loop->tick();
  75. }
  76. public function testRemoveStream()
  77. {
  78. $input = fopen('php://temp', 'r+');
  79. $this->loop->addReadStream($input, $this->expectCallableOnce());
  80. $this->loop->addWriteStream($input, $this->expectCallableOnce());
  81. fwrite($input, "bar\n");
  82. rewind($input);
  83. $this->loop->tick();
  84. $this->loop->removeStream($input);
  85. fwrite($input, "bar\n");
  86. rewind($input);
  87. $this->loop->tick();
  88. }
  89. public function testRemoveInvalid()
  90. {
  91. $stream = fopen('php://temp', 'r+');
  92. // remove a valid stream from the event loop that was never added in the first place
  93. $this->loop->removeReadStream($stream);
  94. $this->loop->removeWriteStream($stream);
  95. $this->loop->removeStream($stream);
  96. }
  97. /** @test */
  98. public function emptyRunShouldSimplyReturn()
  99. {
  100. $this->assertRunFasterThan(0.005);
  101. }
  102. /** @test */
  103. public function runShouldReturnWhenNoMoreFds()
  104. {
  105. $input = fopen('php://temp', 'r+');
  106. $loop = $this->loop;
  107. $this->loop->addReadStream($input, function ($stream) use ($loop) {
  108. $loop->removeStream($stream);
  109. });
  110. fwrite($input, "foo\n");
  111. rewind($input);
  112. $this->assertRunFasterThan(0.005);
  113. }
  114. /** @test */
  115. public function stopShouldStopRunningLoop()
  116. {
  117. $input = fopen('php://temp', 'r+');
  118. $loop = $this->loop;
  119. $this->loop->addReadStream($input, function ($stream) use ($loop) {
  120. $loop->stop();
  121. });
  122. fwrite($input, "foo\n");
  123. rewind($input);
  124. $this->assertRunFasterThan(0.005);
  125. }
  126. public function testIgnoreRemovedCallback()
  127. {
  128. // two independent streams, both should be readable right away
  129. $stream1 = fopen('php://temp', 'r+');
  130. $stream2 = fopen('php://temp', 'r+');
  131. $loop = $this->loop;
  132. $loop->addReadStream($stream1, function ($stream) use ($loop, $stream2) {
  133. // stream1 is readable, remove stream2 as well => this will invalidate its callback
  134. $loop->removeReadStream($stream);
  135. $loop->removeReadStream($stream2);
  136. });
  137. $loop->addReadStream($stream2, function ($stream) use ($loop, $stream1) {
  138. // this callback would have to be called as well, but the first stream already removed us
  139. $loop->removeReadStream($stream);
  140. $loop->removeReadStream($stream1);
  141. });
  142. fwrite($stream1, "foo\n");
  143. rewind($stream1);
  144. fwrite($stream2, "foo\n");
  145. rewind($stream2);
  146. $loop->run();
  147. }
  148. private function assertRunFasterThan($maxInterval)
  149. {
  150. $start = microtime(true);
  151. $this->loop->run();
  152. $end = microtime(true);
  153. $interval = $end - $start;
  154. $this->assertLessThan($maxInterval, $interval);
  155. }
  156. }