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