Skip to content

Commit 48288a8

Browse files
committed
Additional tests for Server
1 parent 818a59b commit 48288a8

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

tests/ServerTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use React\Socket\Server;
66
use React\EventLoop\StreamSelectLoop;
7+
use React\Stream\Stream;
78

89
class ServerTest extends TestCase
910
{
@@ -131,6 +132,65 @@ public function testDataWillBeFragmentedToBufferSize()
131132
$this->loop->tick();
132133
}
133134

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+
134194
/**
135195
* @covers React\EventLoop\StreamSelectLoop::tick
136196
*/
@@ -166,6 +226,15 @@ public function testConnectionDoesEndWhenClientCloses()
166226
$this->loop->tick();
167227
}
168228

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+
169238
/**
170239
* @covers React\Socket\Server::shutdown
171240
*/

0 commit comments

Comments
 (0)