summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_poll_reference.phpt
blob: ca084aa63c64b8e2062c8cab5f15d4cc67298e42 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
--TEST--
mysqli_poll() & references
--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");

if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
    die("skip cannot connect");

if (mysqli_get_server_version($link) < 50012)
    die("skip Test needs SQL function SLEEP() available as of MySQL 5.0.12");

?>
--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;
    }


    $mysqli1 = get_connection();
    $mysqli2 = get_connection();

    var_dump(mysqli_query($mysqli1, "SELECT SLEEP(0.10)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));
    var_dump(mysqli_query($mysqli2, "SELECT SLEEP(0.20)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));

    $processed = $loops = 0;
    do {
        $loops++;
        if ($loops > 10) {
            printf("[002] The queries should have finished already\n");
            break;
        }
        // WARNING: All arrays point to the same object - this will give bogus results!
        // The behaviour is in line with stream_select(). Be warned, be careful.
        $links = $errors = $reject = array($mysqli1, $mysqli2);
        if (0 == ($ready = mysqli_poll($links, $errors, $reject, 0, 50000))) {
            continue;
        }

        foreach ($links as $link) {
            if ($res = mysqli_reap_async_query($link)) {
                mysqli_free_result($res);
            }
            $processed++;
        }
    } while ($processed < 2);

    mysqli_close($mysqli1);
    mysqli_close($mysqli2);

    $mysqli1 = get_connection();
    $mysqli2 = get_connection();

    var_dump(mysqli_query($mysqli1, "SELECT SLEEP(0.10)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));
    var_dump(mysqli_query($mysqli2, "SELECT SLEEP(0.20)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));

    $processed = $loops = 0;
    do {
        $loops++;
        if ($loops > 10) {
            printf("[003] The queries should have finished already\n");
            break;
        }
        // WARNING: All arrays point to the same object - this will give bogus results!
        $links = $errors = array($mysqli1, $mysqli2);
        $reject = array($mysqli1, $mysqli2);
        if (0 == ($ready = mysqli_poll($links, $errors, $reject, 0, 50000))) {
            continue;
        }
        foreach ($links as $link) {
            if ($res = mysqli_reap_async_query($link)) {
                mysqli_free_result($res);
            }
            $processed++;
        }
    } while ($processed < 2);

    mysqli_close($mysqli1);
    mysqli_close($mysqli2);

    $mysqli1 = get_connection();
    $mysqli2 = get_connection();

    var_dump(mysqli_query($mysqli1, "SELECT SLEEP(0.10)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));
    var_dump(mysqli_query($mysqli2, "SELECT SLEEP(0.20)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));

    $processed = $loops = 0;
    do {
        $loops++;
        if ($loops > 10) {
            printf("[004] The queries should have finished already\n");
            break;
        }
        // WARNING: All arrays point to the same object - this will give bogus results!
        $links = array($mysqli1, $mysqli2);
        $errors = $reject = array($mysqli1, $mysqli2);
        if (0 == ($ready = mysqli_poll($links, $errors, $reject, 0, 50000))) {
            continue;
        }
        foreach ($links as $link) {
            if ($res = mysqli_reap_async_query($link)) {
                mysqli_free_result($res);
            }
            $processed++;
        }
    } while ($processed < 2);

    mysqli_close($mysqli1);
    mysqli_close($mysqli2);

    // This is bogus code and bogus usage - OK to throw no errors!
    $mysqli1 = get_connection();
    $mysqli2 = get_connection();

    var_dump(mysqli_query($mysqli1, "SELECT SLEEP(0.10)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));
    $thread_id = mysqli_thread_id($mysqli2);
    printf("Connection %d should be rejected...\n", $thread_id);

    $processed = $loops = 0;
    do {
        $loops++;
        if ($loops > 10) {
            printf("[005] The queries should have finished already\n");
            break;
        }
        $links = $errors = $reject = array($mysqli1, $mysqli2);
        if (0 == ($ready = mysqli_poll($links, $errors, $reject, 0, 50000))) {
            continue;
        }
        // WARNING: Due to the reference issue none of these should ever fire!
        foreach ($reject as $link) {
            printf("Connection %d was rejected...\n", mysqli_thread_id($link));
            if (mysqli_thread_id($link) != $thread_id) {
                printf("[006] Connector %d should have been rejected. But also %d has been rejected.",
                  $thread_id, mysqli_thread_id($link));
            }
            $processed++;
        }
        foreach ($errors as $link) {
            printf("Connection %d has an error...\n", mysqli_thread_id($link));
            $processed++;
        }
        foreach ($links as $link) {
            if ($res = mysqli_reap_async_query($link)) {
                mysqli_free_result($res);
                $processed++;
            }
        }
    } while ($processed < 2);

    mysqli_close($mysqli1);
    mysqli_close($mysqli2);

    $mysqli1 = get_connection();
    $mysqli2 = get_connection();

    var_dump(mysqli_query($mysqli1, "SELECT SLEEP(0.10)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));
    var_dump(mysqli_query($mysqli2, "SELECT SLEEP(0.20)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));

    $processed = $loops = 0;
    $all = array($mysqli1, $mysqli2);
    do {
        $loops++;
        if ($loops > 10) {
            printf("[006] The queries should have finished already\n");
            break;
        }
        $links = $errors = $reject = $all;
        ob_start();
        if (0 == ($ready = mysqli_poll($links, $errors, $reject, 0, 50000))) {
            $tmp = ob_get_contents();
            ob_end_clean();
            if ($tmp != '') {
                printf("Expected error:\n%s\n", $tmp);
                break;
            }
            continue;
        }
        foreach ($links as $link) {
            if ($res = mysqli_reap_async_query($link)) {
                mysqli_free_result($res);
            }
            $processed++;
        }
    } while ($processed < 2);

    $ready = mysqli_poll($links, $errors, $reject, 0, 50000);
    mysqli_close($mysqli1);
    mysqli_close($mysqli2);

    print "done!";
?>
--EXPECTF--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Connection %d should be rejected...
Connection %d was rejected...
bool(true)
bool(true)

Warning: mysqli_poll(): All arrays passed are clear in %s on line %d
done!