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