|
2 | 2 |
|
3 | 3 | namespace React\Tests\EventLoop;
|
4 | 4 |
|
| 5 | +use React\EventLoop\StreamSelectLoop; |
| 6 | + |
5 | 7 | abstract class AbstractLoopTest extends TestCase
|
6 | 8 | {
|
7 | 9 | /**
|
@@ -36,6 +38,110 @@ public function createSocketPair()
|
36 | 38 | return $sockets;
|
37 | 39 | }
|
38 | 40 |
|
| 41 | + public function testAddReadStreamTriggersWhenSocketReceivesData() |
| 42 | + { |
| 43 | + list ($input, $output) = $this->createSocketPair(); |
| 44 | + |
| 45 | + $loop = $this->loop; |
| 46 | + $timeout = $loop->addTimer(0.1, function () use ($input, $loop) { |
| 47 | + $loop->removeReadStream($input); |
| 48 | + }); |
| 49 | + |
| 50 | + $called = 0; |
| 51 | + $this->loop->addReadStream($input, function () use (&$called, $loop, $input, $timeout) { |
| 52 | + ++$called; |
| 53 | + $loop->removeReadStream($input); |
| 54 | + $loop->cancelTimer($timeout); |
| 55 | + }); |
| 56 | + |
| 57 | + fwrite($output, "foo\n"); |
| 58 | + |
| 59 | + $this->loop->run(); |
| 60 | + |
| 61 | + $this->assertEquals(1, $called); |
| 62 | + } |
| 63 | + |
| 64 | + public function testAddReadStreamTriggersWhenSocketCloses() |
| 65 | + { |
| 66 | + list ($input, $output) = $this->createSocketPair(); |
| 67 | + |
| 68 | + $loop = $this->loop; |
| 69 | + $timeout = $loop->addTimer(0.1, function () use ($input, $loop) { |
| 70 | + $loop->removeReadStream($input); |
| 71 | + }); |
| 72 | + |
| 73 | + $called = 0; |
| 74 | + $this->loop->addReadStream($input, function () use (&$called, $loop, $input, $timeout) { |
| 75 | + ++$called; |
| 76 | + $loop->removeReadStream($input); |
| 77 | + $loop->cancelTimer($timeout); |
| 78 | + }); |
| 79 | + |
| 80 | + fclose($output); |
| 81 | + |
| 82 | + $this->loop->run(); |
| 83 | + |
| 84 | + $this->assertEquals(1, $called); |
| 85 | + } |
| 86 | + |
| 87 | + public function testAddWriteStreamTriggersWhenSocketConnectionSucceeds() |
| 88 | + { |
| 89 | + $server = stream_socket_server('127.0.0.1:0'); |
| 90 | + |
| 91 | + $errno = $errstr = null; |
| 92 | + $connecting = stream_socket_client(stream_socket_get_name($server, false), $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT); |
| 93 | + |
| 94 | + $loop = $this->loop; |
| 95 | + $timeout = $loop->addTimer(0.1, function () use ($connecting, $loop) { |
| 96 | + $loop->removeWriteStream($connecting); |
| 97 | + }); |
| 98 | + |
| 99 | + $called = 0; |
| 100 | + $this->loop->addWriteStream($connecting, function () use (&$called, $loop, $connecting, $timeout) { |
| 101 | + ++$called; |
| 102 | + $loop->removeWriteStream($connecting); |
| 103 | + $loop->cancelTimer($timeout); |
| 104 | + }); |
| 105 | + |
| 106 | + $this->loop->run(); |
| 107 | + |
| 108 | + $this->assertEquals(1, $called); |
| 109 | + } |
| 110 | + |
| 111 | + public function testAddWriteStreamTriggersWhenSocketConnectionRefused() |
| 112 | + { |
| 113 | + // @link https://github.com/reactphp/event-loop/issues/206 |
| 114 | + if ($this->loop instanceof StreamSelectLoop && DIRECTORY_SEPARATOR === '\\') { |
| 115 | + $this->markTestIncomplete('StreamSelectLoop does not currently support detecting connection refused errors on Windows'); |
| 116 | + } |
| 117 | + |
| 118 | + // first verify the operating system actually refuses the connection and no firewall is in place |
| 119 | + // use higher timeout because Windows retires multiple times and has a noticeable delay |
| 120 | + // @link https://stackoverflow.com/questions/19440364/why-do-failed-attempts-of-socket-connect-take-1-sec-on-windows |
| 121 | + $errno = $errstr = null; |
| 122 | + if (@stream_socket_client('127.0.0.1:1', $errno, $errstr, 10.0) !== false || $errno !== SOCKET_ECONNREFUSED) { |
| 123 | + $this->markTestSkipped('Expected host to refuse connection, but got error ' . $errno . ': ' . $errstr); |
| 124 | + } |
| 125 | + |
| 126 | + $connecting = stream_socket_client('127.0.0.1:1', $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT); |
| 127 | + |
| 128 | + $loop = $this->loop; |
| 129 | + $timeout = $loop->addTimer(10.0, function () use ($connecting, $loop) { |
| 130 | + $loop->removeWriteStream($connecting); |
| 131 | + }); |
| 132 | + |
| 133 | + $called = 0; |
| 134 | + $this->loop->addWriteStream($connecting, function () use (&$called, $loop, $connecting, $timeout) { |
| 135 | + ++$called; |
| 136 | + $loop->removeWriteStream($connecting); |
| 137 | + $loop->cancelTimer($timeout); |
| 138 | + }); |
| 139 | + |
| 140 | + $this->loop->run(); |
| 141 | + |
| 142 | + $this->assertEquals(1, $called); |
| 143 | + } |
| 144 | + |
39 | 145 | public function testAddReadStream()
|
40 | 146 | {
|
41 | 147 | list ($input, $output) = $this->createSocketPair();
|
|
0 commit comments