Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 1 | #include "event-scheduler.h" |
| 2 | |
| 3 | #include <boost/test/unit_test.hpp> |
| 4 | #include <map> |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | using namespace boost; |
| 8 | using namespace std; |
| 9 | |
| 10 | BOOST_AUTO_TEST_SUITE(SchedulerTests) |
| 11 | |
| 12 | map<string, int> table; |
| 13 | |
| 14 | void func(string str) |
| 15 | { |
| 16 | map<string, int>::iterator it = table.find(str); |
| 17 | if (it == table.end()) |
| 18 | { |
| 19 | table.insert(make_pair(str, 1)); |
| 20 | } |
| 21 | else |
| 22 | { |
| 23 | int count = it->second; |
| 24 | count++; |
| 25 | table.erase(it); |
| 26 | table.insert(make_pair(str, count)); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | bool |
| 31 | matcher(const TaskPtr &task) |
| 32 | { |
| 33 | return task->tag() == "period" || task->tag() == "world"; |
| 34 | } |
| 35 | |
| 36 | BOOST_AUTO_TEST_CASE(SchedulerTest) |
| 37 | { |
| 38 | SchedulerPtr scheduler(new Scheduler()); |
| 39 | IntervalGeneratorPtr generator(new SimpleIntervalGenerator(0.2)); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 40 | IntervalGeneratorPtr randomGen(new RandomIntervalGenerator(0.05, 0.5)); |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 41 | |
| 42 | string tag1 = "hello"; |
| 43 | string tag2 = "world"; |
| 44 | string tag3 = "period"; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 45 | string tag4 = "haha"; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 46 | |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 47 | TaskPtr task1(new OneTimeTask(boost::bind(func, tag1), tag1, scheduler, 0.5)); |
| 48 | TaskPtr task2(new OneTimeTask(boost::bind(func, tag2), tag2, scheduler, 0.5)); |
| 49 | TaskPtr task3(new PeriodicTask(boost::bind(func, tag3), tag3, scheduler, generator)); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 50 | TaskPtr task4(new PeriodicTask(boost::bind(func, tag4), tag4, scheduler, randomGen, 5)); |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 51 | |
| 52 | scheduler->start(); |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 53 | scheduler->addTask(task1); |
| 54 | scheduler->addTask(task2); |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 55 | scheduler->addTask(task3); |
| 56 | BOOST_CHECK_EQUAL(scheduler->size(), 3); |
| 57 | usleep(600000); |
| 58 | BOOST_CHECK_EQUAL(scheduler->size(), 1); |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 59 | scheduler->addTask(task1); |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(scheduler->size(), 2); |
| 61 | usleep(600000); |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 62 | scheduler->addTask(task1); |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(scheduler->size(), 2); |
| 64 | usleep(400000); |
| 65 | scheduler->deleteTask(task1->tag()); |
| 66 | BOOST_CHECK_EQUAL(scheduler->size(), 1); |
| 67 | usleep(200000); |
| 68 | |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 69 | scheduler->addTask(task1); |
| 70 | scheduler->addTask(task2); |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(scheduler->size(), 3); |
| 72 | usleep(100000); |
| 73 | scheduler->deleteTask(bind(matcher, _1)); |
| 74 | BOOST_CHECK_EQUAL(scheduler->size(), 1); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 75 | usleep(500000); |
| 76 | |
| 77 | scheduler->addTask(task4); |
| 78 | usleep(500000); |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 79 | |
| 80 | scheduler->shutdown(); |
| 81 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 82 | int hello = 0, world = 0, period = 0, haha = 0; |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 83 | |
| 84 | map<string, int>::iterator it; |
| 85 | it = table.find(tag1); |
| 86 | if (it != table.end()) |
| 87 | { |
| 88 | hello = it->second; |
| 89 | } |
| 90 | it = table.find(tag2); |
| 91 | if (it != table.end()) |
| 92 | { |
| 93 | world = it->second; |
| 94 | } |
| 95 | it = table.find(tag3); |
| 96 | if (it != table.end()) |
| 97 | { |
| 98 | period = it->second; |
| 99 | } |
| 100 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 101 | it = table.find(tag4); |
| 102 | if (it != table.end()) |
| 103 | { |
| 104 | haha = it->second; |
| 105 | } |
| 106 | |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 107 | // added four times, canceled once before invoking callback |
| 108 | BOOST_CHECK_EQUAL(hello, 3); |
| 109 | // added two times, canceled once by matcher before invoking callback |
| 110 | BOOST_CHECK_EQUAL(world, 1); |
| 111 | // invoked every 0.2 seconds before deleted by matcher |
| 112 | BOOST_CHECK_EQUAL(period, static_cast<int>((0.6 + 0.6 + 0.4 + 0.2 + 0.1) / 0.2)); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 113 | // should be invoked 5 times exactly |
| 114 | BOOST_CHECK_EQUAL(haha, 5); |
Zhenkai Zhu | bc2f628 | 2013-01-08 16:40:58 -0800 | [diff] [blame] | 115 | |
| 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_CASE(GeneratorTest) |
| 119 | { |
| 120 | double interval = 10; |
| 121 | double percent = 0.5; |
| 122 | int times = 10000; |
| 123 | IntervalGeneratorPtr generator(new RandomIntervalGenerator(interval, percent)); |
| 124 | double sum = 0.0; |
| 125 | double min = 2 * interval; |
| 126 | double max = -1; |
| 127 | for (int i = 0; i < times; i++) |
| 128 | { |
| 129 | double next = generator->nextInterval(); |
| 130 | sum += next; |
| 131 | if (next > max) |
| 132 | { |
| 133 | max = next; |
| 134 | } |
| 135 | if (next < min) |
| 136 | { |
| 137 | min = next; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | BOOST_CHECK( abs(1.0 - (sum / static_cast<double>(times)) / interval) < 0.05); |
| 142 | BOOST_CHECK( min > interval * (1 - percent / 2.0)); |
| 143 | BOOST_CHECK( max < interval * (1 + percent / 2.0)); |
| 144 | BOOST_CHECK( abs(1.0 - ((max - min) / interval) / percent) < 0.05); |
| 145 | |
| 146 | } |
| 147 | |
| 148 | BOOST_AUTO_TEST_SUITE_END() |