From 9c59576d1993f60750720700496b1675fda31373 Mon Sep 17 00:00:00 2001 From: dixyes Date: Fri, 26 Jul 2024 00:12:22 +0800 Subject: [PATCH] Add fork tests --- ext/tests/swow_event/fork_worker.phpt | 76 +++++++++++++++++++++++++++ ext/tests/swow_event/simple_fork.phpt | 52 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 ext/tests/swow_event/fork_worker.phpt create mode 100644 ext/tests/swow_event/simple_fork.phpt diff --git a/ext/tests/swow_event/fork_worker.phpt b/ext/tests/swow_event/fork_worker.phpt new file mode 100644 index 00000000..8e89fc04 --- /dev/null +++ b/ext/tests/swow_event/fork_worker.phpt @@ -0,0 +1,76 @@ +--TEST-- +swow_event: fork for worker +--SKIPIF-- + +--FILE-- +bind('127.0.0.1', 0)->listen(); +$port = $serverSock->getSockPort(); + +$pid = pcntl_fork(); +if ($pid === -1) { + // fork failed + printf("Fork failed\n"); + exit(1); +} +if ($pid === 0) { + // child + printf("Child PID: %d\n", getmypid()); + $conn = $serverSock->accept(1000); + + // receive work + $lenWork = $conn->readString(4); + $work = $conn->readString(unpack('N', $lenWork)[1]); + $workload = unserialize($work); + // do work + $result = $workload['work'](...$workload['args']); + // send result + $result = serialize($result); + $resultLen = pack('N', strlen($result)); + $conn->send($resultLen . $result); +} else { + // parent + printf("Parent PID: %d\n", getmypid()); + // close server connection + $serverSock->close(); + // create new connection + $clientSock = new Socket(Socket::TYPE_TCP); + $conn = $clientSock->connect('127.0.0.1', $port); + // send work + $work = serialize([ + 'work' => static function (int $a, int $b): int { + printf("Calculate at pid %d\n", getmypid()); + return $a * $b; + }, + 'args' => [6, 7], + ]); + $conn->send(pack('N', strlen($work)) . $work); + $resultLen = $conn->readString(4); + $result = unserialize($conn->readString(unpack('N', $resultLen)[1])); + printf("The answer to life, the universe, and everything is %d\n", $result); + // receive result + Assert::same($result, 42); + printf("Done\n"); +} +?> +--EXPECTF-- +%s PID: %d +%s PID: %d +Calculate at pid %d +The answer to life, the universe, and everything is 42 +Done diff --git a/ext/tests/swow_event/simple_fork.phpt b/ext/tests/swow_event/simple_fork.phpt new file mode 100644 index 00000000..991f38c7 --- /dev/null +++ b/ext/tests/swow_event/simple_fork.phpt @@ -0,0 +1,52 @@ +--TEST-- +swow_event: simple fork +--SKIPIF-- + +--FILE-- +bind('127.0.0.1', 0)->listen(); +$port = $serverSock->getSockPort(); + +$pid = pcntl_fork(); +if ($pid === -1) { + // fork failed + printf("Fork failed\n"); + exit(1); +} +if ($pid === 0) { + // child + printf("Child PID: %d\n", getmypid()); + $conn = $serverSock->accept(1000); + $conn->send('hello'); +} else { + // parent + printf("Parent PID: %d\n", getmypid()); + // close server connection + $serverSock->close(); + // create new connection + $clientSock = new Socket(Socket::TYPE_TCP); + $conn = $clientSock->connect('127.0.0.1', $port); + $msg = $conn->readString(5); + Assert::same($msg, 'hello'); + printf("Done\n"); +} +?> +--EXPECTF-- +%s PID: %d +%s PID: %d +Done