xref: /PHP-7.0/ext/mysqli/tests/mysqli_poll.phpt (revision 63791d05)
1--TEST--
2int mysqli_poll() simple
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('connect.inc');
8require_once('skipifconnectfailure.inc');
9
10if (!$IS_MYSQLND)
11	die("skip mysqlnd only feature, compile PHP using --with-mysqli=mysqlnd");
12?>
13--FILE--
14<?php
15	require_once('connect.inc');
16
17	function get_connection() {
18		global $host, $user, $passwd, $db, $port, $socket;
19
20		if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
21			printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
22		return $link;
23	}
24
25	if (!$link = get_connection())
26		printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
27
28	if (NULL !== ($tmp = @mysqli_poll()))
29		printf("[002] Expecting NULL got %s\n", var_export($tmp, true));
30
31	$l = array($link);
32	if (NULL !== ($tmp = @mysqli_poll($l)))
33		printf("[003] Expecting NULL got %s\n", var_export($tmp, true));
34
35	$l = array($link); $n = NULL;
36	if (NULL !== ($tmp = @mysqli_poll($l, $n)))
37		printf("[004] Expecting NULL got %s\n", var_export($tmp, true));
38
39	$l = array($link); $n = NULL;
40	if (NULL !== ($tmp = @mysqli_poll($l, $n, $n)))
41		printf("[005] Expecting NULL got %s\n", var_export($tmp, true));
42
43	$l = array($link); $e = NULL; $r = NULL;
44	if (NULL !== ($tmp = @mysqli_poll($l, $e, $r, -1)))
45		printf("[007] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
46
47	$l = array($link); $e = NULL; $r = NULL;
48	if (NULL !== ($tmp = @mysqli_poll($l, $e, $r, 0, -1)))
49		printf("[008] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
50
51	$read = $error = $reject = array($link);
52	if (0 !== ($tmp = (mysqli_poll($read, $error, $reject, 0, 1))))
53		printf("[009] Expecting int/0 got %s/%s\n", gettype($tmp), var_export($tmp, true));
54
55	$read = $error = $reject = array($link);
56	if (false !== ($tmp = (mysqli_poll($read, $error, $reject, -1, 1))))
57		printf("[010] Expecting false got %s/%s\n", gettype($tmp), var_export($tmp, true));
58
59	$read = $error = $reject = array($link);
60	if (false !== ($tmp = (mysqli_poll($read, $error, $reject, 0, -1))))
61		printf("[011] Expecting false got %s/%s\n", gettype($tmp), var_export($tmp, true));
62
63	function poll_async($offset, $link, $links, $errors, $reject, $exp_ready, $use_oo_syntax) {
64
65		if ($exp_ready !== ($tmp = mysqli_poll($links, $errors, $reject, 0, 1000)))
66			printf("[%03d + 1] There should be %d links ready to read from, %d ready\n",
67				$exp_ready, $tmp);
68
69		foreach ($links as $mysqli) {
70			if ($use_oo_syntax) {
71				$res = $mysqli->reap_async_query();
72			} else {
73				$res = mysqli_reap_async_query($mysqli);
74			}
75			if (is_object($res)) {
76				printf("[%03d + 2] Can fetch resultset although no query has been run!\n", $offset);
77			} else if (mysqli_errno($mysqli) > 0) {
78				printf("[%03d + 3] Error indicated through links array: %d/%s",
79					$offset, mysqli_errno($mysqli), mysqli_error($mysqli));
80			} else {
81				printf("[%03d + 4] Cannot fetch and no error set - non resultset query (no SELECT)!\n", $offset);
82			}
83		}
84
85		foreach ($errors as $mysqli)
86			printf("[%03d + 5] Error on %d: %d/%s\n",
87				$offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));
88
89		foreach ($reject as $mysqli)
90			printf("[%03d + 6] Rejecting thread %d: %d/%s\n",
91				$offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));
92
93	}
94
95	// Connections on which no query has been send - 1
96	$link = get_connection();
97	$links = array($link);
98	$errors = array($link);
99	$reject = array($link);
100	poll_async(12, $link, $links, $errors, $reject, 0, false);
101	mysqli_close($link);
102
103	$link = get_connection();
104	$links = array($link);
105	$errors = array($link);
106	$reject = array($link);
107	poll_async(13, $link, $links, $errors, $reject, 0, true);
108	mysqli_close($link);
109
110	// Connections on which no query has been send - 2
111	// Difference: pass $links twice
112	$link = get_connection();
113	$links = array($link, $link);
114	$errors = array($link, $link);
115	$reject = array();
116	poll_async(14, $link, $links, $errors, $reject, 0, false);
117
118	// Connections on which no query has been send - 3
119	// Difference: pass two connections
120	$link = get_connection();
121	$links = array($link, get_connection());
122	$errors = array($link, $link);
123	$reject = array();
124	poll_async(15, $link, $links, $errors, $reject, 0, false);
125
126	// Reference mess...
127	$link = get_connection();
128	$links = array($link);
129	$errors = array($link);
130	$ref_errors =& $errors;
131	$reject = array();
132	poll_async(16, $link, $links, $ref_errors, $reject, 0, false);
133
134	print "done!";
135?>
136--EXPECTF--
137
138Warning: mysqli_poll(): Negative values passed for sec and/or usec in %s on line %d
139
140Warning: mysqli_poll(): Negative values passed for sec and/or usec in %s on line %d
141[012 + 6] Rejecting thread %d: 0/
142[013 + 6] Rejecting thread %d: 0/
143[014 + 6] Rejecting thread %d: 0/
144[014 + 6] Rejecting thread %d: 0/
145[015 + 6] Rejecting thread %d: 0/
146[015 + 6] Rejecting thread %d: 0/
147[016 + 6] Rejecting thread %d: 0/
148done!