1--TEST--
2Trying implicit reconnect after wait_timeout and KILL using mysqli_ping()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8?>
9--INI--
10mysqli.reconnect=0
11--FILE--
12<?php
13    require_once("connect.inc");
14    require_once("table.inc");
15
16    if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
17        printf("[001] Cannot create second database connection, [%d] %s\n",
18            mysqli_connect_errno(), mysqli_connect_error());
19
20    $thread_id_timeout = mysqli_thread_id($link);
21    $thread_id_control = mysqli_thread_id($link2);
22
23    if (!$res = mysqli_query($link2, "SHOW FULL PROCESSLIST"))
24        printf("[002] Cannot get full processlist, [%d] %s\n",
25            mysqli_errno($link2), mysqli_error($link));
26
27    $running_threads = array();
28    while ($row = mysqli_fetch_assoc($res))
29        $running_threads[$row['Id']] = $row;
30    mysqli_free_result($res);
31
32    if (!isset($running_threads[$thread_id_timeout]) ||
33            !isset($running_threads[$thread_id_control]))
34        printf("[003] Processlist is borked, [%d] %s\n",
35            mysqli_errno($link2), mysqli_error($link));
36
37    if (!mysqli_query($link, "SET SESSION wait_timeout = 2"))
38        printf("[004] Cannot set wait_timeout, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
39
40    if (!$res = mysqli_query($link, "SHOW VARIABLES LIKE 'wait_timeout'"))
41        printf("[005] Cannot check if wait_timeout has been set, [%d] %s\n",
42            mysqli_errno($link), mysqli_error($link));
43
44    if (!$row = mysqli_fetch_assoc($res))
45        printf("[006] Cannot get wait_timeout, [%d] %s\n",
46            mysqli_errno($link), mysqli_error($link));
47    mysqli_free_result($res);
48
49    if ($row['Value'] != 2)
50        printf("[007] Failed setting the wait_timeout, test will not work, [%d] %s\n",
51            mysqli_errno($link), mysqli_error($link));
52
53    // after 2+ seconds the server should kill the connection
54    sleep(3);
55
56    if (!$res = mysqli_query($link2, "SHOW FULL PROCESSLIST"))
57        printf("[008] Cannot get full processlist, [%d] %s\n",
58            mysqli_errno($link2), mysqli_error($link));
59
60    $running_threads = array();
61    while ($row = mysqli_fetch_assoc($res))
62        $running_threads[$row['Id']] = $row;
63    mysqli_free_result($res);
64
65    if (isset($running_threads[$thread_id_timeout]))
66        printf("[009] Server should have killed the timeout connection, [%d] %s\n",
67            mysqli_errno($link2), mysqli_error($link));
68
69    if (false !== @mysqli_ping($link))
70        printf("[010] Reconnect should not have happened");
71
72    if ($res = @mysqli_query($link, "SELECT DATABASE() as _dbname"))
73        printf("[011] Executing a query should not be possible, connection should be closed, [%d] %s\n",
74            mysqli_errno($link), mysqli_error($link));
75
76    if (!$link = @my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
77        printf("[012] Cannot create database connection, [%d] %s\n",
78            mysqli_connect_errno(), mysqli_connect_error());
79
80    $thread_id_timeout = mysqli_thread_id($link);
81    /*
82      Don't test for the mysqli_query() return value here.
83      It is undefined if the server replies to the query and how.
84      For example, it seems that on Linux when connecting to MySQL 5.1,
85      the server always manages to send a full a reply. Whereas MySQl 5.5
86      may not. The behaviour is undefined. Any return value is fine.
87    */
88    if ($IS_MYSQLND) {
89        /*
90        mysqlnd is a bit more verbose than libmysql. mysqlnd should print:
91        Warning: mysqli_query(): MySQL server has gone away in %s on line %d
92
93        Warning: mysqli_query(): Error reading result set's header in %d on line %d
94        */
95        @mysqli_query($link, sprintf('KILL %d', $thread_id_timeout));
96    } else {
97        mysqli_query($link, sprintf('KILL %d', $thread_id_timeout));
98    }
99    // Give the server a second to really kill the other thread...
100    sleep(1);
101
102    if (!$res = mysqli_query($link2, "SHOW FULL PROCESSLIST"))
103        printf("[014] Cannot get full processlist, [%d] %s\n",
104            mysqli_errno($link2), mysqli_error($link));
105
106    $running_threads = array();
107    while ($row = mysqli_fetch_assoc($res))
108        $running_threads[$row['Id']] = $row;
109    mysqli_free_result($res);
110
111    if (isset($running_threads[$thread_id_timeout]) ||
112            !isset($running_threads[$thread_id_control]))
113        printf("[015] Processlist is borked, [%d] %s\n",
114            mysqli_errno($link2), mysqli_error($link));
115
116    if (false !== ($tmp = @mysqli_ping($link)))
117        printf("[016] Expecting boolean/false got %s/%s\n", gettype($tmp), $tmp);
118
119    if ($res = @mysqli_query($link, "SELECT DATABASE() as _dbname"))
120        printf("[017] Running a query should not be possible, connection should be gone, [%d] %s\n",
121            mysqli_errno($link), mysqli_error($link));
122
123    mysqli_close($link2);
124    print "done!";
125?>
126--EXPECT--
127done!
128