1 #include "common/ceph_argparse.h"
2 #include "common/Mutex.h"
3 #include "common/Timer.h"
4 #include "global/global_init.h"
11 * Tests the timer classes
13 #define MAX_TEST_CONTEXTS 5
19 int test_array[MAX_TEST_CONTEXTS];
21 TestContext* test_contexts[MAX_TEST_CONTEXTS];
23 Mutex array_lock("test_timers_mutex");
26 class TestContext : public Context
29 explicit TestContext(int num_)
34 void finish(int r) override
37 cout << "TestContext " << num << std::endl;
38 test_array[array_idx++] = num;
42 ~TestContext() override
50 class StrictOrderTestContext : public TestContext
53 explicit StrictOrderTestContext (int num_)
58 void finish(int r) override
61 cout << "StrictOrderTestContext " << num << std::endl;
62 test_array[num] = num;
66 ~StrictOrderTestContext() override
71 static void print_status(const char *str, int ret)
74 cout << ((ret == 0) ? "SUCCESS" : "FAILURE");
79 static int basic_timer_test(T &timer, Mutex *lock)
82 memset(&test_array, 0, sizeof(test_array));
84 memset(&test_contexts, 0, sizeof(test_contexts));
86 cout << __PRETTY_FUNCTION__ << std::endl;
88 for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
89 test_contexts[i] = new TestContext(i);
93 for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
96 utime_t inc(2 * i, 0);
97 utime_t t = ceph_clock_now() + inc;
98 timer.add_event_at(t, test_contexts[i]);
107 done = (array_idx == MAX_TEST_CONTEXTS);
111 for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
112 if (test_array[i] != i) {
114 cout << "error: expected test_array[" << i << "] = " << i
115 << "; got " << test_array[i] << " instead." << std::endl;
122 static int test_out_of_order_insertion(SafeTimer &timer, Mutex *lock)
125 memset(&test_array, 0, sizeof(test_array));
127 memset(&test_contexts, 0, sizeof(test_contexts));
129 cout << __PRETTY_FUNCTION__ << std::endl;
131 test_contexts[0] = new StrictOrderTestContext(0);
132 test_contexts[1] = new StrictOrderTestContext(1);
136 utime_t t = ceph_clock_now() + inc;
138 timer.add_event_at(t, test_contexts[0]);
144 utime_t t = ceph_clock_now() + inc;
146 timer.add_event_at(t, test_contexts[1]);
151 for (; secs < 100 ; ++secs) {
154 int a = test_array[1];
162 cout << "error: expected test_array[" << 1 << "] = " << 1
163 << "; got " << test_array[1] << " instead." << std::endl;
169 static int safe_timer_cancel_all_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
171 cout << __PRETTY_FUNCTION__ << std::endl;
174 memset(&test_array, 0, sizeof(test_array));
176 memset(&test_contexts, 0, sizeof(test_contexts));
178 for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
179 test_contexts[i] = new TestContext(i);
182 safe_timer_lock.Lock();
183 for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
184 utime_t inc(4 * i, 0);
185 utime_t t = ceph_clock_now() + inc;
186 safe_timer.add_event_at(t, test_contexts[i]);
188 safe_timer_lock.Unlock();
192 safe_timer_lock.Lock();
193 safe_timer.cancel_all_events();
194 safe_timer_lock.Unlock();
196 for (int i = 0; i < array_idx; ++i) {
197 if (test_array[i] != i) {
199 cout << "error: expected test_array[" << i << "] = " << i
200 << "; got " << test_array[i] << " instead." << std::endl;
207 static int safe_timer_cancellation_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
209 cout << __PRETTY_FUNCTION__ << std::endl;
212 memset(&test_array, 0, sizeof(test_array));
214 memset(&test_contexts, 0, sizeof(test_contexts));
216 for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
217 test_contexts[i] = new StrictOrderTestContext(i);
220 safe_timer_lock.Lock();
221 for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
222 utime_t inc(4 * i, 0);
223 utime_t t = ceph_clock_now() + inc;
224 safe_timer.add_event_at(t, test_contexts[i]);
226 safe_timer_lock.Unlock();
228 // cancel the even-numbered events
229 for (int i = 0; i < MAX_TEST_CONTEXTS; i += 2) {
230 safe_timer_lock.Lock();
231 safe_timer.cancel_event(test_contexts[i]);
232 safe_timer_lock.Unlock();
237 safe_timer_lock.Lock();
238 safe_timer.cancel_all_events();
239 safe_timer_lock.Unlock();
241 for (int i = 1; i < array_idx; i += 2) {
242 if (test_array[i] != i) {
244 cout << "error: expected test_array[" << i << "] = " << i
245 << "; got " << test_array[i] << " instead." << std::endl;
252 int main(int argc, const char **argv)
254 vector<const char*> args;
255 argv_to_vec(argc, argv, args);
258 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
259 CODE_ENVIRONMENT_UTILITY, 0);
260 common_init_finish(g_ceph_context);
263 Mutex safe_timer_lock("safe_timer_lock");
264 SafeTimer safe_timer(g_ceph_context, safe_timer_lock);
266 ret = basic_timer_test <SafeTimer>(safe_timer, &safe_timer_lock);
270 ret = safe_timer_cancel_all_test(safe_timer, safe_timer_lock);
274 ret = safe_timer_cancellation_test(safe_timer, safe_timer_lock);
278 ret = test_out_of_order_insertion(safe_timer, &safe_timer_lock);
283 print_status(argv[0], ret);