summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_fork.phpt
blob: aab134fbba4ba78942e963ad054600f05c6de339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
--TEST--
Forking a child and using the same connection.
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifemb.inc');
require_once('skipifconnectfailure.inc');

if (!function_exists('pcntl_fork'))
	die("skip Process Control Functions not available");

if (!function_exists('posix_getpid'))
	die("skip POSIX functions not available");

require_once('connect.inc');
if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
	die(sprintf("Cannot connect, [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));

if (!have_innodb($link))
	die(sprintf("Needs InnoDB support, [%d] %s", $link->errno, $link->error));
?>
--FILE--
<?php
	require_once("table.inc");

	$res = mysqli_query($link, "SELECT 'dumped by the parent' AS message");
	$pid = pcntl_fork();
	switch ($pid) {
		case -1:
			printf("[001] Cannot fork child");
			break;

		case 0:
			/* child */
			exit(0);
			break;

		default:
			/* parent */
			$status = null;
			$wait_id = pcntl_waitpid($pid, $status);
			if (pcntl_wifexited($status) && (0 != ($tmp = pcntl_wexitstatus($status)))) {
				printf("Exit code: %s\n", (pcntl_wifexited($status)) ? pcntl_wexitstatus($status) : 'n/a');
				printf("Signal: %s\n", (pcntl_wifsignaled($status)) ? pcntl_wtermsig($status) : 'n/a');
				printf("Stopped: %d\n", (pcntl_wifstopped($status)) ? pcntl_wstopsig($status) : 'n/a');
			}
			var_dump(mysqli_fetch_assoc($res));
			mysqli_free_result($res);
			break;
	}

	if (@mysqli_query($link, "SELECT id FROM test WHERE id = 1"))
		printf("[003] Expecting error and closed connection, child exit should have closed connection\n");
	else if ((($errno = mysqli_errno($link)) == 0) || ('' == ($error = mysqli_error($link))))
		printf("[004] Expecting error string and error code from MySQL, got errno = %s/%s, error = %s/%s\n",
			gettype($errno), $errno, gettype($error), $error);

	mysqli_close($link);
	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
		printf("[005] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
			$host, $user, $db, $port, $socket);

	/* non trivial tests require a message list for parent-child communication */
	if (!mysqli_query($link, "DROP TABLE IF EXISTS messages"))
		printf("[006] [%d] %s\n", mysqli_error($link), mysqli_errno($link));

	if (!mysqli_query($link, "CREATE TABLE messages(
		msg_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
		msg_time TIMESTAMP,
		pid INT NOT NULL,
		sender ENUM('child', 'parent') NOT NULL,
		msg TEXT) ENGINE = InnoDB"))
		printf("[007] [%d] %s\n", mysqli_error($link), mysqli_errno($link));

	mysqli_autocommit($link, false);
	if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id ASC LIMIT 3", MYSQLI_USE_RESULT))
		printf("[008] [%d] %s\n", mysqli_error($link), mysqli_errno($link));

	$pid = pcntl_fork();

	switch ($pid) {
		case -1:
			printf("[009] Cannot fork child");
			break;

		case 0:
			/* child */
			if (!($plink = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) || !mysqli_autocommit($plink, true))
				exit(mysqli_errno($plink));

			$sql = sprintf("INSERT INTO messages(pid, sender, msg) VALUES (%d, 'child', '%%s')", posix_getpid());
			if (!mysqli_query($plink, sprintf($sql, 'start')))
				exit(mysqli_errno($plink));

			$parent_sql = sprintf("SELECT msg_id, msg_time, msg FROM messages WHERE pid = %d  AND sender = 'parent' ORDER BY msg_id DESC LIMIT 1", posix_getppid());
			$msg_id = 0;
			while ($row = mysqli_fetch_assoc($res)) {
				/* send row to parent */
				ob_start();
				var_dump($row);
				$tmp = ob_get_contents();
				ob_end_clean();
				if (!mysqli_query($plink, sprintf($sql, $tmp)))
					exit(mysqli_errno($plink));

				/* let the parent reply... */
				$start = time();
				do {
					usleep(100);
					if (!$pres = mysqli_query($plink, $parent_sql))
						continue;
					$tmp = mysqli_fetch_assoc($pres);
					mysqli_free_result($pres);
					if ($tmp['msg_id'] == $msg_id)
						/* no new message */
						continue;
					if ($tmp['msg'] == 'stop')
						break 2;
					$msg_id = $tmp['msg_id'];
					break;
				} while ((time() - $start) < 5);

			}

			if (!mysqli_query($plink, sprintf($sql, 'stop')) || !mysqli_commit($link))
				exit(mysqli_errno($plink));
			exit(0);
			break;

		default:
			/* parent */
			if (!$plink = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
					printf("[010] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
					$host, $user, $db, $port, $socket);

			$status = null;
			$start = time();
			$sql = sprintf("SELECT msg_id, msg_time, msg FROM messages WHERE pid = %d AND sender = 'child' ORDER BY msg_id DESC LIMIT 1", $pid);
			$parent_sql = sprintf("INSERT INTO messages (pid, sender, msg) VALUES (%d, 'parent', '%%s')", posix_getpid());
			$last_msg_id = 0;
			$num_rows = 0;
			do {
				$wait_id = pcntl_waitpid($pid, $status, WNOHANG);
				if ($pres = mysqli_query($plink, $sql)) {
					$row = mysqli_fetch_assoc($pres);
					if ($row['msg_id'] != $last_msg_id) {
						$last_msg_id = $row['msg_id'];
						switch ($row['msg']) {
							case 'start':
								break;
							case 'stop':
								break 2;
							default:
								/* client has started fetching rows */
								$client_row = $row['msg'];

								$num_rows++;
								if ($num_rows > 3) {
									printf("[011] Child has fetched more than three rows!\n");
									var_dump($client_row);
									if (!mysqli_query($plink, sprintf($parent_sql, 'stop'))) {
										printf("[012] Parent cannot inform child\n", mysqli_errno($plink), mysqli_error($plink));
									}
									break 2;
								}

								if (!$parent_row = mysqli_fetch_assoc($res)) {
									printf("[013] Parent cannot fetch row %d\n", $num_rows, mysqli_errno($link), mysqli_error($link));
									if (!mysqli_query($plink, sprintf($parent_sql, 'stop'))) {
										printf("[014] Parent cannot inform child\n", mysqli_errno($plink), mysqli_error($plink));
									}
									break 2;
								}

								ob_start();
								var_dump($parent_row);
								$parent_row = ob_get_contents();
								ob_end_clean();

								if ($parent_row != $client_row) {
									printf("[015] Child indicates different results than parent.\n");
									var_dump($child_row);
									var_dump($parent_row);
									if (!mysqli_query($plink, sprintf($parent_sql, 'stop'))) {
										printf("[016] Parent cannot inform child\n", mysqli_errno($plink), mysqli_error($plink));
									}
									break 2;
								}

								if (!mysqli_query($plink, sprintf($parent_sql, 'continue'))) {
									printf("[017] Parent cannot inform child to continue.\n", mysqli_errno($plink), mysqli_error($plink));
								}
								break;
						}
					}
					mysqli_free_result($pres);
				}
				usleep(100);
			} while (((time() - $start) < 5) && ($num_rows < 3));
			mysqli_close($plink);
			$wait_id = pcntl_waitpid($pid, $status);
			if (pcntl_wifexited($status) && (0 != ($tmp = pcntl_wexitstatus($status)))) {
				printf("Exit code: %s\n", (pcntl_wifexited($status)) ? pcntl_wexitstatus($status) : 'n/a');
				printf("Signal: %s\n", (pcntl_wifsignaled($status)) ? pcntl_wtermsig($status) : 'n/a');
				printf("Stopped: %d\n", (pcntl_wifstopped($status)) ? pcntl_wstopsig($status) : 'n/a');
			}
			break;
	}
	mysqli_free_result($res);
	mysqli_close($link);

	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
		printf("[018] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
			$host, $user, $db, $port, $socket);

	if (!$res = mysqli_query($link, "SELECT sender, msg FROM messages ORDER BY msg_id ASC"))
		printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	while ($row = mysqli_fetch_assoc($res))
		printf("%10s %s\n", $row['sender'], substr($row['msg'], 0, 5));
	mysqli_free_result($res);

	print "done!";
?>
--CLEAN--
<?php
require_once("connect.inc");
if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());

if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
	printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));

if (!mysqli_query($link, "DROP TABLE IF EXISTS messages"))
	printf("[c003] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));

mysqli_close($link);
?>
--EXPECTF--
array(1) {
  ["message"]=>
  string(20) "dumped by the parent"
}
     child start
     child array
    parent conti
     child array
    parent conti
     child array
    parent conti
     child stop
done!