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