forked from reactphp/reactphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerTest.php
More file actions
193 lines (160 loc) · 5.02 KB
/
Copy pathServerTest.php
File metadata and controls
193 lines (160 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace React\Tests\Socket;
use React\Socket\Server;
use React\EventLoop\StreamSelectLoop;
class ServerTest extends TestCase
{
private $loop;
private $server;
private $port;
private function createLoop()
{
return new StreamSelectLoop();
}
/**
* @covers React\Socket\Server::__construct
* @covers React\Socket\Server::listen
* @covers React\Socket\Server::getPort
*/
public function setUp()
{
$this->loop = $this->createLoop();
$this->server = new Server($this->loop);
$this->server->listen(0);
$this->port = $this->server->getPort();
}
/**
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Server::handleConnection
* @covers React\Socket\Server::createConnection
*/
public function testConnection()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);
$this->server->on('connection', $this->expectCallableOnce());
$this->loop->tick();
}
/**
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Server::handleConnection
* @covers React\Socket\Server::createConnection
*/
public function testConnectionWithManyClients()
{
$client1 = stream_socket_client('tcp://localhost:'.$this->port);
$client2 = stream_socket_client('tcp://localhost:'.$this->port);
$client3 = stream_socket_client('tcp://localhost:'.$this->port);
$this->server->on('connection', $this->expectCallableExactly(3));
$this->loop->tick();
$this->loop->tick();
$this->loop->tick();
}
/**
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Connection::handleData
*/
public function testDataWithNoData()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);
$mock = $this->expectCallableNever();
$this->server->on('connection', function ($conn) use ($mock) {
$conn->on('data', $mock);
});
$this->loop->tick();
$this->loop->tick();
}
/**
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Connection::handleData
*/
public function testData()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);
fwrite($client, "foo\n");
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with("foo\n");
$this->server->on('connection', function ($conn) use ($mock) {
$conn->on('data', $mock);
});
$this->loop->tick();
$this->loop->tick();
}
/**
* Test data sent from python language
*
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Connection::handleData
*/
public function testDataSentFromPy()
{
$client = stream_socket_client('tcp://localhost:' . $this->port);
fwrite($client, "foo\n");
stream_socket_shutdown($client, STREAM_SHUT_WR);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with("foo\n");
$this->server->on('connection', function ($conn) use ($mock) {
$conn->on('data', $mock);
});
$this->loop->tick();
$this->loop->tick();
}
public function testFragmentedMessage()
{
$client = stream_socket_client('tcp://localhost:' . $this->port);
fwrite($client, "Hello World!\n");
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with("He");
$this->server->on('connection', function ($conn) use ($mock) {
$conn->bufferSize = 2;
$conn->on('data', $mock);
});
$this->loop->tick();
$this->loop->tick();
}
/**
* @covers React\EventLoop\StreamSelectLoop::tick
*/
public function testDisconnectWithoutDisconnect()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);
$mock = $this->expectCallableNever();
$this->server->on('connection', function ($conn) use ($mock) {
$conn->on('end', $mock);
});
$this->loop->tick();
$this->loop->tick();
}
/**
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Connection::end
*/
public function testDisconnect()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);
fclose($client);
$mock = $this->expectCallableOnce();
$this->server->on('connection', function ($conn) use ($mock) {
$conn->on('end', $mock);
});
$this->loop->tick();
$this->loop->tick();
}
/**
* @covers React\Socket\Server::shutdown
*/
public function tearDown()
{
if ($this->server) {
$this->server->shutdown();
}
}
}