| 1 | /* Copyright (c) 2014, 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 | #include <gtest/gtest.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include "my_sys.h" |
| 28 | #include "my_systime.h" |
| 29 | #include "my_thread.h" |
| 30 | #include "my_timer.h" |
| 31 | #include "unittest/gunit/thr_template.cc" |
| 32 | #include "unittest/gunit/thread_utils.h" |
| 33 | |
| 34 | #ifdef HAVE_PSI_INTERFACE |
| 35 | PSI_mutex_key key_thd_timer_mutex = PSI_NOT_INSTRUMENTED; |
| 36 | #endif |
| 37 | |
| 38 | /** |
| 39 | Cast a member of a structure to the structure that contains it. |
| 40 | |
| 41 | @param ptr Pointer to the member. |
| 42 | @param type Type of the structure that contains the member. |
| 43 | @param member Name of the member within the structure. |
| 44 | */ |
| 45 | #define my_container_of(ptr, type, member) \ |
| 46 | ((type *)((char *)ptr - offsetof(type, member))) |
| 47 | |
| 48 | namespace my_timer_unittest { |
| 49 | |
| 50 | typedef struct { |
| 51 | my_timer_t timer; |
| 52 | unsigned int fired; |
| 53 | native_mutex_t mutex; |
| 54 | native_cond_t cond; |
| 55 | } test_timer_t; |
| 56 | |
| 57 | static void timer_notify_function(my_timer_t *timer) { |
| 58 | test_timer_t *test = my_container_of(timer, test_timer_t, timer); |
| 59 | |
| 60 | native_mutex_lock(&test->mutex); |
| 61 | test->fired++; |
| 62 | native_cond_signal(&test->cond); |
| 63 | native_mutex_unlock(&test->mutex); |
| 64 | } |
| 65 | |
| 66 | static void test_timer_create(test_timer_t *test) { |
| 67 | memset(test, 0, sizeof(test_timer_t)); |
| 68 | native_mutex_init(&test->mutex, nullptr); |
| 69 | native_cond_init(&test->cond); |
| 70 | test->timer.notify_function = timer_notify_function; |
| 71 | EXPECT_EQ(my_timer_create(&test->timer), 0); |
| 72 | } |
| 73 | |
| 74 | static void test_timer_destroy(test_timer_t *test) { |
| 75 | native_mutex_destroy(&test->mutex); |
| 76 | native_cond_destroy(&test->cond); |
| 77 | my_timer_delete(&test->timer); |
| 78 | } |
| 79 | |
| 80 | static void timer_set_and_wait(test_timer_t *test, unsigned int fired_count) { |
| 81 | int rc, state; |
| 82 | |
| 83 | rc = my_timer_set(&test->timer, 5); |
| 84 | EXPECT_EQ(rc, 0); |
| 85 | |
| 86 | // timer not fired yet |
| 87 | EXPECT_TRUE((test->fired != fired_count)); |
| 88 | |
| 89 | while (test->fired != fired_count) |
| 90 | native_cond_wait(&test->cond, &test->mutex); |
| 91 | |
| 92 | // timer fired |
| 93 | EXPECT_TRUE(test->fired == fired_count); |
| 94 | |
| 95 | rc = my_timer_cancel(&test->timer, &state); |
| 96 | EXPECT_EQ(rc, 0); |
| 97 | |
| 98 | // timer state was signaled |
| 99 | EXPECT_EQ(state, 0); |
| 100 | } |
| 101 | |
| 102 | static void test_timer(void) { |
| 103 | int rc; |
| 104 | test_timer_t test; |
| 105 | |
| 106 | test_timer_create(&test); |
| 107 | |
| 108 | native_mutex_lock(&test.mutex); |
| 109 | |
| 110 | rc = my_timer_set(&test.timer, 5); |
| 111 | EXPECT_EQ(rc, 0); |
| 112 | |
| 113 | /* not fired yet */ |
| 114 | EXPECT_EQ(test.fired, (unsigned int)0); |
| 115 | |
| 116 | while (!test.fired) native_cond_wait(&test.cond, &test.mutex); |
| 117 | |
| 118 | /* timer fired once */ |
| 119 | EXPECT_EQ(test.fired, (unsigned int)1); |
| 120 | |
| 121 | native_mutex_unlock(&test.mutex); |
| 122 | |
| 123 | test_timer_destroy(&test); |
| 124 | } |
| 125 | |
| 126 | extern "C" void *test_timer_per_thread(void *arg) { |
| 127 | int iter = *(int *)arg; |
| 128 | |
| 129 | while (iter--) test_timer(); |
| 130 | |
| 131 | return nullptr; |
| 132 | } |
| 133 | |
| 134 | /* Test timer creation and deletion. */ |
| 135 | TEST(Mysys, TimerCreateAndDelete) { |
| 136 | int rc; |
| 137 | my_timer_t timer; |
| 138 | |
| 139 | EXPECT_EQ(my_timer_initialize(), 0); |
| 140 | |
| 141 | memset(&timer, 0, sizeof(timer)); |
| 142 | |
| 143 | rc = my_timer_create(&timer); |
| 144 | EXPECT_EQ(rc, 0); |
| 145 | |
| 146 | my_timer_delete(&timer); |
| 147 | |
| 148 | my_timer_deinitialize(); |
| 149 | } |
| 150 | |
| 151 | /* Test single timer in one thread */ |
| 152 | TEST(Mysys, TestTimer) { |
| 153 | int rc; |
| 154 | test_timer_t test; |
| 155 | |
| 156 | EXPECT_EQ(my_timer_initialize(), 0); |
| 157 | |
| 158 | test_timer_create(&test); |
| 159 | |
| 160 | native_mutex_lock(&test.mutex); |
| 161 | |
| 162 | rc = my_timer_set(&test.timer, 5); |
| 163 | EXPECT_EQ(rc, 0); |
| 164 | |
| 165 | // timer not fired yet |
| 166 | EXPECT_EQ(test.fired, (unsigned int)0); |
| 167 | |
| 168 | while (!test.fired) native_cond_wait(&test.cond, &test.mutex); |
| 169 | |
| 170 | // timer fired once |
| 171 | EXPECT_EQ(test.fired, (unsigned int)1); |
| 172 | |
| 173 | native_mutex_unlock(&test.mutex); |
| 174 | |
| 175 | test_timer_destroy(&test); |
| 176 | |
| 177 | my_timer_deinitialize(); |
| 178 | } |
| 179 | |
| 180 | /* Test reset function of timer */ |
| 181 | TEST(Mysys, TestTimerReset) { |
| 182 | int rc, state; |
| 183 | test_timer_t test; |
| 184 | |
| 185 | EXPECT_EQ(my_timer_initialize(), 0); |
| 186 | |
| 187 | test_timer_create(&test); |
| 188 | |
| 189 | native_mutex_lock(&test.mutex); |
| 190 | |
| 191 | rc = my_timer_set(&test.timer, 600000); |
| 192 | EXPECT_EQ(rc, 0); |
| 193 | |
| 194 | // timer not fired yet |
| 195 | EXPECT_EQ(test.fired, (unsigned int)0); |
| 196 | |
| 197 | // reset timer |
| 198 | rc = my_timer_cancel(&test.timer, &state); |
| 199 | EXPECT_EQ(rc, 0); |
| 200 | |
| 201 | native_mutex_unlock(&test.mutex); |
| 202 | |
| 203 | test_timer_destroy(&test); |
| 204 | |
| 205 | my_timer_deinitialize(); |
| 206 | } |
| 207 | |
| 208 | /* Test multiple timers in single thread */ |
| 209 | TEST(Mysys, TestMultipleTimers) { |
| 210 | int rc, state; |
| 211 | test_timer_t test1, test2, test3; |
| 212 | |
| 213 | EXPECT_EQ(my_timer_initialize(), 0); |
| 214 | |
| 215 | // Timer "test1" |
| 216 | test_timer_create(&test1); |
| 217 | rc = my_timer_set(&test1.timer, 3); |
| 218 | EXPECT_EQ(rc, 0); |
| 219 | |
| 220 | // Timer "test2" |
| 221 | test_timer_create(&test2); |
| 222 | rc = my_timer_set(&test2.timer, 6); |
| 223 | EXPECT_EQ(rc, 0); |
| 224 | |
| 225 | // Timer "test3" |
| 226 | test_timer_create(&test3); |
| 227 | rc = my_timer_set(&test3.timer, 600000); |
| 228 | EXPECT_EQ(rc, 0); |
| 229 | |
| 230 | // Wait till test1 timer fired. |
| 231 | native_mutex_lock(&test1.mutex); |
| 232 | while (!test1.fired) native_cond_wait(&test1.cond, &test1.mutex); |
| 233 | native_mutex_unlock(&test1.mutex); |
| 234 | |
| 235 | // Wait till test2 timer fired. |
| 236 | native_mutex_lock(&test2.mutex); |
| 237 | while (!test2.fired) native_cond_wait(&test2.cond, &test2.mutex); |
| 238 | native_mutex_unlock(&test2.mutex); |
| 239 | |
| 240 | // timer test1 fired |
| 241 | EXPECT_EQ(test1.fired, (unsigned int)1); |
| 242 | |
| 243 | // timer test2 fired |
| 244 | EXPECT_EQ(test2.fired, (unsigned int)1); |
| 245 | |
| 246 | // timer test3 not fired yet |
| 247 | EXPECT_EQ(test3.fired, (unsigned int)0); |
| 248 | |
| 249 | // reset timer test3 |
| 250 | rc = my_timer_cancel(&test3.timer, &state); |
| 251 | EXPECT_EQ(rc, 0); |
| 252 | |
| 253 | test_timer_destroy(&test1); |
| 254 | test_timer_destroy(&test2); |
| 255 | test_timer_destroy(&test3); |
| 256 | |
| 257 | my_timer_deinitialize(); |
| 258 | } |
| 259 | |
| 260 | /* Test timer in multiple threads */ |
| 261 | TEST(Mysys, TestTimerPerThread) { |
| 262 | my_thread_attr_init(&thr_attr); |
| 263 | my_thread_attr_setdetachstate(&thr_attr, MY_THREAD_CREATE_DETACHED); |
| 264 | |
| 265 | EXPECT_EQ(my_timer_initialize(), 0); |
| 266 | |
| 267 | test_concurrently("per-thread", test_timer_per_thread, THREADS, 5); |
| 268 | |
| 269 | my_timer_deinitialize(); |
| 270 | my_thread_attr_destroy(&thr_attr); |
| 271 | } |
| 272 | |
| 273 | /* Test timer reuse functionality */ |
| 274 | TEST(Mysys, TestTimerReuse) { |
| 275 | test_timer_t test; |
| 276 | |
| 277 | EXPECT_EQ(my_timer_initialize(), 0); |
| 278 | |
| 279 | test_timer_create(&test); |
| 280 | |
| 281 | native_mutex_lock(&test.mutex); |
| 282 | |
| 283 | timer_set_and_wait(&test, 1); |
| 284 | timer_set_and_wait(&test, 2); |
| 285 | timer_set_and_wait(&test, 3); |
| 286 | |
| 287 | native_mutex_unlock(&test.mutex); |
| 288 | |
| 289 | test_timer_destroy(&test); |
| 290 | |
| 291 | my_timer_deinitialize(); |
| 292 | } |
| 293 | |
| 294 | /* Test timer module reinitialization */ |
| 295 | TEST(Mysys, TestReinitialization) { |
| 296 | EXPECT_EQ(my_timer_initialize(), 0); |
| 297 | test_timer(); |
| 298 | my_timer_deinitialize(); |
| 299 | |
| 300 | // Reinitialization |
| 301 | EXPECT_EQ(my_timer_initialize(), 0); |
| 302 | test_timer(); |
| 303 | my_timer_deinitialize(); |
| 304 | } |
| 305 | } // namespace my_timer_unittest |