瀏覽代碼

Stop the process in `afterAll`

Chance 2 年之前
父節點
當前提交
10569a751a
共有 3 個文件被更改,包括 13 次插入16 次删除
  1. 5 9
      tests/Feature/UdpConnectionTest.php
  2. 0 5
      tests/Pest.php
  3. 8 2
      tests/Unit/Connection/UdpConnectionTest.php

+ 5 - 9
tests/Feature/UdpConnectionTest.php

@@ -4,7 +4,8 @@ use Symfony\Component\Process\PhpProcess;
 use Workerman\Worker;
 
 $serverAddress = 'udp://127.0.0.1:6789';
-beforeAll(function () use ($serverAddress) {
+$process = null;
+beforeAll(function () use ($serverAddress, &$process) {
     $process = new PhpProcess(<<<PHP
         <?php    
         if(!defined('STDIN')) define('STDIN', fopen('php://stdin', 'r'));
@@ -15,9 +16,6 @@ beforeAll(function () use ($serverAddress) {
         
         \$server = new Worker('$serverAddress');
         \$server->onMessage = function (\$connection, \$data) {
-            if(str_starts_with(\$data, 'bye')) {
-                terminate_current_process();
-            }
             \$connection->send('received: '.\$data);
         };
         Worker::\$command = 'start';
@@ -25,13 +23,11 @@ beforeAll(function () use ($serverAddress) {
     PHP
     );
     $process->start();
-    sleep(5);
+    sleep(1);
 });
 
-afterAll(function () use ($serverAddress) {
-    $socket = stream_socket_client($serverAddress, timeout: 1);
-    fwrite($socket, 'bye');
-    fclose($socket);
+afterAll(function () use (&$process) {
+    $process->stop();
 });
 
 it('tests udp connection', function () use ($serverAddress) {

+ 0 - 5
tests/Pest.php

@@ -58,8 +58,3 @@ function testWithConnectionClose(Closure $closure, string $dataContains = null,
         $tcpConnection->shouldHaveReceived('close');
     }
 }
-
-function terminate_current_process()
-{
-    posix_kill(posix_getppid(), SIGINT);
-}

+ 8 - 2
tests/Unit/Connection/UdpConnectionTest.php

@@ -5,16 +5,22 @@ use Symfony\Component\Process\PhpProcess;
 use Workerman\Protocols\Text;
 
 $remoteAddress = '[::1]:12345';
-beforeAll(function () use ($remoteAddress) {
+$process = null;
+beforeAll(function () use ($remoteAddress, &$process) {
     $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
+    PHP
     );
     $process->start();
+    sleep(1);
+});
+
+afterAll(function () use (&$process) {
+    $process->stop();
 });
 
 it('tests ' . UdpConnection::class, function () use ($remoteAddress) {