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)); |
| 40 | |
| 41 | string tag1 = "hello"; |
| 42 | string tag2 = "world"; |
| 43 | string tag3 = "period"; |
| 44 | |
| 45 | TaskPtr task1(new Task(boost::bind(func, tag1), tag1, scheduler)); |
| 46 | TaskPtr task2(new Task(boost::bind(func, tag2), tag2, scheduler)); |
| 47 | TaskPtr task3(new Task(boost::bind(func, tag3), tag3, scheduler, generator)); |
| 48 | |
| 49 | scheduler->start(); |
| 50 | scheduler->addTask(task1, 0.5); |
| 51 | scheduler->addTask(task2, 0.5); |
| 52 | scheduler->addTask(task3); |
| 53 | BOOST_CHECK_EQUAL(scheduler->size(), 3); |
| 54 | usleep(600000); |
| 55 | BOOST_CHECK_EQUAL(scheduler->size(), 1); |
| 56 | task1->reset(); |
| 57 | scheduler->addTask(task1, 0.5); |
| 58 | BOOST_CHECK_EQUAL(scheduler->size(), 2); |
| 59 | usleep(600000); |
| 60 | task1->reset(); |
| 61 | scheduler->addTask(task1, 0.5); |
| 62 | BOOST_CHECK_EQUAL(scheduler->size(), 2); |
| 63 | usleep(400000); |
| 64 | scheduler->deleteTask(task1->tag()); |
| 65 | BOOST_CHECK_EQUAL(scheduler->size(), 1); |
| 66 | usleep(200000); |
| 67 | |
| 68 | task1->reset(); |
| 69 | task2->reset(); |
| 70 | scheduler->addTask(task1, 0.5); |
| 71 | scheduler->addTask(task2, 0.5); |
| 72 | BOOST_CHECK_EQUAL(scheduler->size(), 3); |
| 73 | usleep(100000); |
| 74 | scheduler->deleteTask(bind(matcher, _1)); |
| 75 | BOOST_CHECK_EQUAL(scheduler->size(), 1); |
| 76 | usleep(1000000); |
| 77 | |
| 78 | scheduler->shutdown(); |
| 79 | |
| 80 | int hello = 0, world = 0, period = 0; |
| 81 | |
| 82 | map<string, int>::iterator it; |
| 83 | it = table.find(tag1); |
| 84 | if (it != table.end()) |
| 85 | { |
| 86 | hello = it->second; |
| 87 | } |
| 88 | it = table.find(tag2); |
| 89 | if (it != table.end()) |
| 90 | { |
| 91 | world = it->second; |
| 92 | } |
| 93 | it = table.find(tag3); |
| 94 | if (it != table.end()) |
| 95 | { |
| 96 | period = it->second; |
| 97 | } |
| 98 | |
| 99 | // added four times, canceled once before invoking callback |
| 100 | BOOST_CHECK_EQUAL(hello, 3); |
| 101 | // added two times, canceled once by matcher before invoking callback |
| 102 | BOOST_CHECK_EQUAL(world, 1); |
| 103 | // invoked every 0.2 seconds before deleted by matcher |
| 104 | BOOST_CHECK_EQUAL(period, static_cast<int>((0.6 + 0.6 + 0.4 + 0.2 + 0.1) / 0.2)); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | BOOST_AUTO_TEST_CASE(GeneratorTest) |
| 109 | { |
| 110 | double interval = 10; |
| 111 | double percent = 0.5; |
| 112 | int times = 10000; |
| 113 | IntervalGeneratorPtr generator(new RandomIntervalGenerator(interval, percent)); |
| 114 | double sum = 0.0; |
| 115 | double min = 2 * interval; |
| 116 | double max = -1; |
| 117 | for (int i = 0; i < times; i++) |
| 118 | { |
| 119 | double next = generator->nextInterval(); |
| 120 | sum += next; |
| 121 | if (next > max) |
| 122 | { |
| 123 | max = next; |
| 124 | } |
| 125 | if (next < min) |
| 126 | { |
| 127 | min = next; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | BOOST_CHECK( abs(1.0 - (sum / static_cast<double>(times)) / interval) < 0.05); |
| 132 | BOOST_CHECK( min > interval * (1 - percent / 2.0)); |
| 133 | BOOST_CHECK( max < interval * (1 + percent / 2.0)); |
| 134 | BOOST_CHECK( abs(1.0 - ((max - min) / interval) / percent) < 0.05); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_SUITE_END() |