ExtEventLoopTest.php 2.5 KB

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