summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_poll.phpt
blob: 615c027d68482d8fb7cd550f876949ace03f9bf3 (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
--TEST--
int mysqli_poll() simple
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('connect.inc');
require_once('skipifconnectfailure.inc');

if (!$IS_MYSQLND)
    die("skip mysqlnd only feature, compile PHP using --with-mysqli=mysqlnd");
?>
--FILE--
<?php
    require_once('connect.inc');

    function get_connection() {
        global $host, $user, $passwd, $db, $port, $socket;

        if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
            printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
        return $link;
    }

    if (!$link = get_connection())
        printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());

    $read = $error = $reject = array($link);
    if (0 !== ($tmp = (mysqli_poll($read, $error, $reject, 0, 1))))
        printf("[009] Expecting int/0 got %s/%s\n", gettype($tmp), var_export($tmp, true));

    $read = $error = $reject = array($link);
    try {
        mysqli_poll($read, $error, $reject, -1, 1);
    } catch (\ValueError $e) {
        echo $e->getMessage() . \PHP_EOL;
    }
    try {
        mysqli_poll($read, $error, $reject, 0, -1);
    } catch (\ValueError $e) {
        echo $e->getMessage() . \PHP_EOL;
    }

    $link->close();
    $read[0] = get_connection();
    try {
        mysqli_poll($read, $error, $reject, 0, 1);
    } catch (\Error $e) {
        echo $e->getMessage() . \PHP_EOL;
    }

    function poll_async($offset, $link, $links, $errors, $reject, $exp_ready, $use_oo_syntax) {

        if ($exp_ready !== ($tmp = mysqli_poll($links, $errors, $reject, 0, 1000)))
            printf("[%03d + 1] There should be %d links ready to read from, %d ready\n",
                $exp_ready, $tmp);

        foreach ($links as $mysqli) {
            if ($use_oo_syntax) {
                $res = $mysqli->reap_async_query();
            } else {
                $res = mysqli_reap_async_query($mysqli);
            }
            if (is_object($res)) {
                printf("[%03d + 2] Can fetch resultset although no query has been run!\n", $offset);
            } else if (mysqli_errno($mysqli) > 0) {
                printf("[%03d + 3] Error indicated through links array: %d/%s",
                    $offset, mysqli_errno($mysqli), mysqli_error($mysqli));
            } else {
                printf("[%03d + 4] Cannot fetch and no error set - non resultset query (no SELECT)!\n", $offset);
            }
        }

        foreach ($errors as $mysqli)
            printf("[%03d + 5] Error on %d: %d/%s\n",
                $offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));

        foreach ($reject as $mysqli)
            printf("[%03d + 6] Rejecting thread %d: %d/%s\n",
                $offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));

    }

    // Connections on which no query has been send - 1
    $link = get_connection();
    $links = array($link);
    $errors = array($link);
    $reject = array($link);
    poll_async(12, $link, $links, $errors, $reject, 0, false);
    mysqli_close($link);

    $link = get_connection();
    $links = array($link);
    $errors = array($link);
    $reject = array($link);
    poll_async(13, $link, $links, $errors, $reject, 0, true);
    mysqli_close($link);

    // Connections on which no query has been send - 2
    // Difference: pass $links twice
    $link = get_connection();
    $links = array($link, $link);
    $errors = array($link, $link);
    $reject = array();
    poll_async(14, $link, $links, $errors, $reject, 0, false);

    // Connections on which no query has been send - 3
    // Difference: pass two connections
    $link = get_connection();
    $links = array($link, get_connection());
    $errors = array($link, $link);
    $reject = array();
    poll_async(15, $link, $links, $errors, $reject, 0, false);

    // Reference mess...
    $link = get_connection();
    $links = array($link);
    $errors = array($link);
    $ref_errors =& $errors;
    $reject = array();
    poll_async(16, $link, $links, $ref_errors, $reject, 0, false);

    print "done!";
?>
--EXPECTF--
mysqli_poll(): Argument #4 ($seconds) must be greater than or equal to 0
mysqli_poll(): Argument #5 ($microseconds) must be greater than or equal to 0
mysqli object is already closed
[012 + 6] Rejecting thread %d: 0/
[013 + 6] Rejecting thread %d: 0/
[014 + 6] Rejecting thread %d: 0/
[014 + 6] Rejecting thread %d: 0/
[015 + 6] Rejecting thread %d: 0/
[015 + 6] Rejecting thread %d: 0/
[016 + 6] Rejecting thread %d: 0/
done!