forked from reactphp/datagram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketTest.php
More file actions
222 lines (176 loc) · 6.04 KB
/
SocketTest.php
File metadata and controls
222 lines (176 loc) · 6.04 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace React\Tests\Datagram;
use React\Datagram\Socket;
class SocketTest extends TestCase
{
private $loop;
private $factory;
/**
* @before
*/
public function setUpFactory()
{
$this->loop = \React\EventLoop\Factory::create();
$this->factory = new \React\Datagram\Factory($this->loop, $this->createResolverMock());
}
public function testCtorThrowsForInvalidBuffer()
{
$socket = stream_socket_server('udp://127.0.0.1:0', $errno, $errstr, STREAM_SERVER_BIND);
assert(is_resource($socket));
$this->setExpectedException('InvalidArgumentException', 'Argument #3 ($buffer) expected null|React\Datagram\Buffer');
new Socket($this->loop, $socket, 'buffer');
}
/**
* @doesNotPerformAssertions
*/
public function testCreateClientCloseWillNotBlock()
{
$promise = $this->factory->createClient('127.0.0.1:12345');
$client = \React\Async\await($promise);
$client->send('test');
$client->close();
$this->loop->run();
return $client;
}
/**
* @doesNotPerformAssertions
* @param Socket $client
* @depends testCreateClientCloseWillNotBlock
*/
public function testClientCloseAgainWillNotBlock(Socket $client)
{
$client->close();
$this->loop->run();
}
/**
* @doesNotPerformAssertions
*/
public function testCreateClientEndWillNotBlock()
{
$promise = $this->factory->createClient('127.0.0.1:12345');
$client = \React\Async\await($promise);
$client->send('test');
$client->end();
$this->loop->run();
return $client;
}
/**
* @doesNotPerformAssertions
* @param Socket $client
* @depends testCreateClientEndWillNotBlock
*/
public function testClientEndAgainWillNotBlock(Socket $client)
{
$client->end();
$this->loop->run();
return $client;
}
/**
* @doesNotPerformAssertions
* @param Socket $client
* @depends testClientEndAgainWillNotBlock
*/
public function testClientSendAfterEndIsNoop(Socket $client)
{
$client->send('does not matter');
$this->loop->run();
}
public function testClientSendHugeWillFailWithoutCallingCustomErrorHandler()
{
$promise = $this->factory->createClient('127.0.0.1:12345');
$client = \React\Async\await($promise);
$client->send(str_repeat(1, 1024 * 1024));
$client->on('error', $this->expectCallableOnce());
$client->end();
$error = null;
set_error_handler(function ($_, $errstr) use (&$error) {
$error = $errstr;
});
$this->loop->run();
restore_error_handler();
$this->assertNull($error);
}
public function testClientSendNoServerWillFail()
{
$promise = $this->factory->createClient('127.0.0.1:1234');
$client = \React\Async\await($promise);
// send a message to a socket that is not actually listening
// expect the remote end to reject this by sending an ICMP message
// which we will receive as an error message. This depends on the
// host to actually reject UDP datagrams, which not all systems do.
$client->send('hello');
$client->on('error', $this->expectCallableOnce());
$loop = $this->loop;
$client->on('error', function () use ($loop) {
$loop->stop();
});
$that = $this;
$this->loop->addTimer(1.0, function () use ($that, $loop) {
$loop->stop();
$that->markTestSkipped('UDP packet was not rejected after 0.5s, ignoring test');
});
$this->loop->run();
}
public function testCreatePair()
{
$promise = $this->factory->createServer('127.0.0.1:0');
$server = \React\Async\await($promise);
$promise = $this->factory->createClient($server->getLocalAddress());
$client = \React\Async\await($promise);
$that = $this;
$server->on('message', function ($message, $remote, $server) use ($that) {
$that->assertEquals('test', $message);
// once the server receives a message, send it pack to client and stop server
$server->send('response:' . $message, $remote);
$server->end();
});
$client->on('message', function ($message, $remote, $client) use ($that) {
$that->assertEquals('response:test', $message);
// once the client receives a message, stop client
$client->end();
});
$client->send('test');
$this->loop->run();
}
public function provideSanitizeAddress()
{
return array(
array(
'127.0.0.1:1337',
),
array(
'[::1]:1337',
),
);
}
/**
* @dataProvider provideSanitizeAddress
*/
public function testSanitizeAddress($address)
{
$promise = $this->factory->createServer($address);
try {
$server = \React\Async\await($promise);
} catch (\Exception $e) {
if (strpos($address, '[') === false) {
throw $e;
}
$this->markTestSkipped('Unable to start IPv6 server socket (IPv6 not supported on this system?)');
}
$promise = $this->factory->createClient($server->getLocalAddress());
$client = \React\Async\await($promise);
$that = $this;
$server->on('message', function ($message, $remote, $server) use ($that) {
// once the server receives a message, send it pack to client and stop server
$server->send('response:' . $message, $remote);
$server->end();
});
$client->on('message', function ($message, $remote, $client) use ($that, $address) {
$that->assertEquals($address, $remote);
// once the client receives a message, stop client
$client->end();
});
$client->send('test');
$this->loop->run();
}
}