@@ -61,4 +61,70 @@ public function notEqualTypes()
61
61
array (new Uuid ('2a5072fa-7da4-4ccd-a9b4-f017a3872304 ' ), new Uuid ('3b5072fa-7da4-4ccd-a9b4-f017a3872304 ' )),
62
62
);
63
63
}
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
+
115
+ // Execute the PHP script passing in the filename for the UUIDs to be stored
116
+ $ uuidsFilename = tempnam (sys_get_temp_dir (), "uuid " );
117
+ $ scriptFilename = tempnam (sys_get_temp_dir (), "uuid " );
118
+ file_put_contents ($ scriptFilename , $ script , FILE_APPEND );
119
+ exec (PHP_BINARY . " {$ scriptFilename } {$ uuidsFilename } 1024 " );
120
+ unlink ($ scriptFilename );
121
+
122
+ // Get the contents of the file
123
+ $ uuids = file ($ uuidsFilename );
124
+ unlink ($ uuidsFilename );
125
+
126
+ // Ensure all the UUIDs are unique
127
+ $ this ->assertEquals (1024 , count (array_unique ($ uuids )));
128
+ }
129
+ }
64
130
}
0 commit comments