1
#include "common/Mutex.h"
2
#include "common/Timer.h"
3
#include "common/common_init.h"
10
* Tests the timer classes
12
#define MAX_TEST_CONTEXTS 5
18
int array[MAX_TEST_CONTEXTS];
20
TestContext* test_contexts[MAX_TEST_CONTEXTS];
22
Mutex array_lock("test_timers_mutex");
25
class TestContext : public Context
33
virtual void finish(int r)
36
cout << "TestContext " << num << std::endl;
37
array[array_idx++] = num;
41
virtual ~TestContext()
49
class StrictOrderTestContext : public TestContext
52
StrictOrderTestContext (int num_)
57
virtual void finish(int r)
60
cout << "StrictOrderTestContext " << num << std::endl;
65
virtual ~StrictOrderTestContext()
70
static void print_status(const char *str, int ret)
73
cout << ((ret == 0) ? "SUCCESS" : "FAILURE");
78
static int basic_timer_test(T &timer, Mutex *lock)
81
memset(&array, 0, sizeof(array));
83
memset(&test_contexts, 0, sizeof(test_contexts));
85
cout << __PRETTY_FUNCTION__ << std::endl;
87
for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
88
test_contexts[i] = new TestContext(i);
92
for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
95
utime_t inc(2 * i, 0);
96
utime_t t = g_clock.now() + inc;
97
timer.add_event_at(t, test_contexts[i]);
106
done = (array_idx == MAX_TEST_CONTEXTS);
110
for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
113
cout << "error: expected array[" << i << "] = " << i
114
<< "; got " << array[i] << " instead." << std::endl;
121
static int test_out_of_order_insertion(SafeTimer &timer, Mutex *lock)
124
memset(&array, 0, sizeof(array));
126
memset(&test_contexts, 0, sizeof(test_contexts));
128
cout << __PRETTY_FUNCTION__ << std::endl;
130
test_contexts[0] = new StrictOrderTestContext(0);
131
test_contexts[1] = new StrictOrderTestContext(1);
135
utime_t t = g_clock.now() + inc;
137
timer.add_event_at(t, test_contexts[0]);
143
utime_t t = g_clock.now() + inc;
145
timer.add_event_at(t, test_contexts[1]);
150
for (; secs < 100 ; ++secs) {
161
cout << "error: expected array[" << 1 << "] = " << 1
162
<< "; got " << array[1] << " instead." << std::endl;
168
static int safe_timer_cancel_all_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
170
cout << __PRETTY_FUNCTION__ << std::endl;
173
memset(&array, 0, sizeof(array));
175
memset(&test_contexts, 0, sizeof(test_contexts));
177
for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
178
test_contexts[i] = new TestContext(i);
181
safe_timer_lock.Lock();
182
for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
183
utime_t inc(4 * i, 0);
184
utime_t t = g_clock.now() + inc;
185
safe_timer.add_event_at(t, test_contexts[i]);
187
safe_timer_lock.Unlock();
191
safe_timer_lock.Lock();
192
safe_timer.cancel_all_events();
193
safe_timer_lock.Unlock();
195
for (int i = 0; i < array_idx; ++i) {
198
cout << "error: expected array[" << i << "] = " << i
199
<< "; got " << array[i] << " instead." << std::endl;
206
static int safe_timer_cancellation_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
208
cout << __PRETTY_FUNCTION__ << std::endl;
211
memset(&array, 0, sizeof(array));
213
memset(&test_contexts, 0, sizeof(test_contexts));
215
for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
216
test_contexts[i] = new StrictOrderTestContext(i);
219
safe_timer_lock.Lock();
220
for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
221
utime_t inc(4 * i, 0);
222
utime_t t = g_clock.now() + inc;
223
safe_timer.add_event_at(t, test_contexts[i]);
225
safe_timer_lock.Unlock();
227
// cancel the even-numbered events
228
for (int i = 0; i < MAX_TEST_CONTEXTS; i += 2) {
229
safe_timer_lock.Lock();
230
safe_timer.cancel_event(test_contexts[i]);
231
safe_timer_lock.Unlock();
236
safe_timer_lock.Lock();
237
safe_timer.cancel_all_events();
238
safe_timer_lock.Unlock();
240
for (int i = 1; i < array_idx; i += 2) {
243
cout << "error: expected array[" << i << "] = " << i
244
<< "; got " << array[i] << " instead." << std::endl;
251
int main(int argc, const char **argv)
253
vector<const char*> args;
254
argv_to_vec(argc, argv, args);
257
ceph_set_default_id("admin");
258
common_set_defaults(false);
259
common_init(args, "ceph", true);
262
Mutex safe_timer_lock("safe_timer_lock");
263
SafeTimer safe_timer(safe_timer_lock);
265
ret = basic_timer_test <SafeTimer>(safe_timer, &safe_timer_lock);
269
ret = safe_timer_cancel_all_test(safe_timer, safe_timer_lock);
273
ret = safe_timer_cancellation_test(safe_timer, safe_timer_lock);
277
ret = test_out_of_order_insertion(safe_timer, &safe_timer_lock);
282
print_status(argv[0], ret);