Debian Codegraph @

mysql-test/suite/perfschema/t/native_func_thread_id.test

raw
1 --echo #
2 --echo # Tests for the Performance Schema native functions ps_thread_id()
3 --echo # and ps_current_thread_id()
4 --echo
5 --echo # 'connection ID' is the MySQL server processlist ID
6 --echo # 'thread ID' is the Performance Schema thread ID assigned to the connection ID
7 --echo
8
9 --echo
10 USE test;
11
12 --echo # Get the connection ID of this connection
13 SELECT connection_id() INTO @cid;
14
15 --echo
16 --echo # Get the thread ID for this connection
17 SELECT thread_id FROM performance_schema.threads
18 WHERE PROCESSLIST_ID = @cid INTO @tid;
19
20 --echo
21 --echo # Open another connection, get the connection ID
22 connect(con1, localhost, root,,);
23 let $con1_id = `SELECT connection_id()`;
24 # debug eval SELECT $con1_id;
25
26 --connection default
27 --echo
28 --disable_query_log
29 eval SELECT $con1_id INTO @cid_1;
30 --enable_query_log
31
32 --echo
33 --echo # Get the thread ID for the remote thread
34 SELECT thread_id FROM performance_schema.threads
35 WHERE PROCESSLIST_ID = @cid_1 INTO @tid_1;
36
37 --echo
38 --echo # Verify that the remote thread IDs are different
39 SELECT @cid <> @cid_1 AS "Expect 1";
40 SELECT @tid <> @tid_1 AS "Expect 1";
41
42 ## ps_current_thread_id()
43
44 --echo
45 --echo # Returns the current thread ID
46 SELECT ps_current_thread_id() - @tid AS "Expect 0";
47
48 --echo
49 --echo # Bad parameter count
50 --error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
51 SELECT ps_current_thread_id(1);
52
53 ## ps_thread_id()
54
55 --echo
56 --echo # A NULL connection ID returns NULL
57 SELECT ps_thread_id(NULL) AS "Expect NULL";
58
59 --echo
60 --echo # The current connection ID returns the current thread ID
61 SELECT ps_thread_id(@cid) - @tid AS "Expect 0";
62
63 --echo
64 --echo # Another connection ID returns the corresponding thread ID
65 SELECT ps_thread_id(@cid_1) - @tid_1 AS "Expect 0";
66
67 --echo
68 --echo # Hybrid parameter
69 SELECT ps_thread_id(@cid + 0) - @tid AS "Expect 0";
70
71 --echo
72 --echo # Return NULL if connection ID not found
73 SELECT ps_thread_id(9999999) AS "Expect NULL";
74
75 --echo
76 --echo # Return NULL if bad parameter
77 SELECT ps_thread_id("foo") AS "Expect NULL";
78
79 --echo
80 --echo # Return NULL if negative number
81 SELECT ps_thread_id(-1) AS "Expect NULL";
82
83 --echo
84 --echo # Error if bad parameter count
85 --error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
86 SELECT ps_thread_id(1, 2);
87
88 --echo
89 --echo # Error if bad parameter count
90 --error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
91 SELECT ps_thread_id();
92
93
94 ## Name collisions
95
96 --echo
97 --echo # Name collision warning
98 #--warning ER_NATIVE_FCT_NAME_COLLISION
99 USE test;
100 CREATE FUNCTION ps_current_thread_id() RETURNS varchar(100) return "This is function ps_current_thread_id()";
101 --echo
102 SHOW WARNINGS;
103 --echo
104 CREATE FUNCTION ps_thread_id() RETURNS varchar(100) return "This is a function ps_thread_id()";
105 --echo
106 SHOW WARNINGS;
107
108 --echo
109 --echo # Local function should work ok
110 SELECT test.ps_current_thread_id();
111 SELECT test.ps_thread_id();
112
113 --echo
114 --echo # Global function should still work
115 SELECT ps_current_thread_id() - @tid AS "Expect 0";
116 SELECT ps_current_thread_id() - @tid AS "Expect 0";
117
118 --echo
119 --echo # Cleanup
120 DROP FUNCTION test.ps_current_thread_id;
121 DROP FUNCTION test.ps_thread_id;
122 --disconnect con1
123
124 --echo
125 --echo ## Handling of NULL (bug#30525561)
126
127 SELECT PS_CURRENT_THREAD_ID() into @this_tid;
128 SELECT CONNECTION_ID() into @this_cid;
129
130 SELECT PS_THREAD_ID(my_cid) = @this_tid AS PS_TID_CHECK,
131 sys.ps_thread_id(my_cid) = @this_tid AS SYS_TID_CHECK
132 FROM (SELECT @this_cid AS my_cid
133 UNION ALL
134 SELECT CAST(1234567890 AS UNSIGNED)
135 UNION ALL
136 SELECT NULL
137 UNION ALL
138 SELECT @this_cid
139 UNION ALL
140 SELECT CAST(1234567890 AS UNSIGNED)
141 ) t;
142