UdpConnectionTest.php 1.7 KB

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