From 357c305fff44c3885e132e7e3ed2a11a016d7d65 Mon Sep 17 00:00:00 2001 From: matyhtf Date: Tue, 18 May 2021 16:38:49 +0800 Subject: [PATCH] add tests --- tests/swoole_process_pool/detach.phpt | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/swoole_process_pool/detach.phpt diff --git a/tests/swoole_process_pool/detach.phpt b/tests/swoole_process_pool/detach.phpt new file mode 100644 index 00000000000..a7b92ef52ba --- /dev/null +++ b/tests/swoole_process_pool/detach.phpt @@ -0,0 +1,68 @@ +--TEST-- +swoole_process_pool: detach +--SKIPIF-- + +--FILE-- +initFreePorts(); + +$pm->parentFunc = function ($pid) use ($pm, $atomic) { + foreach (range(1, 2) as $i) { + $fp = stream_socket_client("tcp://127.0.0.1:".$pm->getFreePort(), $errno, $errstr) or die("error: $errstr\n"); + $msg = "HELLO-{$i}"; + fwrite($fp, pack('N', strlen($msg)) . $msg); + } + $pm->wait(); + Assert::eq($atomic->get(), N + 1); + echo "DONE\n"; + $pm->kill(); +}; + +$pm->childFunc = function () use ($pm, $atomic) { + $pool = new Pool(1, SWOOLE_IPC_SOCKET); + + $pool->on('WorkerStart', function (Pool $pool, $workerId) use($pm, $atomic) { + echo("[Worker #{$workerId}] WorkerStart\n"); + if ($atomic->get() == 0) { + $pm->wakeup(); + } + }); + + $pool->on('Message', function (Pool $pool, $msg) use($pm, $atomic) { + if ($atomic->get() == 0) { + $atomic->add(); + $pool->detach(); + $n = N; + while($n--) { + usleep(1000); + $atomic->add(); + } + $pm->wakeup(); + } else { + echo $msg.PHP_EOL; + } + }); + + $pool->listen('127.0.0.1', $pm->getFreePort()); + $pool->start(); +}; + +$pm->childFirst(); +$pm->run(); +?> +--EXPECT-- +[Worker #0] WorkerStart +[Worker #0] WorkerStart +HELLO-2 +DONE