AbstractLoopTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. <?php
  2. namespace React\Tests\EventLoop;
  3. use React\EventLoop\StreamSelectLoop;
  4. use React\EventLoop\ExtUvLoop;
  5. abstract class AbstractLoopTest extends TestCase
  6. {
  7. /**
  8. * @var \React\EventLoop\LoopInterface
  9. */
  10. protected $loop;
  11. /** @var float */
  12. private $tickTimeout;
  13. /** @var ?string */
  14. private $received;
  15. const PHP_DEFAULT_CHUNK_SIZE = 8192;
  16. /**
  17. * @before
  18. */
  19. public function setUpLoop()
  20. {
  21. // It's a timeout, don't set it too low. Travis and other CI systems are slow.
  22. $this->tickTimeout = 0.02;
  23. $this->loop = $this->createLoop();
  24. }
  25. abstract public function createLoop();
  26. public function createSocketPair()
  27. {
  28. $domain = (DIRECTORY_SEPARATOR === '\\') ? STREAM_PF_INET : STREAM_PF_UNIX;
  29. $sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
  30. foreach ($sockets as $socket) {
  31. if (function_exists('stream_set_read_buffer')) {
  32. stream_set_read_buffer($socket, 0);
  33. }
  34. }
  35. return $sockets;
  36. }
  37. public function testAddReadStreamTriggersWhenSocketReceivesData()
  38. {
  39. list ($input, $output) = $this->createSocketPair();
  40. $loop = $this->loop;
  41. $timeout = $loop->addTimer(0.1, function () use ($input, $loop) {
  42. $loop->removeReadStream($input);
  43. });
  44. $called = 0;
  45. $this->loop->addReadStream($input, function () use (&$called, $loop, $input, $timeout) {
  46. ++$called;
  47. $loop->removeReadStream($input);
  48. $loop->cancelTimer($timeout);
  49. });
  50. fwrite($output, "foo\n");
  51. $this->loop->run();
  52. $this->assertEquals(1, $called);
  53. }
  54. public function testAddReadStreamTriggersWhenSocketCloses()
  55. {
  56. list ($input, $output) = $this->createSocketPair();
  57. $loop = $this->loop;
  58. $timeout = $loop->addTimer(0.1, function () use ($input, $loop) {
  59. $loop->removeReadStream($input);
  60. });
  61. $called = 0;
  62. $this->loop->addReadStream($input, function () use (&$called, $loop, $input, $timeout) {
  63. ++$called;
  64. $loop->removeReadStream($input);
  65. $loop->cancelTimer($timeout);
  66. });
  67. fclose($output);
  68. $this->loop->run();
  69. $this->assertEquals(1, $called);
  70. }
  71. public function testAddWriteStreamTriggersWhenSocketConnectionSucceeds()
  72. {
  73. $server = stream_socket_server('127.0.0.1:0');
  74. $errno = $errstr = null;
  75. $connecting = stream_socket_client(stream_socket_get_name($server, false), $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
  76. $loop = $this->loop;
  77. $timeout = $loop->addTimer(0.1, function () use ($connecting, $loop) {
  78. $loop->removeWriteStream($connecting);
  79. });
  80. $called = 0;
  81. $this->loop->addWriteStream($connecting, function () use (&$called, $loop, $connecting, $timeout) {
  82. ++$called;
  83. $loop->removeWriteStream($connecting);
  84. $loop->cancelTimer($timeout);
  85. });
  86. $this->loop->run();
  87. $this->assertEquals(1, $called);
  88. }
  89. public function testAddWriteStreamTriggersWhenSocketConnectionRefused()
  90. {
  91. if (defined('HHVM_VERSION')) {
  92. $this->markTestSkipped('Not supported on HHVM');
  93. }
  94. // first verify the operating system actually refuses the connection and no firewall is in place
  95. // use higher timeout because Windows retires multiple times and has a noticeable delay
  96. // @link https://stackoverflow.com/questions/19440364/why-do-failed-attempts-of-socket-connect-take-1-sec-on-windows
  97. $errno = $errstr = null;
  98. if (@stream_socket_client('127.0.0.1:1', $errno, $errstr, 10.0) !== false || (defined('SOCKET_ECONNREFUSED') && $errno !== SOCKET_ECONNREFUSED)) {
  99. $this->markTestSkipped('Expected host to refuse connection, but got error ' . $errno . ': ' . $errstr);
  100. }
  101. $connecting = stream_socket_client('127.0.0.1:1', $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
  102. $loop = $this->loop;
  103. $timeout = $loop->addTimer(10.0, function () use ($connecting, $loop) {
  104. $loop->removeWriteStream($connecting);
  105. });
  106. $called = 0;
  107. $this->loop->addWriteStream($connecting, function () use (&$called, $loop, $connecting, $timeout) {
  108. ++$called;
  109. $loop->removeWriteStream($connecting);
  110. $loop->cancelTimer($timeout);
  111. });
  112. $this->loop->run();
  113. $this->assertEquals(1, $called);
  114. }
  115. public function testAddReadStream()
  116. {
  117. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  118. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  119. }
  120. list ($input, $output) = $this->createSocketPair();
  121. $this->loop->addReadStream($input, $this->expectCallableExactly(2));
  122. fwrite($output, "foo\n");
  123. $this->tickLoop($this->loop);
  124. fwrite($output, "bar\n");
  125. $this->tickLoop($this->loop);
  126. }
  127. public function testAddReadStreamIgnoresSecondCallable()
  128. {
  129. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  130. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  131. }
  132. list ($input, $output) = $this->createSocketPair();
  133. $this->loop->addReadStream($input, $this->expectCallableExactly(2));
  134. $this->loop->addReadStream($input, $this->expectCallableNever());
  135. fwrite($output, "foo\n");
  136. $this->tickLoop($this->loop);
  137. fwrite($output, "bar\n");
  138. $this->tickLoop($this->loop);
  139. }
  140. public function testAddReadStreamReceivesDataFromStreamReference()
  141. {
  142. $this->received = '';
  143. $this->subAddReadStreamReceivesDataFromStreamReference();
  144. $this->assertEquals('', $this->received);
  145. $this->assertRunFasterThan($this->tickTimeout * 2);
  146. $this->assertEquals('[hello]X', $this->received);
  147. }
  148. /**
  149. * Helper for above test. This happens in another helper method to verify
  150. * the loop keeps track of assigned stream resources (refcount).
  151. */
  152. private function subAddReadStreamReceivesDataFromStreamReference()
  153. {
  154. list ($input, $output) = $this->createSocketPair();
  155. fwrite($input, 'hello');
  156. fclose($input);
  157. $loop = $this->loop;
  158. $received =& $this->received;
  159. $loop->addReadStream($output, function ($output) use ($loop, &$received) {
  160. $chunk = fread($output, 1024);
  161. if ($chunk === '') {
  162. $received .= 'X';
  163. $loop->removeReadStream($output);
  164. fclose($output);
  165. } else {
  166. $received .= '[' . $chunk . ']';
  167. }
  168. });
  169. }
  170. public function testAddWriteStream()
  171. {
  172. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  173. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  174. }
  175. list ($input) = $this->createSocketPair();
  176. $this->loop->addWriteStream($input, $this->expectCallableExactly(2));
  177. $this->tickLoop($this->loop);
  178. $this->tickLoop($this->loop);
  179. }
  180. public function testAddWriteStreamIgnoresSecondCallable()
  181. {
  182. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  183. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  184. }
  185. list ($input) = $this->createSocketPair();
  186. $this->loop->addWriteStream($input, $this->expectCallableExactly(2));
  187. $this->loop->addWriteStream($input, $this->expectCallableNever());
  188. $this->tickLoop($this->loop);
  189. $this->tickLoop($this->loop);
  190. }
  191. public function testRemoveReadStreamInstantly()
  192. {
  193. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  194. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  195. }
  196. list ($input, $output) = $this->createSocketPair();
  197. $this->loop->addReadStream($input, $this->expectCallableNever());
  198. $this->loop->removeReadStream($input);
  199. fwrite($output, "bar\n");
  200. $this->tickLoop($this->loop);
  201. }
  202. public function testRemoveReadStreamAfterReading()
  203. {
  204. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  205. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  206. }
  207. list ($input, $output) = $this->createSocketPair();
  208. $this->loop->addReadStream($input, $this->expectCallableOnce());
  209. fwrite($output, "foo\n");
  210. $this->tickLoop($this->loop);
  211. $this->loop->removeReadStream($input);
  212. fwrite($output, "bar\n");
  213. $this->tickLoop($this->loop);
  214. }
  215. public function testRemoveWriteStreamInstantly()
  216. {
  217. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  218. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  219. }
  220. list ($input) = $this->createSocketPair();
  221. $this->loop->addWriteStream($input, $this->expectCallableNever());
  222. $this->loop->removeWriteStream($input);
  223. $this->tickLoop($this->loop);
  224. }
  225. public function testRemoveWriteStreamAfterWriting()
  226. {
  227. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  228. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  229. }
  230. list ($input) = $this->createSocketPair();
  231. $this->loop->addWriteStream($input, $this->expectCallableOnce());
  232. $this->tickLoop($this->loop);
  233. $this->loop->removeWriteStream($input);
  234. $this->tickLoop($this->loop);
  235. }
  236. public function testRemoveStreamForReadOnly()
  237. {
  238. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  239. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  240. }
  241. list ($input, $output) = $this->createSocketPair();
  242. $this->loop->addReadStream($input, $this->expectCallableNever());
  243. $this->loop->addWriteStream($output, $this->expectCallableOnce());
  244. $this->loop->removeReadStream($input);
  245. fwrite($output, "foo\n");
  246. $this->tickLoop($this->loop);
  247. }
  248. public function testRemoveStreamForWriteOnly()
  249. {
  250. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  251. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  252. }
  253. list ($input, $output) = $this->createSocketPair();
  254. fwrite($output, "foo\n");
  255. $this->loop->addReadStream($input, $this->expectCallableOnce());
  256. $this->loop->addWriteStream($output, $this->expectCallableNever());
  257. $this->loop->removeWriteStream($output);
  258. $this->tickLoop($this->loop);
  259. }
  260. public function testRemoveReadAndWriteStreamFromLoopOnceResourceClosesEndsLoop()
  261. {
  262. list($stream, $other) = $this->createSocketPair();
  263. stream_set_blocking($stream, false);
  264. stream_set_blocking($other, false);
  265. // dummy writable handler
  266. $this->loop->addWriteStream($stream, function () { });
  267. // remove stream when the stream is readable (closes)
  268. $loop = $this->loop;
  269. $loop->addReadStream($stream, function ($stream) use ($loop) {
  270. $loop->removeReadStream($stream);
  271. $loop->removeWriteStream($stream);
  272. fclose($stream);
  273. });
  274. // close other side
  275. fclose($other);
  276. $this->assertRunFasterThan($this->tickTimeout);
  277. }
  278. public function testRemoveReadAndWriteStreamFromLoopOnceResourceClosesOnEndOfFileEndsLoop()
  279. {
  280. list($stream, $other) = $this->createSocketPair();
  281. stream_set_blocking($stream, false);
  282. stream_set_blocking($other, false);
  283. // dummy writable handler
  284. $this->loop->addWriteStream($stream, function () { });
  285. // remove stream when the stream is readable (closes)
  286. $loop = $this->loop;
  287. $loop->addReadStream($stream, function ($stream) use ($loop) {
  288. $data = fread($stream, 1024);
  289. if ($data !== '') {
  290. return;
  291. }
  292. $loop->removeReadStream($stream);
  293. $loop->removeWriteStream($stream);
  294. fclose($stream);
  295. });
  296. // send data and close stream
  297. fwrite($other, str_repeat('.', static::PHP_DEFAULT_CHUNK_SIZE));
  298. $this->loop->addTimer(0.01, function () use ($other) {
  299. fclose($other);
  300. });
  301. $this->assertRunFasterThan(0.1);
  302. }
  303. public function testRemoveReadAndWriteStreamFromLoopWithClosingResourceEndsLoop()
  304. {
  305. // get only one part of the pair to ensure the other side will close immediately
  306. list($stream) = $this->createSocketPair();
  307. stream_set_blocking($stream, false);
  308. // dummy writable handler
  309. $this->loop->addWriteStream($stream, function () { });
  310. // remove stream when the stream is readable (closes)
  311. $loop = $this->loop;
  312. $loop->addReadStream($stream, function ($stream) use ($loop) {
  313. $loop->removeReadStream($stream);
  314. $loop->removeWriteStream($stream);
  315. fclose($stream);
  316. });
  317. $this->assertRunFasterThan($this->tickTimeout);
  318. }
  319. public function testRemoveInvalid()
  320. {
  321. list ($stream) = $this->createSocketPair();
  322. // remove a valid stream from the event loop that was never added in the first place
  323. $this->loop->removeReadStream($stream);
  324. $this->loop->removeWriteStream($stream);
  325. $this->assertTrue(true);
  326. }
  327. /** @test */
  328. public function emptyRunShouldSimplyReturn()
  329. {
  330. $this->assertRunFasterThan($this->tickTimeout);
  331. }
  332. /** @test */
  333. public function runShouldReturnWhenNoMoreFds()
  334. {
  335. list ($input, $output) = $this->createSocketPair();
  336. $loop = $this->loop;
  337. $this->loop->addReadStream($input, function ($stream) use ($loop) {
  338. $loop->removeReadStream($stream);
  339. });
  340. fwrite($output, "foo\n");
  341. $this->assertRunFasterThan($this->tickTimeout * 2);
  342. }
  343. /** @test */
  344. public function stopShouldStopRunningLoop()
  345. {
  346. list ($input, $output) = $this->createSocketPair();
  347. $loop = $this->loop;
  348. $this->loop->addReadStream($input, function ($stream) use ($loop) {
  349. $loop->stop();
  350. });
  351. fwrite($output, "foo\n");
  352. $this->assertRunFasterThan($this->tickTimeout * 2);
  353. }
  354. public function testStopShouldPreventRunFromBlocking()
  355. {
  356. $that = $this;
  357. $this->loop->addTimer(
  358. 1,
  359. function () use ($that) {
  360. $that->fail('Timer was executed.');
  361. }
  362. );
  363. $loop = $this->loop;
  364. $this->loop->futureTick(
  365. function () use ($loop) {
  366. $loop->stop();
  367. }
  368. );
  369. $this->assertRunFasterThan($this->tickTimeout * 2);
  370. }
  371. public function testIgnoreRemovedCallback()
  372. {
  373. // two independent streams, both should be readable right away
  374. list ($input1, $output1) = $this->createSocketPair();
  375. list ($input2, $output2) = $this->createSocketPair();
  376. $called = false;
  377. $loop = $this->loop;
  378. $loop->addReadStream($input1, function ($stream) use (& $called, $loop, $input2) {
  379. // stream1 is readable, remove stream2 as well => this will invalidate its callback
  380. $loop->removeReadStream($stream);
  381. $loop->removeReadStream($input2);
  382. $called = true;
  383. });
  384. // this callback would have to be called as well, but the first stream already removed us
  385. $that = $this;
  386. $loop->addReadStream($input2, function () use (& $called, $that) {
  387. if ($called) {
  388. $that->fail('Callback 2 must not be called after callback 1 was called');
  389. }
  390. });
  391. fwrite($output1, "foo\n");
  392. fwrite($output2, "foo\n");
  393. $loop->run();
  394. $this->assertTrue($called);
  395. }
  396. public function testFutureTickEventGeneratedByFutureTick()
  397. {
  398. $loop = $this->loop;
  399. $this->loop->futureTick(
  400. function () use ($loop) {
  401. $loop->futureTick(
  402. function () {
  403. echo 'future-tick' . PHP_EOL;
  404. }
  405. );
  406. }
  407. );
  408. $this->expectOutputString('future-tick' . PHP_EOL);
  409. $this->loop->run();
  410. }
  411. public function testFutureTick()
  412. {
  413. $called = false;
  414. $callback = function () use (&$called) {
  415. $called = true;
  416. };
  417. $this->loop->futureTick($callback);
  418. $this->assertFalse($called);
  419. $this->tickLoop($this->loop);
  420. $this->assertTrue($called);
  421. }
  422. public function testFutureTickFiresBeforeIO()
  423. {
  424. if ($this->loop instanceof ExtUvLoop && DIRECTORY_SEPARATOR === '\\') {
  425. $this->markTestIncomplete('Ticking ExtUvLoop not supported on Windows');
  426. }
  427. list ($stream) = $this->createSocketPair();
  428. $this->loop->addWriteStream(
  429. $stream,
  430. function () {
  431. echo 'stream' . PHP_EOL;
  432. }
  433. );
  434. $this->loop->futureTick(
  435. function () {
  436. echo 'future-tick' . PHP_EOL;
  437. }
  438. );
  439. $this->expectOutputString('future-tick' . PHP_EOL . 'stream' . PHP_EOL);
  440. $this->tickLoop($this->loop);
  441. }
  442. /**
  443. * @depends testFutureTickFiresBeforeIO
  444. */
  445. public function testRecursiveFutureTick()
  446. {
  447. list ($stream) = $this->createSocketPair();
  448. $loop = $this->loop;
  449. $this->loop->addWriteStream(
  450. $stream,
  451. function () use ($stream, $loop) {
  452. echo 'stream' . PHP_EOL;
  453. $loop->removeWriteStream($stream);
  454. }
  455. );
  456. $this->loop->futureTick(
  457. function () use ($loop) {
  458. echo 'future-tick-1' . PHP_EOL;
  459. $loop->futureTick(
  460. function () {
  461. echo 'future-tick-2' . PHP_EOL;
  462. }
  463. );
  464. }
  465. );
  466. $this->expectOutputString('future-tick-1' . PHP_EOL . 'stream' . PHP_EOL . 'future-tick-2' . PHP_EOL);
  467. $this->loop->run();
  468. }
  469. public function testRunWaitsForFutureTickEvents()
  470. {
  471. list ($stream) = $this->createSocketPair();
  472. $loop = $this->loop;
  473. $this->loop->addWriteStream(
  474. $stream,
  475. function () use ($stream, $loop) {
  476. $loop->removeWriteStream($stream);
  477. $loop->futureTick(
  478. function () {
  479. echo 'future-tick' . PHP_EOL;
  480. }
  481. );
  482. }
  483. );
  484. $this->expectOutputString('future-tick' . PHP_EOL);
  485. $this->loop->run();
  486. }
  487. public function testFutureTickEventGeneratedByTimer()
  488. {
  489. $loop = $this->loop;
  490. $this->loop->addTimer(
  491. 0.001,
  492. function () use ($loop) {
  493. $loop->futureTick(
  494. function () {
  495. echo 'future-tick' . PHP_EOL;
  496. }
  497. );
  498. }
  499. );
  500. $this->expectOutputString('future-tick' . PHP_EOL);
  501. $this->loop->run();
  502. }
  503. public function testRemoveSignalNotRegisteredIsNoOp()
  504. {
  505. $this->loop->removeSignal(2, function () { });
  506. $this->assertTrue(true);
  507. }
  508. /**
  509. * @requires extension pcntl
  510. * @requires function posix_kill()
  511. * @requires function posix_getpid()
  512. */
  513. public function testSignal()
  514. {
  515. if ($this->loop instanceof StreamSelectLoop && !(\function_exists('pcntl_signal') && \function_exists('pcntl_signal_dispatch'))) {
  516. $this->markTestSkipped('Signal handling with StreamSelectLoop requires pcntl_signal() and pcntl_signal_dispatch(), see also disable_functions');
  517. }
  518. $called = false;
  519. $calledShouldNot = true;
  520. $timer = $this->loop->addPeriodicTimer(1, function () {});
  521. $this->loop->addSignal(SIGUSR2, $func2 = function () use (&$calledShouldNot) {
  522. $calledShouldNot = false;
  523. });
  524. $loop = $this->loop;
  525. $this->loop->addSignal(SIGUSR1, $func1 = function () use (&$func1, &$func2, &$called, $timer, $loop) {
  526. $called = true;
  527. $loop->removeSignal(SIGUSR1, $func1);
  528. $loop->removeSignal(SIGUSR2, $func2);
  529. $loop->cancelTimer($timer);
  530. });
  531. $this->loop->futureTick(function () {
  532. posix_kill(posix_getpid(), SIGUSR1);
  533. });
  534. $this->loop->run();
  535. $this->assertTrue($called);
  536. $this->assertTrue($calledShouldNot);
  537. }
  538. /**
  539. * @requires extension pcntl
  540. */
  541. public function testSignalMultipleUsagesForTheSameListener()
  542. {
  543. if ($this->loop instanceof StreamSelectLoop && !(\function_exists('pcntl_signal') && \function_exists('pcntl_signal_dispatch'))) {
  544. $this->markTestSkipped('Signal handling with StreamSelectLoop requires pcntl_signal() and pcntl_signal_dispatch(), see also disable_functions');
  545. }
  546. $funcCallCount = 0;
  547. $func = function () use (&$funcCallCount) {
  548. $funcCallCount++;
  549. };
  550. $this->loop->addTimer(1, function () {});
  551. $this->loop->addSignal(SIGUSR1, $func);
  552. $this->loop->addSignal(SIGUSR1, $func);
  553. $this->loop->addTimer(0.4, function () {
  554. posix_kill(posix_getpid(), SIGUSR1);
  555. });
  556. $loop = $this->loop;
  557. $this->loop->addTimer(0.9, function () use (&$func, $loop) {
  558. $loop->removeSignal(SIGUSR1, $func);
  559. });
  560. $this->loop->run();
  561. $this->assertSame(1, $funcCallCount);
  562. }
  563. /**
  564. * @requires extension pcntl
  565. */
  566. public function testSignalsKeepTheLoopRunning()
  567. {
  568. if ($this->loop instanceof StreamSelectLoop && !(\function_exists('pcntl_signal') && \function_exists('pcntl_signal_dispatch'))) {
  569. $this->markTestSkipped('Signal handling with StreamSelectLoop requires pcntl_signal() and pcntl_signal_dispatch(), see also disable_functions');
  570. }
  571. $loop = $this->loop;
  572. $function = function () {};
  573. $this->loop->addSignal(SIGUSR1, $function);
  574. $this->loop->addTimer(1.5, function () use ($function, $loop) {
  575. $loop->removeSignal(SIGUSR1, $function);
  576. $loop->stop();
  577. });
  578. $this->assertRunSlowerThan(1.4);
  579. }
  580. /**
  581. * @requires extension pcntl
  582. */
  583. public function testSignalsKeepTheLoopRunningAndRemovingItStopsTheLoop()
  584. {
  585. if ($this->loop instanceof StreamSelectLoop && !(\function_exists('pcntl_signal') && \function_exists('pcntl_signal_dispatch'))) {
  586. $this->markTestSkipped('Signal handling with StreamSelectLoop requires pcntl_signal() and pcntl_signal_dispatch(), see also disable_functions');
  587. }
  588. $loop = $this->loop;
  589. $function = function () {};
  590. $this->loop->addSignal(SIGUSR1, $function);
  591. $this->loop->addTimer(1.5, function () use ($function, $loop) {
  592. $loop->removeSignal(SIGUSR1, $function);
  593. });
  594. $this->assertRunFasterThan(1.6);
  595. }
  596. public function testTimerIntervalCanBeFarInFuture()
  597. {
  598. // Maximum interval for ExtUvLoop implementation
  599. $interval = ((int) (PHP_INT_MAX / 1000)) - 1;
  600. $loop = $this->loop;
  601. // start a timer very far in the future
  602. $timer = $this->loop->addTimer($interval, function () { });
  603. $this->loop->futureTick(function () use ($timer, $loop) {
  604. $loop->cancelTimer($timer);
  605. });
  606. $this->assertRunFasterThan($this->tickTimeout);
  607. }
  608. private function assertRunSlowerThan($minInterval)
  609. {
  610. $start = microtime(true);
  611. $this->loop->run();
  612. $end = microtime(true);
  613. $interval = $end - $start;
  614. $this->assertLessThan($interval, $minInterval);
  615. }
  616. private function assertRunFasterThan($maxInterval)
  617. {
  618. $start = microtime(true);
  619. $this->loop->run();
  620. $end = microtime(true);
  621. $interval = $end - $start;
  622. $this->assertLessThan($maxInterval, $interval);
  623. }
  624. }