1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/* Copyright (c) 2014, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <gtest/gtest.h>
#include <string.h>
#include "my_sys.h"
#include "my_systime.h"
#include "my_thread.h"
#include "my_timer.h"
#include "unittest/gunit/thr_template.cc"
#include "unittest/gunit/thread_utils.h"
#ifdef HAVE_PSI_INTERFACE
PSI_mutex_key key_thd_timer_mutex = PSI_NOT_INSTRUMENTED;
#endif
/**
Cast a member of a structure to the structure that contains it.
@param ptr Pointer to the member.
@param type Type of the structure that contains the member.
@param member Name of the member within the structure.
*/
#define my_container_of(ptr, type, member) \
((type *)((char *)ptr - offsetof(type, member)))
namespace my_timer_unittest {
typedef struct {
my_timer_t timer;
unsigned int fired;
native_mutex_t mutex;
native_cond_t cond;
} test_timer_t;
static void timer_notify_function(my_timer_t *timer) {
test_timer_t *test = my_container_of(timer, test_timer_t, timer);
native_mutex_lock(&test->mutex);
test->fired++;
native_cond_signal(&test->cond);
native_mutex_unlock(&test->mutex);
}
static void test_timer_create(test_timer_t *test) {
memset(test, 0, sizeof(test_timer_t));
native_mutex_init(&test->mutex, nullptr);
native_cond_init(&test->cond);
test->timer.notify_function = timer_notify_function;
EXPECT_EQ(my_timer_create(&test->timer), 0);
}
static void test_timer_destroy(test_timer_t *test) {
native_mutex_destroy(&test->mutex);
native_cond_destroy(&test->cond);
my_timer_delete(&test->timer);
}
static void timer_set_and_wait(test_timer_t *test, unsigned int fired_count) {
int rc, state;
rc = my_timer_set(&test->timer, 5);
EXPECT_EQ(rc, 0);
// timer not fired yet
EXPECT_TRUE((test->fired != fired_count));
while (test->fired != fired_count)
native_cond_wait(&test->cond, &test->mutex);
// timer fired
EXPECT_TRUE(test->fired == fired_count);
rc = my_timer_cancel(&test->timer, &state);
EXPECT_EQ(rc, 0);
// timer state was signaled
EXPECT_EQ(state, 0);
}
static void test_timer(void) {
int rc;
test_timer_t test;
test_timer_create(&test);
native_mutex_lock(&test.mutex);
rc = my_timer_set(&test.timer, 5);
EXPECT_EQ(rc, 0);
/* not fired yet */
EXPECT_EQ(test.fired, (unsigned int)0);
while (!test.fired) native_cond_wait(&test.cond, &test.mutex);
/* timer fired once */
EXPECT_EQ(test.fired, (unsigned int)1);
native_mutex_unlock(&test.mutex);
test_timer_destroy(&test);
}
extern "C" void *test_timer_per_thread(void *arg) {
int iter = *(int *)arg;
while (iter--) test_timer();
return nullptr;
}
/* Test timer creation and deletion. */
TEST(Mysys, TimerCreateAndDelete) {
int rc;
my_timer_t timer;
EXPECT_EQ(my_timer_initialize(), 0);
memset(&timer, 0, sizeof(timer));
rc = my_timer_create(&timer);
EXPECT_EQ(rc, 0);
my_timer_delete(&timer);
my_timer_deinitialize();
}
/* Test single timer in one thread */
TEST(Mysys, TestTimer) {
int rc;
test_timer_t test;
EXPECT_EQ(my_timer_initialize(), 0);
test_timer_create(&test);
native_mutex_lock(&test.mutex);
rc = my_timer_set(&test.timer, 5);
EXPECT_EQ(rc, 0);
// timer not fired yet
EXPECT_EQ(test.fired, (unsigned int)0);
while (!test.fired) native_cond_wait(&test.cond, &test.mutex);
// timer fired once
EXPECT_EQ(test.fired, (unsigned int)1);
native_mutex_unlock(&test.mutex);
test_timer_destroy(&test);
my_timer_deinitialize();
}
/* Test reset function of timer */
TEST(Mysys, TestTimerReset) {
int rc, state;
test_timer_t test;
EXPECT_EQ(my_timer_initialize(), 0);
test_timer_create(&test);
native_mutex_lock(&test.mutex);
rc = my_timer_set(&test.timer, 600000);
EXPECT_EQ(rc, 0);
// timer not fired yet
EXPECT_EQ(test.fired, (unsigned int)0);
// reset timer
rc = my_timer_cancel(&test.timer, &state);
EXPECT_EQ(rc, 0);
native_mutex_unlock(&test.mutex);
test_timer_destroy(&test);
my_timer_deinitialize();
}
/* Test multiple timers in single thread */
TEST(Mysys, TestMultipleTimers) {
int rc, state;
test_timer_t test1, test2, test3;
EXPECT_EQ(my_timer_initialize(), 0);
// Timer "test1"
test_timer_create(&test1);
rc = my_timer_set(&test1.timer, 3);
EXPECT_EQ(rc, 0);
// Timer "test2"
test_timer_create(&test2);
rc = my_timer_set(&test2.timer, 6);
EXPECT_EQ(rc, 0);
// Timer "test3"
test_timer_create(&test3);
rc = my_timer_set(&test3.timer, 600000);
EXPECT_EQ(rc, 0);
// Wait till test1 timer fired.
native_mutex_lock(&test1.mutex);
while (!test1.fired) native_cond_wait(&test1.cond, &test1.mutex);
native_mutex_unlock(&test1.mutex);
// Wait till test2 timer fired.
native_mutex_lock(&test2.mutex);
while (!test2.fired) native_cond_wait(&test2.cond, &test2.mutex);
native_mutex_unlock(&test2.mutex);
// timer test1 fired
EXPECT_EQ(test1.fired, (unsigned int)1);
// timer test2 fired
EXPECT_EQ(test2.fired, (unsigned int)1);
// timer test3 not fired yet
EXPECT_EQ(test3.fired, (unsigned int)0);
// reset timer test3
rc = my_timer_cancel(&test3.timer, &state);
EXPECT_EQ(rc, 0);
test_timer_destroy(&test1);
test_timer_destroy(&test2);
test_timer_destroy(&test3);
my_timer_deinitialize();
}
/* Test timer in multiple threads */
TEST(Mysys, TestTimerPerThread) {
my_thread_attr_init(&thr_attr);
my_thread_attr_setdetachstate(&thr_attr, MY_THREAD_CREATE_DETACHED);
EXPECT_EQ(my_timer_initialize(), 0);
test_concurrently("per-thread", test_timer_per_thread, THREADS, 5);
my_timer_deinitialize();
my_thread_attr_destroy(&thr_attr);
}
/* Test timer reuse functionality */
TEST(Mysys, TestTimerReuse) {
test_timer_t test;
EXPECT_EQ(my_timer_initialize(), 0);
test_timer_create(&test);
native_mutex_lock(&test.mutex);
timer_set_and_wait(&test, 1);
timer_set_and_wait(&test, 2);
timer_set_and_wait(&test, 3);
native_mutex_unlock(&test.mutex);
test_timer_destroy(&test);
my_timer_deinitialize();
}
/* Test timer module reinitialization */
TEST(Mysys, TestReinitialization) {
EXPECT_EQ(my_timer_initialize(), 0);
test_timer();
my_timer_deinitialize();
// Reinitialization
EXPECT_EQ(my_timer_initialize(), 0);
test_timer();
my_timer_deinitialize();
}
} // namespace my_timer_unittest
|