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