1--TEST--
2mysqli_change_user() - GET_LOCK()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7die("skip - is the server still buggy?");
8?>
9--INI--
10max_execution_time=240
11--FILE--
12<?php
13    require_once('connect.inc');
14    require_once('table.inc');
15
16    // We need this little hack to be able to re-run the test
17    $lock = 'phptest_' . mt_rand(0, 100000);
18    $thread_id = mysqli_thread_id($link);
19
20    printf("Testing GET_LOCK()...\n");
21
22    if (!$res = mysqli_query($link, sprintf('SELECT GET_LOCK("%s", 2) AS _ok', $lock)))
23        printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
24
25    if (!$row = mysqli_fetch_assoc($res))
26        printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
27
28    if ($row['_ok'] != 1)
29        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
30    else
31        printf("... lock '%s' acquired by thread %d\n", $lock, $thread_id);
32
33    mysqli_free_result($res);
34
35
36    // GET_LOCK("phptest") should be released
37    /* From the mysql_change_user documentation:
38This command resets the state as if one had done a new connect. (See Section 25.2.13, “Controlling Automatic Reconnect Behavior”.) It always performs a ROLLBACK of any active transactions, closes and drops all temporary tables, and unlocks all locked tables. Session system variables are reset to the values of the corresponding global system variables. Prepared statements are released and HANDLER variables are closed. Locks acquired with GET_LOCK() are released. These effects occur even if the user didn't change.
39    */
40    mysqli_change_user($link, $user, $passwd, $db);
41sleep(5);
42    $new_thread_id = mysqli_thread_id($link);
43
44    printf("... calling IS_USED_LOCK() on '%s' using thread '%d'\n", $lock, $new_thread_id);
45    if (!$res = mysqli_query($link, 'SELECT IS_USED_LOCK("phptest") AS _ok'))
46        printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
47
48    if (!$row = mysqli_fetch_assoc($res))
49        printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
50
51    if ($row['_ok'] != NULL)
52        printf("[006] Lock '%s' should have been released, [%d] %s\n",
53            $lock,
54            mysqli_errno($link), mysqli_error($link));
55
56    printf("... calling IS_FREE_LOCK() on '%s' using thread '%d'\n", $lock, $new_thread_id);
57    if (!$res = mysqli_query($link, 'SELECT IS_FREE_LOCK("phptest") AS _ok'))
58        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
59
60    if (!$row = mysqli_fetch_assoc($res))
61        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
62
63    if ($row['_ok'] != 1)
64        printf("[009] Lock '%s' should have been released, [%d] %s\n",
65            $lock,
66            mysqli_errno($link), mysqli_error($link));
67
68    mysqli_free_result($res);
69
70    /* Ok, let's try a NEW connection and a NEW lock! */
71    mysqli_close($link);
72    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
73        printf("[010] Cannot open new connection, [%d] %s\n",
74            mysqli_connect_errno(), mysqli_connect_error());
75
76    do {
77        $newlock = 'phptest_' . mt_rand(0, 100000);
78    } while ($lock == $newlock);
79
80    $new_thread_id = mysqli_thread_id($link);
81    printf("... calling IS_USED_LOCK() on '%s' using new connection with thread '%d'\n", $newlock, $new_thread_id);
82    if (!$res = mysqli_query($link, 'SELECT IS_USED_LOCK("phptest") AS _ok'))
83        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
84
85    if (!$row = mysqli_fetch_assoc($res))
86        printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
87
88    if ($row['_ok'] != NULL)
89        printf("[013] Lock '%s' should have been released, [%d] %s\n",
90            $lock,
91            mysqli_errno($link), mysqli_error($link));
92
93    print "done!";
94?>
95--CLEAN--
96<?php
97	require_once("clean_table.inc");
98?>
99--EXPECTF--
100Testing GET_LOCK()...
101... lock 'phptest_%d' acquired by thread %d
102... calling IS_USED_LOCK() on 'phptest_%d' using thread '%d'
103... calling IS_FREE_LOCK() on 'phptest_%d' using thread '%d'
104... calling IS_USED_LOCK() on 'phptest_%d' using new connection with thread '%d'
105done!
106