@@ -61,4 +61,71 @@ public function notEqualTypes()
6161 array (new Uuid ('2a5072fa-7da4-4ccd-a9b4-f017a3872304 ' ), new Uuid ('3b5072fa-7da4-4ccd-a9b4-f017a3872304 ' )),
6262 );
6363 }
64+
65+ /**
66+ * Ensure UUIDs are unique for fork/child processes
67+ *
68+ * This test will ensure that the PHP driver is producing unique UUIDs for
69+ * all child processes that get created during fork() operations in web
70+ * servers (e.g. Apache, nginx, ...etc).
71+ *
72+ * @test
73+ * @ticket PHP-115
74+ */
75+ public function testUniqueInChild () {
76+ if (!function_exists ("pcntl_fork " )) {
77+ $ this ->markTestSkipped ("Unable to Execute testUniqueInChild Unit Test: pcntl_fork() does not exists " );
78+ } else {
79+ // Create a PHP script to call within a PHPUnit test (exit call fails test)
80+ $ script = <<<EOF
81+ <?php
82+ // Get and open the file for appending child UUIDs
83+ \$uuidsFilename = \$_SERVER['argv'][1];
84+ \$numberOfForks = \$_SERVER['argv'][2];
85+
86+ // Create requested children process; create UUIDs and append to a file
87+ \$children = array();
88+ foreach (range(1, \$numberOfForks) as \$i) {
89+ // Create the child process
90+ \$pid = pcntl_fork();
91+
92+ // Ensure the child process was create successfully
93+ if ( \$pid < 0) {
94+ die("Unable to Create Fork: Unique UUID test cannot complete");
95+ } else if ( \$pid === 0) {
96+ // Create a UUID and add it to the file
97+ \$uuid = new \Cassandra\Uuid();
98+ file_put_contents( \$uuidsFilename, \$uuid->uuid() . PHP_EOL, FILE_APPEND);
99+
100+ // Terminate child process
101+ exit(0);
102+ } else {
103+ // Parent process: Add the process ID to force waiting on children
104+ \$children[] = \$pid;
105+ }
106+ }
107+
108+ // Wait on each child process to finish
109+ foreach ( \$children as \$pid) {
110+ pcntl_waitpid( \$pid, \$status);
111+ }
112+ ?>
113+ EOF ;
114+ $ numProcesses = 64 ;
115+
116+ // Execute the PHP script passing in the filename for the UUIDs to be stored
117+ $ uuidsFilename = tempnam (sys_get_temp_dir (), "uuid " );
118+ $ scriptFilename = tempnam (sys_get_temp_dir (), "uuid " );
119+ file_put_contents ($ scriptFilename , $ script , FILE_APPEND );
120+ exec (PHP_BINARY . " {$ scriptFilename } {$ uuidsFilename } $ numProcesses " );
121+ unlink ($ scriptFilename );
122+
123+ // Get the contents of the file
124+ $ uuids = file ($ uuidsFilename );
125+ unlink ($ uuidsFilename );
126+
127+ // Ensure all the UUIDs are unique
128+ $ this ->assertEquals ($ numProcesses , count (array_unique ($ uuids )));
129+ }
130+ }
64131}
0 commit comments