ExtEventLoopTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace React\Tests\EventLoop;
  3. use React\EventLoop\ExtEventLoop;
  4. class ExtEventLoopTest extends AbstractLoopTest
  5. {
  6. /** @var ?string */
  7. private $fifoPath;
  8. public function createLoop($readStreamCompatible = false)
  9. {
  10. if ('Linux' === PHP_OS && !extension_loaded('posix')) {
  11. $this->markTestSkipped('libevent tests skipped on linux due to linux epoll issues.');
  12. }
  13. if (!extension_loaded('event')) {
  14. $this->markTestSkipped('ext-event tests skipped because ext-event is not installed.');
  15. }
  16. return new ExtEventLoop();
  17. }
  18. /**
  19. * @after
  20. */
  21. public function tearDownFile()
  22. {
  23. if ($this->fifoPath !== null && file_exists($this->fifoPath)) {
  24. unlink($this->fifoPath);
  25. }
  26. }
  27. public function createStream()
  28. {
  29. // Use a FIFO on linux to get around lack of support for disk-based file
  30. // descriptors when using the EPOLL back-end.
  31. if ('Linux' === PHP_OS) {
  32. $this->fifoPath = tempnam(sys_get_temp_dir(), 'react-');
  33. assert(is_string($this->fifoPath));
  34. unlink($this->fifoPath);
  35. posix_mkfifo($this->fifoPath, 0600);
  36. $stream = fopen($this->fifoPath, 'r+');
  37. // ext-event (as of 1.8.1) does not yet support in-memory temporary
  38. // streams. Setting maxmemory:0 and performing a write forces PHP to
  39. // back this temporary stream with a real file.
  40. //
  41. // This problem is mentioned at https://bugs.php.net/bug.php?id=64652&edit=3
  42. // but remains unresolved (despite that issue being closed).
  43. } else {
  44. $stream = fopen('php://temp/maxmemory:0', 'r+');
  45. fwrite($stream, 'x');
  46. ftruncate($stream, 0);
  47. }
  48. return $stream;
  49. }
  50. public function writeToStream($stream, $content)
  51. {
  52. if ('Linux' !== PHP_OS) {
  53. return parent::writeToStream($stream, $content);
  54. }
  55. fwrite($stream, $content);
  56. }
  57. /**
  58. * @group epoll-readable-error
  59. */
  60. public function testCanUseReadableStreamWithFeatureFds()
  61. {
  62. if (PHP_VERSION_ID > 70000) {
  63. $this->markTestSkipped('Memory stream not supported');
  64. }
  65. $this->loop = $this->createLoop(true);
  66. $input = fopen('php://temp/maxmemory:0', 'r+');
  67. fwrite($input, 'x');
  68. ftruncate($input, 0);
  69. $this->loop->addReadStream($input, $this->expectCallableExactly(2));
  70. fwrite($input, "foo\n");
  71. $this->tickLoop($this->loop);
  72. fwrite($input, "bar\n");
  73. $this->tickLoop($this->loop);
  74. }
  75. }