Skip to content

Commit f24d86f

Browse files
committed
UdpConnection test
1 parent e06475f commit f24d86f

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
<?php
22

3-
it('tests udp connection', function (){
3+
use Workerman\Connection\UdpConnection;
4+
use Symfony\Component\Process\PhpProcess;
45

6+
$remoteAddress = '[::1]:12345';
7+
$process = new PhpProcess(<<<PHP
8+
<?php
9+
\$socketServer = stream_socket_server("udp://$remoteAddress");
10+
do{
11+
\$data = stream_socket_recvfrom(\$socketServer, 3);
12+
}while(\$data !== false && \$data !== 'bye');
13+
PHP
14+
);
15+
$process->run();
16+
17+
it('tests ' . UdpConnection::class, function () use ($remoteAddress) {
18+
19+
$socketClient = stream_socket_client("udp://$remoteAddress");
20+
$udpConnection = new UdpConnection($socketClient, $remoteAddress);
21+
$udpConnection->protocol = \Workerman\Protocols\Text::class;
22+
expect($udpConnection->send('foo'))->toBeTrue();
23+
24+
expect($udpConnection->getRemoteIp())->toBe('::1');
25+
expect($udpConnection->getRemotePort())->toBe(12345);
26+
expect($udpConnection->getRemoteAddress())->toBe($remoteAddress);
27+
expect($udpConnection->getLocalIp())->toBeIn(['::1', '[::1]', '127.0.0.1']);
28+
expect($udpConnection->getLocalPort())->toBeInt();
29+
30+
expect(json_encode($udpConnection))->toBeJson()
31+
->toContain('transport')
32+
->toContain('getRemoteIp')
33+
->toContain('remotePort')
34+
->toContain('getRemoteAddress')
35+
->toContain('getLocalIp')
36+
->toContain('getLocalPort')
37+
->toContain('isIpV4')
38+
->toContain('isIpV6');
39+
40+
$udpConnection->close('bye');
41+
if (is_resource($socketClient)) {
42+
fclose($socketClient);
43+
}
544
});

0 commit comments

Comments
 (0)