~johannes-s/php-mysqlnd/mysqli-to-stream

« back to all changes in this revision

Viewing changes to tests/ext/mysqli/mysqli_poll.phpt

  • Committer: Johannes Schlueter
  • Date: 2008-10-27 21:16:39 UTC
  • mfrom: (1607.1.11 trunk)
  • Revision ID: johannes@mysql.com-20081027211639-2qop5lb8gj6nlxvs
Merge master branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--TEST--
 
2
int mysqli_poll(array read_links, array write_links, array errors, int timeout_seconds [, int timeout_microseconds])
 
3
--SKIPIF--
 
4
<?php
 
5
require_once('skipif.inc');
 
6
require_once('skipifemb.inc');
 
7
require_once('connect.inc');
 
8
 
 
9
if (!$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 = 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
        if (NULL !== ($tmp = @mysqli_poll()))
 
28
                printf("[002] Expecting NULL got %s\n", var_export($tmp, true));
 
29
 
 
30
        if (NULL !== ($tmp = @mysqli_poll(array($link))))
 
31
                printf("[003] Expecting NULL got %s\n", var_export($tmp, true));
 
32
 
 
33
        if (NULL !== ($tmp = @mysqli_poll(array($link), NULL)))
 
34
                printf("[004] Expecting NULL got %s\n", var_export($tmp, true));
 
35
 
 
36
        if (NULL !== ($tmp = @mysqli_poll(array($link), NULL, array($link))))
 
37
                printf("[004a] Expecting NULL got %s\n", var_export($tmp, true));
 
38
 
 
39
        $tmp = 1;
 
40
        $errors = &$tmp;
 
41
        if (NULL !== ($tmp = @mysqli_poll(array($link), $errors, 2)))
 
42
                printf("[005] Expecting NULL got %s\n", var_export($tmp, true));
 
43
 
 
44
        if (false !== ($tmp = @mysqli_poll(array($link), NULL, array($link), -1)))
 
45
                printf("[006] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
46
 
 
47
        if (false !== ($tmp = @mysqli_poll(array($link), NULL, array($link), 0, -1)))
 
48
                printf("[007] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
49
 
 
50
        if (false !== ($tmp = @mysqli_poll(array($link), NULL, array($link), 0, -1)))
 
51
                printf("[008] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
52
 
 
53
        if (0 !== ($tmp = (mysqli_poll(array($link), array($link), array($link), 0, 1))))
 
54
                printf("[009] Expecting int/0 got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
55
 
 
56
        // Killing connection
 
57
        $errors = array($link);
 
58
        $reject = array($link);
 
59
        mysqli_kill($link, mysqli_thread_id($link));
 
60
        // Yes, 1 is expected! We have to fetch the reply from the kill!
 
61
        if (1 !== ($tmp = (mysqli_poll(array($link), $errors, $reject, 0, 10000))))
 
62
                printf("[010] Expecting int/1 got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
63
 
 
64
        $link = get_connection();
 
65
        if (true !== ($tmp = mysqli_query($link, "SELECT 1", MYSQLI_ASYNC |  MYSQLI_USE_RESULT)))
 
66
                printf("[011] Expecting boolean/true got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
67
 
 
68
        $errors = array($link);
 
69
        $reject = array($link);
 
70
        mysqli_kill($link, mysqli_thread_id($link));
 
71
        if (1 !== ($tmp = (mysqli_poll(array($link), $errors, $reject, 0, 10000))))
 
72
                printf("[012] Expecting int/1 got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
73
 
 
74
 
 
75
        $link = get_connection();
 
76
        $new_links = array($link);
 
77
        $reject = array($link);
 
78
        var_dump(mysqli_poll($new_links, NULL, $reject, 0, 1000));
 
79
        var_dump($new_links);
 
80
        var_dump(count(($reject)));
 
81
 
 
82
        // No read links, only write links
 
83
        require_once('table.inc');
 
84
 
 
85
        $queries = array(
 
86
                        'CREATE TABLE IF NOT EXISTS bogus(id INT)',
 
87
                        'SET @a = 1',
 
88
                        'SELECT * FROM test ORDER BY id ASC LIMIT 2',
 
89
                        'INSERT INTO test(id, label) VALUES (100, "z")',
 
90
                        'SELECT * FROM test ORDER BY id ASC LIMIT 2',
 
91
                        'SELECT',
 
92
                        'UPDATE test SET id = 101 WHERE id > 3',
 
93
                        'UPDATE_FIX test SET id = 101 WHERE id > 3',
 
94
                        'DROP TABLE IF EXISTS bogus',
 
95
                        'DELETE FROM test WHERE id = @a',
 
96
                        'DELETE FROM test WHERE id = 1',
 
97
        );
 
98
 
 
99
        $link = get_connection();
 
100
        $have_proc = false;
 
101
        mysqli_real_query($link, "DROP PROCEDURE IF EXISTS p");
 
102
        if (mysqli_real_query($link, 'CREATE PROCEDURE p(IN ver_in VARCHAR(25), OUT ver_out VARCHAR(25)) BEGIN SELECT ver_in INTO ver_out; END;')) {
 
103
                        $have_proc = true;
 
104
                        $queries[] = 'CALL p("myversion", @version)';
 
105
        }
 
106
        mysqli_close($link);
 
107
 
 
108
        $links = array();
 
109
        for ($i = 0; $i < count($queries); $i++) {
 
110
 
 
111
                $link = get_connection();
 
112
 
 
113
                if (true !== ($tmp = mysqli_query($link, $queries[$i], MYSQLI_ASYNC |  MYSQLI_USE_RESULT)))
 
114
                        printf("[013] Expecting true got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
115
 
 
116
                $links[mysqli_thread_id($link)] = array(
 
117
                        'query' => $queries[$i],
 
118
                        'link' => $link,
 
119
                        'processed' => false,
 
120
                );
 
121
        }
 
122
 
 
123
        $saved_errors = array();
 
124
        do {
 
125
                $poll_links = $poll_close = $poll_reject = array();
 
126
                foreach ($links as $thread_id => $link) {
 
127
                        if (!$link['processed']) {
 
128
                                $poll_links[] = $link['link'];
 
129
                                $poll_close[] = $link['link'];
 
130
                                $poll_reject[] = $link['link'];
 
131
                        }
 
132
                }
 
133
                if (0 == count($poll_links))
 
134
                        break;
 
135
 
 
136
                if (0 == ($num_ready = mysqli_poll($poll_links, $poll_close, $poll_reject, 0, 200000)))
 
137
                        continue;
 
138
 
 
139
                if (!empty($poll_close)) {
 
140
                        die(var_dump($poll_close));
 
141
                }
 
142
 
 
143
                foreach ($poll_links as $link) {
 
144
                        $thread_id = mysqli_thread_id($link);
 
145
                        $links[$thread_id]['processed'] = true;
 
146
 
 
147
                        if (is_object($res = mysqli_reap_async_query($link))) {
 
148
                                // result set object
 
149
                                while ($row = mysqli_fetch_assoc($res)) {
 
150
                                        // eat up all results
 
151
                                        ;
 
152
                                }
 
153
                                mysqli_free_result($res);
 
154
                        } else {
 
155
                                // either there is no result (no SELECT) or there is an error
 
156
                                if (mysqli_errno($link) > 0) {
 
157
                                        $saved_errors[$thread_id] = mysqli_errno($link);
 
158
                                        printf("[014] '%s' caused %d\n", $links[$thread_id]['query'],   mysqli_errno($link));
 
159
                                }
 
160
 
 
161
                        }
 
162
                }
 
163
 
 
164
        } while (true);
 
165
 
 
166
        // Checking if all lines are still usable
 
167
        foreach ($links as $thread_id => $link) {
 
168
                if (isset($saved_errors[$thread_id]) &&
 
169
                        $saved_errors[$thread_id] != mysqli_errno($link['link'])) {
 
170
                        printf("[015] Error state not saved for query '%s', %d != %d\n", $link['query'],
 
171
                                        $saved_errors[$thread_id], mysqli_errno($link['link']));
 
172
                }
 
173
 
 
174
                if (!$res = mysqli_query($link['link'], 'SELECT * FROM test WHERE id = 100'))
 
175
                        printf("[016] Expecting true got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
176
                if (!$row = mysqli_fetch_row($res))
 
177
                        printf("[017] Expecting true got %s/%s\n", gettype($tmp), var_export($tmp, true));
 
178
 
 
179
                mysqli_free_result($res);
 
180
 
 
181
        }
 
182
 
 
183
        if ($res = mysqli_query($link['link'], "SELECT * FROM test WHERE id = 1")) {
 
184
                $row = mysqli_fetch_assoc($res);
 
185
                var_dump($row);
 
186
                mysqli_free_result($res);
 
187
        }
 
188
 
 
189
        if ($have_proc && ($res = mysqli_query($link['link'], "SELECT @version as _version"))) {
 
190
                $row = mysqli_fetch_assoc($res);
 
191
                if ($row['_version'] != 'myversion') {
 
192
                        printf("[018] Check procedures\n");
 
193
                }
 
194
                mysqli_free_result($res);
 
195
        }
 
196
 
 
197
        foreach ($links as $link)
 
198
                mysqli_close($link['link']);
 
199
 
 
200
        print "done!";
 
201
?>
 
202
--EXPECTF--
 
203
 
 
204
Warning: mysqli_poll(): All arrays passed are clear in /home/nixnutz/php5/ext/mysqli/tests/mysqli_poll.php on line 66
 
205
int(0)
 
206
array(0) {
 
207
}
 
208
int(1)
 
209
[014] 'SELECT' caused %d
 
210
[014] 'UPDATE_FIX test SET id = 101 WHERE id > 3' caused %d
 
211
[014] 'UPDATE test SET id = 101 WHERE id > 3' caused %d
 
212
NULL
 
213
done!
 
 
b'\\ No newline at end of file'