Skip to content

Commit

Permalink
UdpConnection test
Browse files Browse the repository at this point in the history
  • Loading branch information
jhdxr committed Apr 16, 2023
1 parent f8ee935 commit 098c8b6
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion tests/Unit/Connection/UdpConnectionTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
<?php

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

$remoteAddress = '[::1]:12345';
$process = new PhpProcess(<<<PHP
<?php
\$socketServer = stream_socket_server("udp://$remoteAddress", \$errno, \$errstr, STREAM_SERVER_BIND);
do{
\$data = stream_socket_recvfrom(\$socketServer, 3);
}while(\$data !== false && \$data !== 'bye');
PHP
);
$process->start();

it('tests ' . UdpConnection::class, function () use ($remoteAddress) {

$socketClient = stream_socket_client("udp://$remoteAddress");
$udpConnection = new UdpConnection($socketClient, $remoteAddress);
$udpConnection->protocol = \Workerman\Protocols\Text::class;
expect($udpConnection->send('foo'))->toBeTrue();

expect($udpConnection->getRemoteIp())->toBe('::1');
expect($udpConnection->getRemotePort())->toBe(12345);
expect($udpConnection->getRemoteAddress())->toBe($remoteAddress);
expect($udpConnection->getLocalIp())->toBeIn(['::1', '[::1]', '127.0.0.1']);
expect($udpConnection->getLocalPort())->toBeInt();

expect(json_encode($udpConnection))->toBeJson()
->toContain('transport')
->toContain('getRemoteIp')
->toContain('remotePort')
->toContain('getRemoteAddress')
->toContain('getLocalIp')
->toContain('getLocalPort')
->toContain('isIpV4')
->toContain('isIpV6');

$udpConnection->close('bye');
if (is_resource($socketClient)) {
fclose($socketClient);
}
});

0 comments on commit 098c8b6

Please sign in to comment.