blob: b827296b567d38bb3c6a288d4a91b176c0ef33e4 [file] [log] [blame]
Zhenkai Zhubc2f6282013-01-08 16:40:58 -08001#include "event-scheduler.h"
2
3#include <boost/test/unit_test.hpp>
4#include <map>
5#include <unistd.h>
6
7using namespace boost;
8using namespace std;
9
10BOOST_AUTO_TEST_SUITE(SchedulerTests)
11
12map<string, int> table;
13
14void 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
30bool
31matcher(const TaskPtr &task)
32{
33 return task->tag() == "period" || task->tag() == "world";
34}
35
36BOOST_AUTO_TEST_CASE(SchedulerTest)
37{
38 SchedulerPtr scheduler(new Scheduler());
39 IntervalGeneratorPtr generator(new SimpleIntervalGenerator(0.2));
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080040 IntervalGeneratorPtr randomGen(new RandomIntervalGenerator(0.05, 0.5));
Zhenkai Zhubc2f6282013-01-08 16:40:58 -080041
42 string tag1 = "hello";
43 string tag2 = "world";
44 string tag3 = "period";
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080045 string tag4 = "haha";
Zhenkai Zhubc2f6282013-01-08 16:40:58 -080046
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -080047 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 Zhu74dd53c2013-01-10 23:39:57 -080050 TaskPtr task4(new PeriodicTask(boost::bind(func, tag4), tag4, scheduler, randomGen, 5));
Zhenkai Zhubc2f6282013-01-08 16:40:58 -080051
52 scheduler->start();
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -080053 scheduler->addTask(task1);
54 scheduler->addTask(task2);
Zhenkai Zhubc2f6282013-01-08 16:40:58 -080055 scheduler->addTask(task3);
56 BOOST_CHECK_EQUAL(scheduler->size(), 3);
57 usleep(600000);
58 BOOST_CHECK_EQUAL(scheduler->size(), 1);
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -080059 scheduler->addTask(task1);
Zhenkai Zhubc2f6282013-01-08 16:40:58 -080060 BOOST_CHECK_EQUAL(scheduler->size(), 2);
61 usleep(600000);
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -080062 scheduler->addTask(task1);
Zhenkai Zhubc2f6282013-01-08 16:40:58 -080063 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 Zhu66dc5a92013-01-08 21:41:15 -080069 scheduler->addTask(task1);
70 scheduler->addTask(task2);
Zhenkai Zhubc2f6282013-01-08 16:40:58 -080071 BOOST_CHECK_EQUAL(scheduler->size(), 3);
72 usleep(100000);
73 scheduler->deleteTask(bind(matcher, _1));
74 BOOST_CHECK_EQUAL(scheduler->size(), 1);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080075 usleep(500000);
76
77 scheduler->addTask(task4);
78 usleep(500000);
Zhenkai Zhubc2f6282013-01-08 16:40:58 -080079
80 scheduler->shutdown();
81
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080082 int hello = 0, world = 0, period = 0, haha = 0;
Zhenkai Zhubc2f6282013-01-08 16:40:58 -080083
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 Zhu74dd53c2013-01-10 23:39:57 -0800101 it = table.find(tag4);
102 if (it != table.end())
103 {
104 haha = it->second;
105 }
106
Zhenkai Zhubc2f6282013-01-08 16:40:58 -0800107 // 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 Zhu74dd53c2013-01-10 23:39:57 -0800113 // should be invoked 5 times exactly
114 BOOST_CHECK_EQUAL(haha, 5);
Zhenkai Zhubc2f6282013-01-08 16:40:58 -0800115
116}
117
118BOOST_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
148BOOST_AUTO_TEST_SUITE_END()