Skip to content

Commit

Permalink
Add fork tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dixyes committed Jul 25, 2024
1 parent 4f82e75 commit 9c59576
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
76 changes: 76 additions & 0 deletions ext/tests/swow_event/fork_worker.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
--TEST--
swow_event: fork for worker
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.php';
skip_if_extension_not_exist('pcntl');
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swow\Socket;

// create pipe
if (PHP_OS_FAMILY !== 'Windows') {
define('SOCK_NAME', '/tmp/swow_flock_' . getRandomBytes(8) . '.sock');
} else {
define('SOCK_NAME', '\\\?\pipe\swow_flock_' . getRandomBytes(8));
}

$serverSock = new Socket(Socket::TYPE_TCP);
$serverSock->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
52 changes: 52 additions & 0 deletions ext/tests/swow_event/simple_fork.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
swow_event: simple fork
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.php';
skip_if_extension_not_exist('pcntl');
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swow\Socket;

// create pipe
if (PHP_OS_FAMILY !== 'Windows') {
define('SOCK_NAME', '/tmp/swow_flock_' . getRandomBytes(8) . '.sock');
} else {
define('SOCK_NAME', '\\\?\pipe\swow_flock_' . getRandomBytes(8));
}

$serverSock = new Socket(Socket::TYPE_TCP);
$serverSock->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

0 comments on commit 9c59576

Please sign in to comment.