|
4 | 4 |
|
5 | 5 | use React\Socket\Server; |
6 | 6 | use React\EventLoop\StreamSelectLoop; |
| 7 | +use React\Stream\Stream; |
7 | 8 |
|
8 | 9 | class ServerTest extends TestCase |
9 | 10 | { |
@@ -131,6 +132,65 @@ public function testDataWillBeFragmentedToBufferSize() |
131 | 132 | $this->loop->tick(); |
132 | 133 | } |
133 | 134 |
|
| 135 | + public function testLoopWillEndWhenServerIsShutDown() |
| 136 | + { |
| 137 | + // explicitly unset server because we already call shutdown() |
| 138 | + $this->server->shutdown(); |
| 139 | + $this->server = null; |
| 140 | + |
| 141 | + $this->loop->run(); |
| 142 | + } |
| 143 | + |
| 144 | + public function testLoopWillEndWhenServerIsShutDownAfterSingleConnection() |
| 145 | + { |
| 146 | + $client = stream_socket_client('tcp://localhost:' . $this->port); |
| 147 | + |
| 148 | + // explicitly unset server because we only accept a single connection |
| 149 | + // and then already call shutdown() |
| 150 | + $server = $this->server; |
| 151 | + $this->server = null; |
| 152 | + |
| 153 | + $server->on('connection', function ($conn) use ($server) { |
| 154 | + $conn->close(); |
| 155 | + $server->shutdown(); |
| 156 | + }); |
| 157 | + |
| 158 | + $this->loop->run(); |
| 159 | + } |
| 160 | + |
| 161 | + public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmounts() |
| 162 | + { |
| 163 | + $client = stream_socket_client('tcp://localhost:' . $this->port); |
| 164 | + $stream = new Stream($client, $this->loop); |
| 165 | + |
| 166 | + $bytes = 1024 * 1024; |
| 167 | + $stream->end(str_repeat('*', $bytes)); |
| 168 | + |
| 169 | + $mock = $this->expectCallableOnce(); |
| 170 | + |
| 171 | + // explicitly unset server because we only accept a single connection |
| 172 | + // and then already call shutdown() |
| 173 | + $server = $this->server; |
| 174 | + $this->server = null; |
| 175 | + |
| 176 | + $received = 0; |
| 177 | + $server->on('connection', function ($conn) use ($mock, &$received, $server) { |
| 178 | + // count number of bytes received |
| 179 | + $conn->on('data', function ($data) use (&$received) { |
| 180 | + $received += strlen($data); |
| 181 | + }); |
| 182 | + |
| 183 | + $conn->on('end', $mock); |
| 184 | + |
| 185 | + // do not await any further connections in order to let the loop terminate |
| 186 | + $server->shutdown(); |
| 187 | + }); |
| 188 | + |
| 189 | + $this->loop->run(); |
| 190 | + |
| 191 | + $this->assertEquals($bytes, $received); |
| 192 | + } |
| 193 | + |
134 | 194 | /** |
135 | 195 | * @covers React\EventLoop\StreamSelectLoop::tick |
136 | 196 | */ |
@@ -166,6 +226,15 @@ public function testConnectionDoesEndWhenClientCloses() |
166 | 226 | $this->loop->tick(); |
167 | 227 | } |
168 | 228 |
|
| 229 | + /** |
| 230 | + * @expectedException React\Socket\ConnectionException |
| 231 | + */ |
| 232 | + public function testListenOnBusyPortThrows() |
| 233 | + { |
| 234 | + $another = new Server($this->loop); |
| 235 | + $another->listen($this->port); |
| 236 | + } |
| 237 | + |
169 | 238 | /** |
170 | 239 | * @covers React\Socket\Server::shutdown |
171 | 240 | */ |
|
0 commit comments