init
This commit is contained in:
539
vendor/react/event-loop/tests/AbstractLoopTest.php
vendored
Executable file
539
vendor/react/event-loop/tests/AbstractLoopTest.php
vendored
Executable file
@@ -0,0 +1,539 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
10
vendor/react/event-loop/tests/CallableStub.php
vendored
Executable file
10
vendor/react/event-loop/tests/CallableStub.php
vendored
Executable file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop;
|
||||
|
||||
class CallableStub
|
||||
{
|
||||
public function __invoke()
|
||||
{
|
||||
}
|
||||
}
|
||||
90
vendor/react/event-loop/tests/ExtEventLoopTest.php
vendored
Executable file
90
vendor/react/event-loop/tests/ExtEventLoopTest.php
vendored
Executable file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop;
|
||||
|
||||
use React\EventLoop\ExtEventLoop;
|
||||
|
||||
class ExtEventLoopTest extends AbstractLoopTest
|
||||
{
|
||||
public function createLoop($readStreamCompatible = false)
|
||||
{
|
||||
if ('Linux' === PHP_OS && !extension_loaded('posix')) {
|
||||
$this->markTestSkipped('libevent tests skipped on linux due to linux epoll issues.');
|
||||
}
|
||||
|
||||
if (!extension_loaded('event')) {
|
||||
$this->markTestSkipped('ext-event tests skipped because ext-event is not installed.');
|
||||
}
|
||||
|
||||
$cfg = null;
|
||||
if ($readStreamCompatible) {
|
||||
$cfg = new \EventConfig();
|
||||
$cfg->requireFeatures(\EventConfig::FEATURE_FDS);
|
||||
}
|
||||
|
||||
return new ExtEventLoop($cfg);
|
||||
}
|
||||
|
||||
public function createStream()
|
||||
{
|
||||
// Use a FIFO on linux to get around lack of support for disk-based file
|
||||
// descriptors when using the EPOLL back-end.
|
||||
if ('Linux' === PHP_OS) {
|
||||
$this->fifoPath = tempnam(sys_get_temp_dir(), 'react-');
|
||||
|
||||
unlink($this->fifoPath);
|
||||
|
||||
posix_mkfifo($this->fifoPath, 0600);
|
||||
|
||||
$stream = fopen($this->fifoPath, 'r+');
|
||||
|
||||
// ext-event (as of 1.8.1) does not yet support in-memory temporary
|
||||
// streams. Setting maxmemory:0 and performing a write forces PHP to
|
||||
// back this temporary stream with a real file.
|
||||
//
|
||||
// This problem is mentioned at https://bugs.php.net/bug.php?id=64652&edit=3
|
||||
// but remains unresolved (despite that issue being closed).
|
||||
} else {
|
||||
$stream = fopen('php://temp/maxmemory:0', 'r+');
|
||||
|
||||
fwrite($stream, 'x');
|
||||
ftruncate($stream, 0);
|
||||
}
|
||||
|
||||
return $stream;
|
||||
}
|
||||
|
||||
public function writeToStream($stream, $content)
|
||||
{
|
||||
if ('Linux' !== PHP_OS) {
|
||||
return parent::writeToStream($stream, $content);
|
||||
}
|
||||
|
||||
fwrite($stream, $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group epoll-readable-error
|
||||
*/
|
||||
public function testCanUseReadableStreamWithFeatureFds()
|
||||
{
|
||||
if (PHP_VERSION_ID > 70000) {
|
||||
$this->markTestSkipped('Memory stream not supported');
|
||||
}
|
||||
|
||||
$this->loop = $this->createLoop(true);
|
||||
|
||||
$input = fopen('php://temp/maxmemory:0', 'r+');
|
||||
|
||||
fwrite($input, 'x');
|
||||
ftruncate($input, 0);
|
||||
|
||||
$this->loop->addReadStream($input, $this->expectCallableExactly(2));
|
||||
|
||||
fwrite($input, "foo\n");
|
||||
$this->loop->tick();
|
||||
|
||||
fwrite($input, "bar\n");
|
||||
$this->loop->tick();
|
||||
}
|
||||
}
|
||||
22
vendor/react/event-loop/tests/LibEvLoopTest.php
vendored
Executable file
22
vendor/react/event-loop/tests/LibEvLoopTest.php
vendored
Executable file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop;
|
||||
|
||||
use React\EventLoop\LibEvLoop;
|
||||
|
||||
class LibEvLoopTest extends AbstractLoopTest
|
||||
{
|
||||
public function createLoop()
|
||||
{
|
||||
if (!class_exists('libev\EventLoop')) {
|
||||
$this->markTestSkipped('libev tests skipped because ext-libev is not installed.');
|
||||
}
|
||||
|
||||
return new LibEvLoop();
|
||||
}
|
||||
|
||||
public function testLibEvConstructor()
|
||||
{
|
||||
$loop = new LibEvLoop();
|
||||
}
|
||||
}
|
||||
58
vendor/react/event-loop/tests/LibEventLoopTest.php
vendored
Executable file
58
vendor/react/event-loop/tests/LibEventLoopTest.php
vendored
Executable file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop;
|
||||
|
||||
use React\EventLoop\LibEventLoop;
|
||||
|
||||
class LibEventLoopTest extends AbstractLoopTest
|
||||
{
|
||||
private $fifoPath;
|
||||
|
||||
public function createLoop()
|
||||
{
|
||||
if ('Linux' === PHP_OS && !extension_loaded('posix')) {
|
||||
$this->markTestSkipped('libevent tests skipped on linux due to linux epoll issues.');
|
||||
}
|
||||
|
||||
if (!function_exists('event_base_new')) {
|
||||
$this->markTestSkipped('libevent tests skipped because ext-libevent is not installed.');
|
||||
}
|
||||
|
||||
return new LibEventLoop();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
if (file_exists($this->fifoPath)) {
|
||||
unlink($this->fifoPath);
|
||||
}
|
||||
}
|
||||
|
||||
public function createStream()
|
||||
{
|
||||
if ('Linux' !== PHP_OS) {
|
||||
return parent::createStream();
|
||||
}
|
||||
|
||||
$this->fifoPath = tempnam(sys_get_temp_dir(), 'react-');
|
||||
|
||||
unlink($this->fifoPath);
|
||||
|
||||
// Use a FIFO on linux to get around lack of support for disk-based file
|
||||
// descriptors when using the EPOLL back-end.
|
||||
posix_mkfifo($this->fifoPath, 0600);
|
||||
|
||||
$stream = fopen($this->fifoPath, 'r+');
|
||||
|
||||
return $stream;
|
||||
}
|
||||
|
||||
public function writeToStream($stream, $content)
|
||||
{
|
||||
if ('Linux' !== PHP_OS) {
|
||||
return parent::writeToStream($stream, $content);
|
||||
}
|
||||
|
||||
fwrite($stream, $content);
|
||||
}
|
||||
}
|
||||
179
vendor/react/event-loop/tests/StreamSelectLoopTest.php
vendored
Executable file
179
vendor/react/event-loop/tests/StreamSelectLoopTest.php
vendored
Executable file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop;
|
||||
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\EventLoop\StreamSelectLoop;
|
||||
use React\EventLoop\Timer\Timer;
|
||||
|
||||
class StreamSelectLoopTest extends AbstractLoopTest
|
||||
{
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
if (strncmp($this->getName(false), 'testSignal', 10) === 0 && extension_loaded('pcntl')) {
|
||||
$this->resetSignalHandlers();
|
||||
}
|
||||
}
|
||||
|
||||
public function createLoop()
|
||||
{
|
||||
return new StreamSelectLoop();
|
||||
}
|
||||
|
||||
public function testStreamSelectTimeoutEmulation()
|
||||
{
|
||||
$this->loop->addTimer(
|
||||
0.05,
|
||||
$this->expectCallableOnce()
|
||||
);
|
||||
|
||||
$start = microtime(true);
|
||||
|
||||
$this->loop->run();
|
||||
|
||||
$end = microtime(true);
|
||||
$interval = $end - $start;
|
||||
|
||||
$this->assertGreaterThan(0.04, $interval);
|
||||
}
|
||||
|
||||
public function signalProvider()
|
||||
{
|
||||
return [
|
||||
['SIGUSR1'],
|
||||
['SIGHUP'],
|
||||
['SIGTERM'],
|
||||
];
|
||||
}
|
||||
|
||||
private $_signalHandled = false;
|
||||
|
||||
/**
|
||||
* Test signal interrupt when no stream is attached to the loop
|
||||
* @dataProvider signalProvider
|
||||
*/
|
||||
public function testSignalInterruptNoStream($signal)
|
||||
{
|
||||
if (!extension_loaded('pcntl')) {
|
||||
$this->markTestSkipped('"pcntl" extension is required to run this test.');
|
||||
}
|
||||
|
||||
// dispatch signal handler once before signal is sent and once after
|
||||
$this->loop->addTimer(0.01, function() { pcntl_signal_dispatch(); });
|
||||
$this->loop->addTimer(0.03, function() { pcntl_signal_dispatch(); });
|
||||
if (defined('HHVM_VERSION')) {
|
||||
// hhvm startup is slow so we need to add another handler much later
|
||||
$this->loop->addTimer(0.5, function() { pcntl_signal_dispatch(); });
|
||||
}
|
||||
|
||||
$this->setUpSignalHandler($signal);
|
||||
|
||||
// spawn external process to send signal to current process id
|
||||
$this->forkSendSignal($signal);
|
||||
$this->loop->run();
|
||||
$this->assertTrue($this->_signalHandled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test signal interrupt when a stream is attached to the loop
|
||||
* @dataProvider signalProvider
|
||||
*/
|
||||
public function testSignalInterruptWithStream($signal)
|
||||
{
|
||||
if (!extension_loaded('pcntl')) {
|
||||
$this->markTestSkipped('"pcntl" extension is required to run this test.');
|
||||
}
|
||||
|
||||
// dispatch signal handler every 10ms
|
||||
$this->loop->addPeriodicTimer(0.01, function() { pcntl_signal_dispatch(); });
|
||||
|
||||
// add stream to the loop
|
||||
list($writeStream, $readStream) = $this->createSocketPair();
|
||||
$this->loop->addReadStream($readStream, function($stream, $loop) {
|
||||
/** @var $loop LoopInterface */
|
||||
$read = fgets($stream);
|
||||
if ($read === "end loop\n") {
|
||||
$loop->stop();
|
||||
}
|
||||
});
|
||||
$this->loop->addTimer(0.05, function() use ($writeStream) {
|
||||
fwrite($writeStream, "end loop\n");
|
||||
});
|
||||
|
||||
$this->setUpSignalHandler($signal);
|
||||
|
||||
// spawn external process to send signal to current process id
|
||||
$this->forkSendSignal($signal);
|
||||
|
||||
$this->loop->run();
|
||||
|
||||
$this->assertTrue($this->_signalHandled);
|
||||
}
|
||||
|
||||
/**
|
||||
* add signal handler for signal
|
||||
*/
|
||||
protected function setUpSignalHandler($signal)
|
||||
{
|
||||
$this->_signalHandled = false;
|
||||
$this->assertTrue(pcntl_signal(constant($signal), function() { $this->_signalHandled = true; }));
|
||||
}
|
||||
|
||||
/**
|
||||
* reset all signal handlers to default
|
||||
*/
|
||||
protected function resetSignalHandlers()
|
||||
{
|
||||
foreach($this->signalProvider() as $signal) {
|
||||
pcntl_signal(constant($signal[0]), SIG_DFL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* fork child process to send signal to current process id
|
||||
*/
|
||||
protected function forkSendSignal($signal)
|
||||
{
|
||||
$currentPid = posix_getpid();
|
||||
$childPid = pcntl_fork();
|
||||
if ($childPid == -1) {
|
||||
$this->fail("Failed to fork child process!");
|
||||
} else if ($childPid === 0) {
|
||||
// this is executed in the child process
|
||||
usleep(20000);
|
||||
posix_kill($currentPid, constant($signal));
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* https://github.com/reactphp/event-loop/issues/48
|
||||
*
|
||||
* Tests that timer with very small interval uses at least 1 microsecond
|
||||
* timeout.
|
||||
*/
|
||||
public function testSmallTimerInterval()
|
||||
{
|
||||
/** @var StreamSelectLoop|\PHPUnit_Framework_MockObject_MockObject $loop */
|
||||
$loop = $this->getMock('React\EventLoop\StreamSelectLoop', ['streamSelect']);
|
||||
$loop
|
||||
->expects($this->at(0))
|
||||
->method('streamSelect')
|
||||
->with([], [], 1);
|
||||
$loop
|
||||
->expects($this->at(1))
|
||||
->method('streamSelect')
|
||||
->with([], [], 0);
|
||||
|
||||
$callsCount = 0;
|
||||
$loop->addPeriodicTimer(Timer::MIN_INTERVAL, function() use (&$loop, &$callsCount) {
|
||||
$callsCount++;
|
||||
if ($callsCount == 2) {
|
||||
$loop->stop();
|
||||
}
|
||||
});
|
||||
|
||||
$loop->run();
|
||||
}
|
||||
}
|
||||
41
vendor/react/event-loop/tests/TestCase.php
vendored
Executable file
41
vendor/react/event-loop/tests/TestCase.php
vendored
Executable file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop;
|
||||
|
||||
class TestCase extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function expectCallableExactly($amount)
|
||||
{
|
||||
$mock = $this->createCallableMock();
|
||||
$mock
|
||||
->expects($this->exactly($amount))
|
||||
->method('__invoke');
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
protected function expectCallableOnce()
|
||||
{
|
||||
$mock = $this->createCallableMock();
|
||||
$mock
|
||||
->expects($this->once())
|
||||
->method('__invoke');
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
protected function expectCallableNever()
|
||||
{
|
||||
$mock = $this->createCallableMock();
|
||||
$mock
|
||||
->expects($this->never())
|
||||
->method('__invoke');
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
protected function createCallableMock()
|
||||
{
|
||||
return $this->getMockBuilder('React\Tests\EventLoop\CallableStub')->getMock();
|
||||
}
|
||||
}
|
||||
97
vendor/react/event-loop/tests/Timer/AbstractTimerTest.php
vendored
Executable file
97
vendor/react/event-loop/tests/Timer/AbstractTimerTest.php
vendored
Executable file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop\Timer;
|
||||
|
||||
use React\Tests\EventLoop\TestCase;
|
||||
|
||||
abstract class AbstractTimerTest extends TestCase
|
||||
{
|
||||
abstract public function createLoop();
|
||||
|
||||
public function testAddTimer()
|
||||
{
|
||||
// usleep is intentionally high
|
||||
|
||||
$loop = $this->createLoop();
|
||||
|
||||
$loop->addTimer(0.001, $this->expectCallableOnce());
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
}
|
||||
|
||||
public function testAddPeriodicTimer()
|
||||
{
|
||||
$loop = $this->createLoop();
|
||||
|
||||
$loop->addPeriodicTimer(0.001, $this->expectCallableExactly(3));
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
}
|
||||
|
||||
public function testAddPeriodicTimerWithCancel()
|
||||
{
|
||||
$loop = $this->createLoop();
|
||||
|
||||
$timer = $loop->addPeriodicTimer(0.001, $this->expectCallableExactly(2));
|
||||
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
|
||||
$timer->cancel();
|
||||
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
}
|
||||
|
||||
public function testAddPeriodicTimerCancelsItself()
|
||||
{
|
||||
$i = 0;
|
||||
|
||||
$loop = $this->createLoop();
|
||||
|
||||
$loop->addPeriodicTimer(0.001, function ($timer) use (&$i) {
|
||||
$i++;
|
||||
|
||||
if ($i == 2) {
|
||||
$timer->cancel();
|
||||
}
|
||||
});
|
||||
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
usleep(1000);
|
||||
$loop->tick();
|
||||
|
||||
$this->assertSame(2, $i);
|
||||
}
|
||||
|
||||
public function testIsTimerActive()
|
||||
{
|
||||
$loop = $this->createLoop();
|
||||
|
||||
$timer = $loop->addPeriodicTimer(0.001, function () {});
|
||||
|
||||
$this->assertTrue($loop->isTimerActive($timer));
|
||||
|
||||
$timer->cancel();
|
||||
|
||||
$this->assertFalse($loop->isTimerActive($timer));
|
||||
}
|
||||
|
||||
public function testMinimumIntervalOneMicrosecond()
|
||||
{
|
||||
$loop = $this->createLoop();
|
||||
|
||||
$timer = $loop->addTimer(0, function () {});
|
||||
|
||||
$this->assertEquals(0.000001, $timer->getInterval());
|
||||
}
|
||||
}
|
||||
17
vendor/react/event-loop/tests/Timer/ExtEventTimerTest.php
vendored
Executable file
17
vendor/react/event-loop/tests/Timer/ExtEventTimerTest.php
vendored
Executable file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop\Timer;
|
||||
|
||||
use React\EventLoop\ExtEventLoop;
|
||||
|
||||
class ExtEventTimerTest extends AbstractTimerTest
|
||||
{
|
||||
public function createLoop()
|
||||
{
|
||||
if (!extension_loaded('event')) {
|
||||
$this->markTestSkipped('ext-event tests skipped because ext-event is not installed.');
|
||||
}
|
||||
|
||||
return new ExtEventLoop();
|
||||
}
|
||||
}
|
||||
17
vendor/react/event-loop/tests/Timer/LibEvTimerTest.php
vendored
Executable file
17
vendor/react/event-loop/tests/Timer/LibEvTimerTest.php
vendored
Executable file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop\Timer;
|
||||
|
||||
use React\EventLoop\LibEvLoop;
|
||||
|
||||
class LibEvTimerTest extends AbstractTimerTest
|
||||
{
|
||||
public function createLoop()
|
||||
{
|
||||
if (!class_exists('libev\EventLoop')) {
|
||||
$this->markTestSkipped('libev tests skipped because ext-libev is not installed.');
|
||||
}
|
||||
|
||||
return new LibEvLoop();
|
||||
}
|
||||
}
|
||||
17
vendor/react/event-loop/tests/Timer/LibEventTimerTest.php
vendored
Executable file
17
vendor/react/event-loop/tests/Timer/LibEventTimerTest.php
vendored
Executable file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop\Timer;
|
||||
|
||||
use React\EventLoop\LibEventLoop;
|
||||
|
||||
class LibEventTimerTest extends AbstractTimerTest
|
||||
{
|
||||
public function createLoop()
|
||||
{
|
||||
if (!function_exists('event_base_new')) {
|
||||
$this->markTestSkipped('libevent tests skipped because ext-libevent is not installed.');
|
||||
}
|
||||
|
||||
return new LibEventLoop();
|
||||
}
|
||||
}
|
||||
13
vendor/react/event-loop/tests/Timer/StreamSelectTimerTest.php
vendored
Executable file
13
vendor/react/event-loop/tests/Timer/StreamSelectTimerTest.php
vendored
Executable file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop\Timer;
|
||||
|
||||
use React\EventLoop\StreamSelectLoop;
|
||||
|
||||
class StreamSelectTimerTest extends AbstractTimerTest
|
||||
{
|
||||
public function createLoop()
|
||||
{
|
||||
return new StreamSelectLoop();
|
||||
}
|
||||
}
|
||||
29
vendor/react/event-loop/tests/Timer/TimersTest.php
vendored
Executable file
29
vendor/react/event-loop/tests/Timer/TimersTest.php
vendored
Executable file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace React\Tests\EventLoop\Timer;
|
||||
|
||||
use React\Tests\EventLoop\TestCase;
|
||||
use React\EventLoop\Timer\Timer;
|
||||
use React\EventLoop\Timer\Timers;
|
||||
|
||||
class TimersTest extends TestCase
|
||||
{
|
||||
public function testBlockedTimer()
|
||||
{
|
||||
$loop = $this
|
||||
->getMockBuilder('React\EventLoop\LoopInterface')
|
||||
->getMock();
|
||||
|
||||
$timers = new Timers();
|
||||
$timers->tick();
|
||||
|
||||
// simulate a bunch of processing on stream events,
|
||||
// part of which schedules a future timer...
|
||||
sleep(1);
|
||||
$timers->add(new Timer($loop, 0.5, function () {
|
||||
$this->fail("Timer shouldn't be called");
|
||||
}));
|
||||
|
||||
$timers->tick();
|
||||
}
|
||||
}
|
||||
7
vendor/react/event-loop/tests/bootstrap.php
vendored
Executable file
7
vendor/react/event-loop/tests/bootstrap.php
vendored
Executable file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
$loader = @include __DIR__ . '/../vendor/autoload.php';
|
||||
if (!$loader) {
|
||||
$loader = require __DIR__ . '/../../../../vendor/autoload.php';
|
||||
}
|
||||
$loader->addPsr4('React\\Tests\\EventLoop\\', __DIR__);
|
||||
Reference in New Issue
Block a user