Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed May 18, 2021
1 parent 7423d14 commit 357c305
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions tests/swoole_process_pool/detach.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--TEST--
swoole_process_pool: detach
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Process\Pool;
use Swoole\Atomic;

const N = 100;

$atomic = new Atomic();

$pm = new ProcessManager;
$pm->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

0 comments on commit 357c305

Please sign in to comment.