| 1 | /* Copyright (c) 2013, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | /** |
| 25 | This is unit test for the Global_THD_manager class. |
| 26 | */ |
| 27 | |
| 28 | #include "my_config.h" |
| 29 | |
| 30 | #include <gtest/gtest.h> |
| 31 | #include <stddef.h> |
| 32 | #include <sys/types.h> |
| 33 | |
| 34 | #include "my_inttypes.h" |
| 35 | #include "sql/mysqld.h" |
| 36 | #include "sql/mysqld_thd_manager.h" // Global_THD_manager |
| 37 | #include "sql/sql_class.h" |
| 38 | #include "unittest/gunit/thread_utils.h" |
| 39 | |
| 40 | using thread::Notification; |
| 41 | using thread::Thread; |
| 42 | |
| 43 | namespace thd_manager_unittest { |
| 44 | |
| 45 | class ThreadManagerTest : public ::testing::Test { |
| 46 | protected: |
| 47 | ThreadManagerTest() = default; |
| 48 | |
| 49 | void SetUp() override { |
| 50 | Global_THD_manager::create_instance(); |
| 51 | thd_manager = Global_THD_manager::get_instance(); |
| 52 | thd_manager->set_unit_test(); |
| 53 | } |
| 54 | |
| 55 | void TearDown() override {} |
| 56 | |
| 57 | Global_THD_manager *thd_manager; |
| 58 | |
| 59 | private: |
| 60 | ThreadManagerTest(ThreadManagerTest const &) = delete; |
| 61 | ThreadManagerTest &operator=(ThreadManagerTest const &) = delete; |
| 62 | }; |
| 63 | |
| 64 | enum TEST_TYPE { TEST_WAIT = 0, TEST_TIMED_WAIT = 1 }; |
| 65 | |
| 66 | /* |
| 67 | Verify add_thd(), remove_thd() methods |
| 68 | */ |
| 69 | TEST_F(ThreadManagerTest, AddRemoveTHDWithGuard) { |
| 70 | THD thd1(false), thd2(false); |
| 71 | thd1.server_id = 1; |
| 72 | thd1.set_new_thread_id(); |
| 73 | thd2.server_id = 2; |
| 74 | thd2.set_new_thread_id(); |
| 75 | |
| 76 | EXPECT_EQ(0U, thd_manager->get_thd_count()); |
| 77 | thd_manager->add_thd(&thd1); |
| 78 | EXPECT_EQ(1U, thd_manager->get_thd_count()); |
| 79 | thd_manager->add_thd(&thd2); |
| 80 | |
| 81 | thd_manager->remove_thd(&thd1); |
| 82 | EXPECT_EQ(1U, thd_manager->get_thd_count()); |
| 83 | thd_manager->remove_thd(&thd2); |
| 84 | EXPECT_EQ(0U, thd_manager->get_thd_count()); |
| 85 | } |
| 86 | |
| 87 | TEST_F(ThreadManagerTest, IncDecThreadRunning) { |
| 88 | EXPECT_EQ(0, thd_manager->get_num_thread_running()); |
| 89 | thd_manager->inc_thread_running(); |
| 90 | EXPECT_EQ(1, thd_manager->get_num_thread_running()); |
| 91 | thd_manager->dec_thread_running(); |
| 92 | EXPECT_EQ(0, thd_manager->get_num_thread_running()); |
| 93 | } |
| 94 | |
| 95 | TEST_F(ThreadManagerTest, IncThreadCreated) { |
| 96 | EXPECT_EQ(0U, thd_manager->get_num_thread_created()); |
| 97 | thd_manager->inc_thread_created(); |
| 98 | EXPECT_EQ(1U, thd_manager->get_num_thread_created()); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | Test function to validate do_for_all_thd, do_for_all_thd_copy. |
| 103 | It emulates counter function to count number of thds in thd list. |
| 104 | */ |
| 105 | class TestFunc1 : public Do_THD_Impl { |
| 106 | private: |
| 107 | int cnt; |
| 108 | |
| 109 | public: |
| 110 | TestFunc1() : cnt(0) {} |
| 111 | int get_count() { return cnt; } |
| 112 | void reset_count() { cnt = 0; } |
| 113 | void operator()(THD *) override { cnt = cnt + 1; } |
| 114 | }; |
| 115 | |
| 116 | TEST_F(ThreadManagerTest, TestTHDCopyDoFunc) { |
| 117 | THD thd1(false), thd2(false); |
| 118 | thd1.server_id = 1; |
| 119 | thd1.set_new_thread_id(); |
| 120 | thd2.server_id = 2; |
| 121 | thd2.set_new_thread_id(); |
| 122 | // Add two THD into thd list. |
| 123 | thd_manager->add_thd(&thd1); |
| 124 | thd_manager->add_thd(&thd2); |
| 125 | |
| 126 | int cnt = 0; |
| 127 | TestFunc1 testFunc1; |
| 128 | thd_manager->do_for_all_thd_copy(&testFunc1); |
| 129 | cnt = testFunc1.get_count(); |
| 130 | EXPECT_EQ(2, cnt); |
| 131 | |
| 132 | testFunc1.reset_count(); |
| 133 | thd_manager->do_for_all_thd(&testFunc1); |
| 134 | cnt = testFunc1.get_count(); |
| 135 | EXPECT_EQ(2, cnt); |
| 136 | |
| 137 | // Cleanup - Remove added THD. |
| 138 | thd_manager->remove_thd(&thd1); |
| 139 | thd_manager->remove_thd(&thd2); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | Test class to verify find_thd() |
| 144 | */ |
| 145 | class TestFunc2 : public Find_THD_Impl { |
| 146 | public: |
| 147 | TestFunc2() : search_value(0) {} |
| 148 | bool operator()(THD *thd) override { |
| 149 | if (thd->server_id == search_value) { |
| 150 | return true; |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | void set_search_value(uint val) { search_value = val; } |
| 155 | |
| 156 | private: |
| 157 | uint search_value; |
| 158 | }; |
| 159 | |
| 160 | /* |
| 161 | Test class to verify do_all_for_thd() function. |
| 162 | Counts all thd whose server_id value is less than or equal to 2. |
| 163 | */ |
| 164 | class TestFunc3 : public Do_THD_Impl { |
| 165 | public: |
| 166 | TestFunc3() : count(0) {} |
| 167 | void operator()(THD *thd) override { |
| 168 | if (thd->server_id <= 2) { |
| 169 | count++; |
| 170 | } |
| 171 | } |
| 172 | int get_count() { return count; } |
| 173 | |
| 174 | private: |
| 175 | int count; |
| 176 | }; |
| 177 | |
| 178 | TEST_F(ThreadManagerTest, TestTHDFindFunc) { |
| 179 | THD thd1(false), thd2(false); |
| 180 | thd1.server_id = 1; |
| 181 | thd1.set_new_thread_id(); |
| 182 | thd2.server_id = 2; |
| 183 | thd2.set_new_thread_id(); |
| 184 | thd_manager->add_thd(&thd1); |
| 185 | thd_manager->add_thd(&thd2); |
| 186 | TestFunc2 testFunc2; |
| 187 | testFunc2.set_search_value(2); |
| 188 | THD_ptr thd_ptr = thd_manager->find_thd(&testFunc2); |
| 189 | /* Returns the last thd which matches. */ |
| 190 | EXPECT_EQ(2U, thd_ptr->server_id); |
| 191 | |
| 192 | testFunc2.set_search_value(6); |
| 193 | thd_ptr = thd_manager->find_thd(&testFunc2); |
| 194 | /* Find non existing thd with server_id value 6. Expected to return NULL. */ |
| 195 | const THD *null_thd = nullptr; |
| 196 | EXPECT_EQ(null_thd, thd_ptr.get()); |
| 197 | |
| 198 | /* Verify if THD being disposed is returned from find_thd() */ |
| 199 | testFunc2.set_search_value(1); |
| 200 | thd_ptr = thd_manager->find_thd(&testFunc2); |
| 201 | EXPECT_EQ(1U, thd_ptr->server_id); |
| 202 | EXPECT_EQ(false, thd_ptr->is_being_disposed()); |
| 203 | thd_ptr.release(); |
| 204 | // Starting thd1 disposal. |
| 205 | thd1.release_resources(); |
| 206 | // Find THD being disposed. Expected to return nullptr. |
| 207 | thd_ptr = thd_manager->find_thd(&testFunc2); |
| 208 | EXPECT_EQ(null_thd, thd_ptr.get()); |
| 209 | EXPECT_EQ(true, thd1.release_resources_done()); |
| 210 | |
| 211 | // Cleanup - Remove added THD. |
| 212 | thd_manager->remove_thd(&thd1); |
| 213 | thd_manager->remove_thd(&thd2); |
| 214 | } |
| 215 | |
| 216 | TEST_F(ThreadManagerTest, TestTHDCountFunc) { |
| 217 | THD thd1(false), thd2(false), thd3(false); |
| 218 | thd1.server_id = 1; |
| 219 | thd1.set_new_thread_id(); |
| 220 | thd2.server_id = 2; |
| 221 | thd2.set_new_thread_id(); |
| 222 | thd3.server_id = 3; |
| 223 | thd3.set_new_thread_id(); |
| 224 | thd_manager->add_thd(&thd1); |
| 225 | thd_manager->add_thd(&thd2); |
| 226 | thd_manager->add_thd(&thd3); |
| 227 | |
| 228 | TestFunc3 testFunc3; |
| 229 | thd_manager->do_for_all_thd(&testFunc3); |
| 230 | int ret = testFunc3.get_count(); |
| 231 | // testFunc3 counts for thd->server_id values, 1 and 2. |
| 232 | EXPECT_EQ(2, ret); |
| 233 | |
| 234 | // Cleanup - Remove added THD. |
| 235 | thd_manager->remove_thd(&thd1); |
| 236 | thd_manager->remove_thd(&thd2); |
| 237 | thd_manager->remove_thd(&thd3); |
| 238 | } |
| 239 | |
| 240 | TEST_F(ThreadManagerTest, ThreadID) { |
| 241 | // Code assumes that the size of my_thread_id is 32 bit. |
| 242 | ASSERT_EQ(4U, sizeof(my_thread_id)); |
| 243 | |
| 244 | // Reset the thread ID counter |
| 245 | thd_manager->set_thread_id_counter(1); |
| 246 | EXPECT_EQ(1U, thd_manager->get_thread_id()); |
| 247 | |
| 248 | // The counter is incremented after ID is assigned. |
| 249 | EXPECT_EQ(1U, thd_manager->get_new_thread_id()); |
| 250 | EXPECT_EQ(2U, thd_manager->get_thread_id()); |
| 251 | |
| 252 | // Two increments in a row |
| 253 | EXPECT_EQ(2U, thd_manager->get_new_thread_id()); |
| 254 | EXPECT_EQ(3U, thd_manager->get_new_thread_id()); |
| 255 | EXPECT_EQ(4U, thd_manager->get_thread_id()); |
| 256 | |
| 257 | // Force wrap of the counter |
| 258 | thd_manager->set_thread_id_counter(UINT_MAX32); |
| 259 | EXPECT_EQ(UINT_MAX32, thd_manager->get_new_thread_id()); |
| 260 | |
| 261 | // We should not use the value reserved for temporary THDs (0). |
| 262 | // The next available value should be 4. |
| 263 | EXPECT_EQ(4U, thd_manager->get_new_thread_id()); |
| 264 | EXPECT_EQ(5U, thd_manager->get_thread_id()); |
| 265 | |
| 266 | // Release thread ID 3 and reset counter. |
| 267 | thd_manager->release_thread_id(3); |
| 268 | thd_manager->set_thread_id_counter(1U); |
| 269 | EXPECT_EQ(3U, thd_manager->get_new_thread_id()); |
| 270 | |
| 271 | // Releasing the reserved thread ID is allowed - multiple times. |
| 272 | thd_manager->release_thread_id(Global_THD_manager::reserved_thread_id); |
| 273 | thd_manager->release_thread_id(Global_THD_manager::reserved_thread_id); |
| 274 | |
| 275 | // Cleanup |
| 276 | thd_manager->release_thread_id(1); |
| 277 | thd_manager->release_thread_id(2); |
| 278 | thd_manager->release_thread_id(3); |
| 279 | thd_manager->release_thread_id(4); |
| 280 | thd_manager->release_thread_id(UINT_MAX32); |
| 281 | } |
| 282 | |
| 283 | #if !defined(NDEBUG) |
| 284 | TEST_F(ThreadManagerTest, ThreadIDDeathTest) { |
| 285 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 286 | my_thread_id thread_id = thd_manager->get_new_thread_id(); |
| 287 | thd_manager->release_thread_id(thread_id); |
| 288 | // Releasing the same ID twice should assert. |
| 289 | EXPECT_DEATH_IF_SUPPORTED(thd_manager->release_thread_id(thread_id), |
| 290 | ".*Assertion .*1 == num_erased.*"); |
| 291 | } |
| 292 | #endif |
| 293 | |
| 294 | } // namespace thd_manager_unittest |