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