| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- <?php
- namespace React\Tests\Socket;
- use React\Promise\Promise;
- use React\Socket\ConnectionInterface;
- use React\Socket\FdServer;
- class FdServerTest extends TestCase
- {
- public function testCtorAddsResourceToLoop()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- assert($socket !== false);
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('addReadStream');
- new FdServer($fd, $loop);
- }
- public function testCtorThrowsForInvalidFd()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->never())->method('addReadStream');
- $this->setExpectedException(
- 'InvalidArgumentException',
- 'Invalid FD number given (EINVAL)',
- defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
- );
- new FdServer(-1, $loop);
- }
- public function testCtorThrowsForInvalidUrl()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->never())->method('addReadStream');
- $this->setExpectedException(
- 'InvalidArgumentException',
- 'Invalid FD number given (EINVAL)',
- defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
- );
- new FdServer('tcp://127.0.0.1:8080', $loop);
- }
- public function testCtorThrowsForUnknownFdWithoutCallingCustomErrorHandler()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->never())->method('addReadStream');
- $error = null;
- set_error_handler(function ($_, $errstr) use (&$error) {
- $error = $errstr;
- });
- $this->setExpectedException(
- 'RuntimeException',
- 'Failed to listen on FD ' . $fd . ': ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EBADF) . ' (EBADF)' : 'Bad file descriptor'),
- defined('SOCKET_EBADF') ? SOCKET_EBADF : 9
- );
- try {
- new FdServer($fd, $loop);
- restore_error_handler();
- } catch (\Exception $e) {
- restore_error_handler();
- $this->assertNull($error);
- throw $e;
- }
- }
- public function testCtorThrowsIfFdIsAFileAndNotASocket()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $tmpfile = tmpfile();
- assert($tmpfile !== false);
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->never())->method('addReadStream');
- $this->setExpectedException(
- 'RuntimeException',
- 'Failed to listen on FD ' . $fd . ': ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_ENOTSOCK) : 'Not a socket') . ' (ENOTSOCK)',
- defined('SOCKET_ENOTSOCK') ? SOCKET_ENOTSOCK : 88
- );
- new FdServer($fd, $loop);
- }
- public function testCtorThrowsIfFdIsAConnectedSocketInsteadOfServerSocket()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $socket = stream_socket_server('tcp://127.0.0.1:0');
- $fd = self::getNextFreeFd();
- $client = stream_socket_client('tcp://' . stream_socket_get_name($socket, false));
- assert($client !== false);
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->never())->method('addReadStream');
- $this->setExpectedException(
- 'RuntimeException',
- 'Failed to listen on FD ' . $fd . ': ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EISCONN) : 'Socket is connected') . ' (EISCONN)',
- defined('SOCKET_EISCONN') ? SOCKET_EISCONN : 106
- );
- new FdServer($fd, $loop);
- }
- public function testGetAddressReturnsSameAddressAsOriginalSocketForIpv4Socket()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $server = new FdServer($fd, $loop);
- $this->assertEquals('tcp://' . stream_socket_get_name($socket, false), $server->getAddress());
- }
- public function testGetAddressReturnsSameAddressAsOriginalSocketForIpv4SocketGivenAsUrlToFd()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $server = new FdServer('php://fd/' . $fd, $loop);
- $this->assertEquals('tcp://' . stream_socket_get_name($socket, false), $server->getAddress());
- }
- public function testGetAddressReturnsSameAddressAsOriginalSocketForIpv6Socket()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = @stream_socket_server('[::1]:0');
- if ($socket === false) {
- $this->markTestSkipped('Listening on IPv6 not supported');
- }
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $server = new FdServer($fd, $loop);
- $port = preg_replace('/.*:/', '', stream_socket_get_name($socket, false));
- $this->assertEquals('tcp://[::1]:' . $port, $server->getAddress());
- }
- public function testGetAddressReturnsSameAddressAsOriginalSocketForUnixDomainSocket()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = @stream_socket_server($this->getRandomSocketUri());
- if ($socket === false) {
- $this->markTestSkipped('Listening on Unix domain socket (UDS) not supported');
- }
- assert(is_resource($socket));
- unlink(str_replace('unix://', '', stream_socket_get_name($socket, false)));
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $server = new FdServer($fd, $loop);
- $this->assertEquals('unix://' . stream_socket_get_name($socket, false), $server->getAddress());
- }
- public function testGetAddressReturnsNullAfterClose()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- assert($socket !== false);
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $server = new FdServer($fd, $loop);
- $server->close();
- $this->assertNull($server->getAddress());
- }
- public function testCloseRemovesResourceFromLoop()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- assert($socket !== false);
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('removeReadStream');
- $server = new FdServer($fd, $loop);
- $server->close();
- }
- public function testCloseTwiceRemovesResourceFromLoopOnce()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- assert($socket !== false);
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('removeReadStream');
- $server = new FdServer($fd, $loop);
- $server->close();
- $server->close();
- }
- public function testResumeWithoutPauseIsNoOp()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- assert($socket !== false);
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('addReadStream');
- $server = new FdServer($fd, $loop);
- $server->resume();
- }
- public function testPauseRemovesResourceFromLoop()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- assert($socket !== false);
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('removeReadStream');
- $server = new FdServer($fd, $loop);
- $server->pause();
- }
- public function testPauseAfterPauseIsNoOp()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- assert($socket !== false);
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('removeReadStream');
- $server = new FdServer($fd, $loop);
- $server->pause();
- $server->pause();
- }
- public function testServerEmitsConnectionEventForNewConnection()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- assert($socket !== false);
- $client = stream_socket_client('tcp://' . stream_socket_get_name($socket, false));
- $server = new FdServer($fd);
- $promise = new Promise(function ($resolve) use ($server) {
- $server->on('connection', $resolve);
- });
- $connection = \React\Async\await(\React\Promise\Timer\timeout($promise, 1.0));
- /**
- * @var ConnectionInterface $connection
- */
- $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection);
- fclose($client);
- $connection->close();
- $server->close();
- }
- public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHandler()
- {
- if (!is_dir('/dev/fd') || defined('HHVM_VERSION')) {
- $this->markTestSkipped('Not supported on your platform');
- }
- $listener = null;
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $loop->expects($this->once())->method('addReadStream')->with($this->anything(), $this->callback(function ($cb) use (&$listener) {
- $listener = $cb;
- return true;
- }));
- $fd = self::getNextFreeFd();
- $socket = stream_socket_server('127.0.0.1:0');
- assert($socket !== false);
- $server = new FdServer($fd, $loop);
- $exception = null;
- $server->on('error', function ($e) use (&$exception) {
- $exception = $e;
- });
- $this->assertNotNull($listener);
- $socket = stream_socket_server('tcp://127.0.0.1:0');
- $error = null;
- set_error_handler(function ($_, $errstr) use (&$error) {
- $error = $errstr;
- });
- $time = microtime(true);
- $listener($socket);
- $time = microtime(true) - $time;
- restore_error_handler();
- $this->assertNull($error);
- $this->assertLessThan(1, $time);
- $this->assertInstanceOf('RuntimeException', $exception);
- assert($exception instanceof \RuntimeException);
- $this->assertStringStartsWith('Unable to accept new connection: ', $exception->getMessage());
- return $exception;
- }
- /**
- * @param \RuntimeException $e
- * @requires extension sockets
- * @depends testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHandler
- */
- public function testEmitsTimeoutErrorWhenAcceptListenerFails(\RuntimeException $exception)
- {
- $this->assertEquals('Unable to accept new connection: ' . socket_strerror(SOCKET_ETIMEDOUT) . ' (ETIMEDOUT)', $exception->getMessage());
- $this->assertEquals(SOCKET_ETIMEDOUT, $exception->getCode());
- }
- /**
- * @return int
- * @throws \UnexpectedValueException
- * @throws \BadMethodCallException
- * @throws \UnderflowException
- * @copyright Copyright (c) 2018 Christian Lück, taken from https://github.com/clue/fd with permission
- */
- public static function getNextFreeFd()
- {
- // open tmpfile to occupy next free FD temporarily
- $tmp = tmpfile();
- $dir = @scandir('/dev/fd');
- if ($dir === false) {
- throw new \BadMethodCallException('Not supported on your platform because /dev/fd is not readable');
- }
- $stat = fstat($tmp);
- $ino = (int) $stat['ino'];
- foreach ($dir as $file) {
- $stat = @stat('/dev/fd/' . $file);
- if (isset($stat['ino']) && $stat['ino'] === $ino) {
- return (int) $file;
- }
- }
- throw new \UnderflowException('Could not locate file descriptor for this resource');
- }
- private function getRandomSocketUri()
- {
- return "unix://" . sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(rand(), true) . '.sock';
- }
- }
|