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