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