123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- <?php
- namespace React\Tests\EventLoop;
- abstract class AbstractLoopTest extends TestCase
- {
- /**
- * @var \React\EventLoop\LoopInterface
- */
- protected $loop;
- private $tickTimeout;
- public function setUp()
- {
- // HHVM is a bit slow, so give it more time
- $this->tickTimeout = defined('HHVM_VERSION') ? 0.02 : 0.005;
- $this->loop = $this->createLoop();
- }
- abstract public function createLoop();
- public function createSocketPair()
- {
- $domain = (DIRECTORY_SEPARATOR === '\\') ? STREAM_PF_INET : STREAM_PF_UNIX;
- $sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
-
- foreach ($sockets as $socket) {
- if (function_exists('stream_set_read_buffer')) {
- stream_set_read_buffer($socket, 0);
- }
- }
-
- return $sockets;
- }
- public function testAddReadStream()
- {
- list ($input, $output) = $this->createSocketPair();
- $this->loop->addReadStream($input, $this->expectCallableExactly(2));
- fwrite($output, "foo\n");
- $this->loop->tick();
- fwrite($output, "bar\n");
- $this->loop->tick();
- }
- public function testAddReadStreamIgnoresSecondCallable()
- {
- list ($input, $output) = $this->createSocketPair();
- $this->loop->addReadStream($input, $this->expectCallableExactly(2));
- $this->loop->addReadStream($input, $this->expectCallableNever());
- fwrite($output, "foo\n");
- $this->loop->tick();
- fwrite($output, "bar\n");
- $this->loop->tick();
- }
- public function testAddWriteStream()
- {
- list ($input) = $this->createSocketPair();
- $this->loop->addWriteStream($input, $this->expectCallableExactly(2));
- $this->loop->tick();
- $this->loop->tick();
- }
- public function testAddWriteStreamIgnoresSecondCallable()
- {
- list ($input) = $this->createSocketPair();
- $this->loop->addWriteStream($input, $this->expectCallableExactly(2));
- $this->loop->addWriteStream($input, $this->expectCallableNever());
- $this->loop->tick();
- $this->loop->tick();
- }
- public function testRemoveReadStreamInstantly()
- {
- list ($input, $output) = $this->createSocketPair();
- $this->loop->addReadStream($input, $this->expectCallableNever());
- $this->loop->removeReadStream($input);
- fwrite($output, "bar\n");
- $this->loop->tick();
- }
- public function testRemoveReadStreamAfterReading()
- {
- list ($input, $output) = $this->createSocketPair();
- $this->loop->addReadStream($input, $this->expectCallableOnce());
- fwrite($output, "foo\n");
- $this->loop->tick();
- $this->loop->removeReadStream($input);
- fwrite($output, "bar\n");
- $this->loop->tick();
- }
- public function testRemoveWriteStreamInstantly()
- {
- list ($input) = $this->createSocketPair();
- $this->loop->addWriteStream($input, $this->expectCallableNever());
- $this->loop->removeWriteStream($input);
- $this->loop->tick();
- }
- public function testRemoveWriteStreamAfterWriting()
- {
- list ($input) = $this->createSocketPair();
- $this->loop->addWriteStream($input, $this->expectCallableOnce());
- $this->loop->tick();
- $this->loop->removeWriteStream($input);
- $this->loop->tick();
- }
- public function testRemoveStreamInstantly()
- {
- list ($input, $output) = $this->createSocketPair();
-
- $this->loop->addReadStream($input, $this->expectCallableNever());
- $this->loop->addWriteStream($input, $this->expectCallableNever());
- $this->loop->removeStream($input);
-
- fwrite($output, "bar\n");
- $this->loop->tick();
- }
- public function testRemoveStreamForReadOnly()
- {
- list ($input, $output) = $this->createSocketPair();
- $this->loop->addReadStream($input, $this->expectCallableNever());
- $this->loop->addWriteStream($output, $this->expectCallableOnce());
- $this->loop->removeReadStream($input);
- fwrite($output, "foo\n");
- $this->loop->tick();
- }
- public function testRemoveStreamForWriteOnly()
- {
- list ($input, $output) = $this->createSocketPair();
- fwrite($output, "foo\n");
- $this->loop->addReadStream($input, $this->expectCallableOnce());
- $this->loop->addWriteStream($output, $this->expectCallableNever());
- $this->loop->removeWriteStream($output);
- $this->loop->tick();
- }
- public function testRemoveStream()
- {
- list ($input, $output) = $this->createSocketPair();
- $this->loop->addReadStream($input, $this->expectCallableOnce());
- $this->loop->addWriteStream($input, $this->expectCallableOnce());
- fwrite($output, "bar\n");
- $this->loop->tick();
- $this->loop->removeStream($input);
- fwrite($output, "bar\n");
- $this->loop->tick();
- }
- public function testRemoveInvalid()
- {
- list ($stream) = $this->createSocketPair();
- // remove a valid stream from the event loop that was never added in the first place
- $this->loop->removeReadStream($stream);
- $this->loop->removeWriteStream($stream);
- $this->loop->removeStream($stream);
- }
- /** @test */
- public function emptyRunShouldSimplyReturn()
- {
- $this->assertRunFasterThan($this->tickTimeout);
- }
- /** @test */
- public function runShouldReturnWhenNoMoreFds()
- {
- list ($input, $output) = $this->createSocketPair();
- $loop = $this->loop;
- $this->loop->addReadStream($input, function ($stream) use ($loop) {
- $loop->removeStream($stream);
- });
- fwrite($output, "foo\n");
- $this->assertRunFasterThan($this->tickTimeout * 2);
- }
- /** @test */
- public function stopShouldStopRunningLoop()
- {
- list ($input, $output) = $this->createSocketPair();
- $loop = $this->loop;
- $this->loop->addReadStream($input, function ($stream) use ($loop) {
- $loop->stop();
- });
- fwrite($output, "foo\n");
- $this->assertRunFasterThan($this->tickTimeout * 2);
- }
- public function testStopShouldPreventRunFromBlocking()
- {
- $this->loop->addTimer(
- 1,
- function () {
- $this->fail('Timer was executed.');
- }
- );
- $this->loop->nextTick(
- function () {
- $this->loop->stop();
- }
- );
- $this->assertRunFasterThan($this->tickTimeout * 2);
- }
- public function testIgnoreRemovedCallback()
- {
- // two independent streams, both should be readable right away
- list ($input1, $output1) = $this->createSocketPair();
- list ($input2, $output2) = $this->createSocketPair();
-
- $called = false;
- $loop = $this->loop;
- $loop->addReadStream($input1, function ($stream) use (& $called, $loop, $input2) {
- // stream1 is readable, remove stream2 as well => this will invalidate its callback
- $loop->removeReadStream($stream);
- $loop->removeReadStream($input2);
-
- $called = true;
- });
- // this callback would have to be called as well, but the first stream already removed us
- $loop->addReadStream($input2, function () use (& $called) {
- if ($called) {
- $this->fail('Callback 2 must not be called after callback 1 was called');
- }
- });
-
- fwrite($output1, "foo\n");
- fwrite($output2, "foo\n");
-
- $loop->run();
-
- $this->assertTrue($called);
- }
- public function testNextTick()
- {
- $called = false;
- $callback = function ($loop) use (&$called) {
- $this->assertSame($this->loop, $loop);
- $called = true;
- };
- $this->loop->nextTick($callback);
- $this->assertFalse($called);
- $this->loop->tick();
- $this->assertTrue($called);
- }
- public function testNextTickFiresBeforeIO()
- {
- list ($stream) = $this->createSocketPair();
- $this->loop->addWriteStream(
- $stream,
- function () {
- echo 'stream' . PHP_EOL;
- }
- );
- $this->loop->nextTick(
- function () {
- echo 'next-tick' . PHP_EOL;
- }
- );
- $this->expectOutputString('next-tick' . PHP_EOL . 'stream' . PHP_EOL);
- $this->loop->tick();
- }
- public function testRecursiveNextTick()
- {
- list ($stream) = $this->createSocketPair();
- $this->loop->addWriteStream(
- $stream,
- function () {
- echo 'stream' . PHP_EOL;
- }
- );
- $this->loop->nextTick(
- function () {
- $this->loop->nextTick(
- function () {
- echo 'next-tick' . PHP_EOL;
- }
- );
- }
- );
- $this->expectOutputString('next-tick' . PHP_EOL . 'stream' . PHP_EOL);
- $this->loop->tick();
- }
- public function testRunWaitsForNextTickEvents()
- {
- list ($stream) = $this->createSocketPair();
- $this->loop->addWriteStream(
- $stream,
- function () use ($stream) {
- $this->loop->removeStream($stream);
- $this->loop->nextTick(
- function () {
- echo 'next-tick' . PHP_EOL;
- }
- );
- }
- );
- $this->expectOutputString('next-tick' . PHP_EOL);
- $this->loop->run();
- }
- public function testNextTickEventGeneratedByFutureTick()
- {
- list ($stream) = $this->createSocketPair();
- $this->loop->futureTick(
- function () {
- $this->loop->nextTick(
- function () {
- echo 'next-tick' . PHP_EOL;
- }
- );
- }
- );
- $this->expectOutputString('next-tick' . PHP_EOL);
- $this->loop->run();
- }
- public function testNextTickEventGeneratedByTimer()
- {
- $this->loop->addTimer(
- 0.001,
- function () {
- $this->loop->nextTick(
- function () {
- echo 'next-tick' . PHP_EOL;
- }
- );
- }
- );
- $this->expectOutputString('next-tick' . PHP_EOL);
- $this->loop->run();
- }
- public function testFutureTick()
- {
- $called = false;
- $callback = function ($loop) use (&$called) {
- $this->assertSame($this->loop, $loop);
- $called = true;
- };
- $this->loop->futureTick($callback);
- $this->assertFalse($called);
- $this->loop->tick();
- $this->assertTrue($called);
- }
- public function testFutureTickFiresBeforeIO()
- {
- list ($stream) = $this->createSocketPair();
- $this->loop->addWriteStream(
- $stream,
- function () {
- echo 'stream' . PHP_EOL;
- }
- );
- $this->loop->futureTick(
- function () {
- echo 'future-tick' . PHP_EOL;
- }
- );
- $this->expectOutputString('future-tick' . PHP_EOL . 'stream' . PHP_EOL);
- $this->loop->tick();
- }
- public function testRecursiveFutureTick()
- {
- list ($stream) = $this->createSocketPair();
- $this->loop->addWriteStream(
- $stream,
- function () use ($stream) {
- echo 'stream' . PHP_EOL;
- $this->loop->removeWriteStream($stream);
- }
- );
- $this->loop->futureTick(
- function () {
- echo 'future-tick-1' . PHP_EOL;
- $this->loop->futureTick(
- function () {
- echo 'future-tick-2' . PHP_EOL;
- }
- );
- }
- );
- $this->expectOutputString('future-tick-1' . PHP_EOL . 'stream' . PHP_EOL . 'future-tick-2' . PHP_EOL);
- $this->loop->run();
- }
- public function testRunWaitsForFutureTickEvents()
- {
- list ($stream) = $this->createSocketPair();
- $this->loop->addWriteStream(
- $stream,
- function () use ($stream) {
- $this->loop->removeStream($stream);
- $this->loop->futureTick(
- function () {
- echo 'future-tick' . PHP_EOL;
- }
- );
- }
- );
- $this->expectOutputString('future-tick' . PHP_EOL);
- $this->loop->run();
- }
- public function testFutureTickEventGeneratedByNextTick()
- {
- list ($stream) = $this->createSocketPair();
- $this->loop->nextTick(
- function () {
- $this->loop->futureTick(
- function () {
- echo 'future-tick' . PHP_EOL;
- }
- );
- }
- );
- $this->expectOutputString('future-tick' . PHP_EOL);
- $this->loop->run();
- }
- public function testFutureTickEventGeneratedByTimer()
- {
- $this->loop->addTimer(
- 0.001,
- function () {
- $this->loop->futureTick(
- function () {
- echo 'future-tick' . PHP_EOL;
- }
- );
- }
- );
- $this->expectOutputString('future-tick' . PHP_EOL);
- $this->loop->run();
- }
- private function assertRunFasterThan($maxInterval)
- {
- $start = microtime(true);
- $this->loop->run();
- $end = microtime(true);
- $interval = $end - $start;
- $this->assertLessThan($maxInterval, $interval);
- }
- }
|