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