1--TEST--
2Trying implicit reconnect after wait_timeout and KILL using mysqli_ping()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8if (stristr(mysqli_get_client_info(), 'mysqlnd'))
9    die("skip: test for libmysql");
10?>
11--INI--
12mysqli.reconnect=1
13--FILE--
14<?php
15    require_once("connect.inc");
16    require_once("table.inc");
17
18    if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
19        printf("[001] Cannot create second database connection, [%d] %s\n",
20            mysqli_connect_errno(), mysqli_connect_error());
21
22    $thread_id_timeout = mysqli_thread_id($link);
23    $thread_id_control = mysqli_thread_id($link2);
24
25    if (!$res = mysqli_query($link2, "SHOW FULL PROCESSLIST"))
26        printf("[002] Cannot get full processlist, [%d] %s\n",
27            mysqli_errno($link2), mysqli_error($link));
28
29    $running_threads = array();
30    while ($row = mysqli_fetch_assoc($res))
31        $running_threads[$row['Id']] = $row;
32    mysqli_free_result($res);
33
34    if (!isset($running_threads[$thread_id_timeout]) ||
35            !isset($running_threads[$thread_id_control]))
36        printf("[003] Processlist is borked, [%d] %s\n",
37            mysqli_errno($link2), mysqli_error($link));
38
39    if (!mysqli_query($link, "SET SESSION wait_timeout = 2"))
40        printf("[004] Cannot set wait_timeout, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
41
42    if (!$res = mysqli_query($link, "SHOW VARIABLES LIKE 'wait_timeout'"))
43        printf("[005] Cannot check if wait_timeout has been set, [%d] %s\n",
44            mysqli_errno($link), mysqli_error($link));
45
46    if (!$row = mysqli_fetch_assoc($res))
47        printf("[006] Cannot get wait_timeout, [%d] %s\n",
48            mysqli_errno($link), mysqli_error($link));
49    mysqli_free_result($res);
50
51    if ($row['Value'] != 2)
52        printf("[007] Failed setting the wait_timeout, test will not work, [%d] %s\n",
53            mysqli_errno($link), mysqli_error($link));
54
55    // after 2+ seconds the server should kill the connection
56    sleep(3);
57
58    if (!$res = mysqli_query($link2, "SHOW FULL PROCESSLIST"))
59        printf("[008] Cannot get full processlist, [%d] %s\n",
60            mysqli_errno($link2), mysqli_error($link));
61
62    $running_threads = array();
63    while ($row = mysqli_fetch_assoc($res))
64        $running_threads[$row['Id']] = $row;
65    mysqli_free_result($res);
66
67    if (isset($running_threads[$thread_id_timeout]))
68        printf("[009] Server should have killed the timeout connection, [%d] %s\n",
69            mysqli_errno($link2), mysqli_error($link));
70
71    if (true !== mysqli_ping($link))
72        printf("[010] Reconnect should have happened");
73
74    if (!$res = mysqli_query($link, "SELECT DATABASE() as _dbname"))
75        printf("[011] Cannot get database name, [%d] %s\n",
76            mysqli_errno($link), mysqli_error($link));
77
78    if (!$row = mysqli_fetch_assoc($res))
79        printf("[012] Cannot get database name, [%d] %s\n",
80            mysqli_errno($link), mysqli_error($link));
81
82    mysqli_free_result($res);
83    if ($row['_dbname'] != $db)
84        printf("[013] Connection should has been made to DB/Schema '%s', expecting '%s', [%d] %s\n",
85            $row['_dbname'], $db, mysqli_errno($link), mysqli_error($link));
86
87    // ... and now we try KILL
88    $thread_id_timeout = mysqli_thread_id($link);
89
90    if (!mysqli_query($link2, sprintf('KILL %d', $thread_id_timeout)))
91        printf("[014] Cannot KILL timeout connection, [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
92    // Give the server a second to really kill the other thread...
93    sleep(1);
94
95    if (!$res = mysqli_query($link2, "SHOW FULL PROCESSLIST"))
96        printf("[015] Cannot get full processlist, [%d] %s\n",
97            mysqli_errno($link2), mysqli_error($link));
98
99    $running_threads = array();
100    while ($row = mysqli_fetch_assoc($res))
101        $running_threads[$row['Id']] = $row;
102    mysqli_free_result($res);
103
104    if (isset($running_threads[$thread_id_timeout]) ||
105            !isset($running_threads[$thread_id_control]))
106        printf("[016] Processlist is borked, [%d] %s\n",
107            mysqli_errno($link2), mysqli_error($link));
108
109    if (true !== ($tmp = mysqli_ping($link)))
110        printf("[017] Expecting boolean/true got %s/%s\n", gettype($tmp), $tmp);
111
112    if (!$res = mysqli_query($link, "SELECT DATABASE() as _dbname"))
113        printf("[018] Cannot get database name, [%d] %s\n",
114            mysqli_errno($link), mysqli_error($link));
115
116    if (!$row = mysqli_fetch_assoc($res))
117        printf("[019] Cannot get database name, [%d] %s\n",
118            mysqli_errno($link), mysqli_error($link));
119
120    mysqli_free_result($res);
121    if ($row['_dbname'] != $db)
122        printf("[020] Connection should has been made to DB/Schema '%s', expecting '%s', [%d] %s\n",
123            $row['_dbname'], $db, mysqli_errno($link), mysqli_error($link));
124
125    mysqli_close($link);
126    mysqli_close($link2);
127    print "done!";
128?>
129--EXPECT--
130done!
131