123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <?php
- namespace React\Tests\EventLoop;
- use React\EventLoop\Factory;
- use React\EventLoop\Loop;
- final class LoopTest extends TestCase
- {
- /**
- * @dataProvider numberOfTests
- */
- public function testFactoryCreateSetsEventLoopOnLoopAccessor()
- {
- $factoryLoop = Factory::create();
- $accessorLoop = Loop::get();
- self::assertSame($factoryLoop, $accessorLoop);
- }
- /**
- * @dataProvider numberOfTests
- */
- public function testCallingFactoryAfterCallingLoopGetYieldsADifferentInstanceOfTheEventLoop()
- {
- // Note that this behavior isn't wise and highly advised against. Always used Loop::get.
- $accessorLoop = Loop::get();
- $factoryLoop = Factory::create();
- self::assertNotSame($factoryLoop, $accessorLoop);
- }
- /**
- * @dataProvider numberOfTests
- */
- public function testCallingLoopGetShouldAlwaysReturnTheSameEventLoop()
- {
- self::assertSame(Loop::get(), Loop::get());
- }
- /**
- * Run several tests several times to ensure we reset the loop between tests and code is still behavior as expected.
- *
- * @return array<array>
- */
- public function numberOfTests()
- {
- return array(array(), array(), array());
- }
- public function testStaticAddReadStreamCallsAddReadStreamOnLoopInstance()
- {
- $stream = tmpfile();
- $listener = function () { };
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('addReadStream')->with($stream, $listener);
- Loop::set($loop);
- Loop::addReadStream($stream, $listener);
- }
- public function testStaticAddReadStreamWithNoDefaultLoopCallsAddReadStreamOnNewLoopInstance()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $stream = stream_socket_server('127.0.0.1:0');
- $listener = function () { };
- Loop::addReadStream($stream, $listener);
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $ref->getValue());
- }
- public function testStaticAddWriteStreamCallsAddWriteStreamOnLoopInstance()
- {
- $stream = tmpfile();
- $listener = function () { };
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('addWriteStream')->with($stream, $listener);
- Loop::set($loop);
- Loop::addWriteStream($stream, $listener);
- }
- public function testStaticAddWriteStreamWithNoDefaultLoopCallsAddWriteStreamOnNewLoopInstance()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $stream = stream_socket_server('127.0.0.1:0');
- $listener = function () { };
- Loop::addWriteStream($stream, $listener);
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $ref->getValue());
- }
- public function testStaticRemoveReadStreamCallsRemoveReadStreamOnLoopInstance()
- {
- $stream = tmpfile();
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('removeReadStream')->with($stream);
- Loop::set($loop);
- Loop::removeReadStream($stream);
- }
- public function testStaticRemoveReadStreamWithNoDefaultLoopIsNoOp()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $stream = tmpfile();
- Loop::removeReadStream($stream);
- $this->assertNull($ref->getValue());
- }
- public function testStaticRemoveWriteStreamCallsRemoveWriteStreamOnLoopInstance()
- {
- $stream = tmpfile();
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('removeWriteStream')->with($stream);
- Loop::set($loop);
- Loop::removeWriteStream($stream);
- }
- public function testStaticRemoveWriteStreamWithNoDefaultLoopIsNoOp()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $stream = tmpfile();
- Loop::removeWriteStream($stream);
- $this->assertNull($ref->getValue());
- }
- public function testStaticAddTimerCallsAddTimerOnLoopInstanceAndReturnsTimerInstance()
- {
- $interval = 1.0;
- $callback = function () { };
- $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('addTimer')->with($interval, $callback)->willReturn($timer);
- Loop::set($loop);
- $ret = Loop::addTimer($interval, $callback);
- $this->assertSame($timer, $ret);
- }
- public function testStaticAddTimerWithNoDefaultLoopCallsAddTimerOnNewLoopInstance()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $interval = 1.0;
- $callback = function () { };
- $ret = Loop::addTimer($interval, $callback);
- $this->assertInstanceOf('React\EventLoop\TimerInterface', $ret);
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $ref->getValue());
- }
- public function testStaticAddPeriodicTimerCallsAddPeriodicTimerOnLoopInstanceAndReturnsTimerInstance()
- {
- $interval = 1.0;
- $callback = function () { };
- $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('addPeriodicTimer')->with($interval, $callback)->willReturn($timer);
- Loop::set($loop);
- $ret = Loop::addPeriodicTimer($interval, $callback);
- $this->assertSame($timer, $ret);
- }
- public function testStaticAddPeriodicTimerWithNoDefaultLoopCallsAddPeriodicTimerOnNewLoopInstance()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $interval = 1.0;
- $callback = function () { };
- $ret = Loop::addPeriodicTimer($interval, $callback);
- $this->assertInstanceOf('React\EventLoop\TimerInterface', $ret);
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $ref->getValue());
- }
- public function testStaticCancelTimerCallsCancelTimerOnLoopInstance()
- {
- $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('cancelTimer')->with($timer);
- Loop::set($loop);
- Loop::cancelTimer($timer);
- }
- public function testStaticCancelTimerWithNoDefaultLoopIsNoOp()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
- Loop::cancelTimer($timer);
- $this->assertNull($ref->getValue());
- }
- public function testStaticFutureTickCallsFutureTickOnLoopInstance()
- {
- $listener = function () { };
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('futureTick')->with($listener);
- Loop::set($loop);
- Loop::futureTick($listener);
- }
- public function testStaticFutureTickWithNoDefaultLoopCallsFutureTickOnNewLoopInstance()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $listener = function () { };
- Loop::futureTick($listener);
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $ref->getValue());
- }
- public function testStaticAddSignalCallsAddSignalOnLoopInstance()
- {
- $signal = 1;
- $listener = function () { };
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('addSignal')->with($signal, $listener);
- Loop::set($loop);
- Loop::addSignal($signal, $listener);
- }
- public function testStaticAddSignalWithNoDefaultLoopCallsAddSignalOnNewLoopInstance()
- {
- if (DIRECTORY_SEPARATOR === '\\') {
- $this->markTestSkipped('Not supported on Windows');
- }
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $signal = 1;
- $listener = function () { };
- try {
- Loop::addSignal($signal, $listener);
- } catch (\BadMethodCallException $e) {
- $this->markTestSkipped('Skipped: ' . $e->getMessage());
- }
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $ref->getValue());
- }
- public function testStaticRemoveSignalCallsRemoveSignalOnLoopInstance()
- {
- $signal = 1;
- $listener = function () { };
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('removeSignal')->with($signal, $listener);
- Loop::set($loop);
- Loop::removeSignal($signal, $listener);
- }
- public function testStaticRemoveSignalWithNoDefaultLoopIsNoOp()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- $signal = 1;
- $listener = function () { };
- Loop::removeSignal($signal, $listener);
- $this->assertNull($ref->getValue());
- }
- public function testStaticRunCallsRunOnLoopInstance()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('run')->with();
- Loop::set($loop);
- Loop::run();
- }
- public function testStaticRunWithNoDefaultLoopCallsRunsOnNewLoopInstance()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- Loop::run();
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $ref->getValue());
- }
- public function testStaticStopCallsStopOnLoopInstance()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('stop')->with();
- Loop::set($loop);
- Loop::stop();
- }
- public function testStaticStopCallWithNoDefaultLoopIsNoOp()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- Loop::stop();
- $this->assertNull($ref->getValue());
- }
- /**
- * @after
- * @before
- */
- public function unsetLoopFromLoopAccessor()
- {
- $ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
- $ref->setAccessible(true);
- $ref->setValue(null, null);
- }
- }
|