public function testUniqueInChild()
{
if (!function_exists("pcntl_fork")) {
$this->markTestSkipped("Unable to Execute testUniqueInChild Unit Test: pcntl_fork() does not exists");
} else {
// Create a PHP script to call within a PHPUnit test (exit call fails test)
$script = <<<EOF
<?php
// Get and open the file for appending child UUIDs
\$uuidsFilename = \$_SERVER['argv'][1];
\$numberOfForks = \$_SERVER['argv'][2];
// Create requested children process; create UUIDs and append to a file
\$children = array();
foreach (range(1, \$numberOfForks) as \$i) {
// Create the child process
\$pid = pcntl_fork();
// Ensure the child process was create successfully
if (\$pid < 0) {
die("Unable to Create Fork: Unique UUID test cannot complete");
} else if (\$pid === 0) {
// Create a UUID and add it to the file
\$uuid = new \\Cassandra\\Uuid();
file_put_contents(\$uuidsFilename, \$uuid->uuid() . PHP_EOL, FILE_APPEND);
// Terminate child process
exit(0);
} else {
// Parent process: Add the process ID to force waiting on children
\$children[] = \$pid;
}
}
// Wait on each child process to finish
foreach (\$children as \$pid) {
pcntl_waitpid(\$pid, \$status);
}
?>
EOF;
$numProcesses = 64;
// Execute the PHP script passing in the filename for the UUIDs to be stored
$uuidsFilename = tempnam(sys_get_temp_dir(), "uuid");
$scriptFilename = tempnam(sys_get_temp_dir(), "uuid");
file_put_contents($scriptFilename, $script, FILE_APPEND);
exec(PHP_BINARY . " {$scriptFilename} {$uuidsFilename} {$numProcesses}");
unlink($scriptFilename);
// Get the contents of the file
$uuids = file($uuidsFilename);
unlink($uuidsFilename);
// Ensure all the UUIDs are unique
$this->assertEquals($numProcesses, count(array_unique($uuids)));
}
}