blob: f1c32149e0980263b70c5231e403c7be25da1ded [file] [log] [blame]
Alexander Afanasyevf6468892014-01-29 01:04:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "util/scheduler.hpp"
8
9#include <boost/test/unit_test.hpp>
10
11namespace ndn {
12
13BOOST_AUTO_TEST_SUITE(TestScheduler)
14
15struct SchedulerFixture
16{
17 SchedulerFixture()
18 : count1(0)
19 , count2(0)
20 , count3(0)
21 , count4(0)
22 {
23 }
24
25 void
26 event1()
27 {
28 BOOST_CHECK_EQUAL(count3, 1);
29 ++count1;
30 }
31
32 void
33 event2()
34 {
35 ++count2;
36 }
37
38 void
39 event3()
40 {
41 BOOST_CHECK_EQUAL(count1, 0);
42 ++count3;
43 }
44
45 void
46 event4()
47 {
48 ++count4;
49 }
50
51 int count1;
52 int count2;
53 int count3;
54 int count4;
55};
56
57BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
58{
59 boost::asio::io_service io;
60
61 Scheduler scheduler(io);
62 scheduler.scheduleEvent(time::seconds(0.1), bind(&SchedulerFixture::event1, this));
63
64 EventId i = scheduler.scheduleEvent(time::seconds(0.2), bind(&SchedulerFixture::event2, this));
65 scheduler.cancelEvent(i);
66
67 scheduler.scheduleEvent(time::seconds(0.05), bind(&SchedulerFixture::event3, this));
68
69 i = scheduler.scheduleEvent(time::seconds(0.01), bind(&SchedulerFixture::event2, this));
70 scheduler.cancelEvent(i);
71
72 i = scheduler.schedulePeriodicEvent(time::seconds(0.3), time::seconds(0.1), bind(&SchedulerFixture::event4, this));
73 scheduler.scheduleEvent(time::seconds(0.69), bind(&Scheduler::cancelEvent, &scheduler, i));
74
75 io.run();
76
77 BOOST_CHECK_EQUAL(count1, 1);
78 BOOST_CHECK_EQUAL(count2, 0);
79 BOOST_CHECK_EQUAL(count3, 1);
80 BOOST_CHECK_EQUAL(count4, 4);
81}
82
Yingdi Yuf2a82092014-02-03 16:49:15 -080083BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
84{
85 boost::asio::io_service io;
86 Scheduler scheduler(io);
87
88 EventId i;
89 scheduler.cancelEvent(i);
90}
91
92struct SelfCancelFixture
93{
94 SelfCancelFixture()
95 : m_scheduler(m_io)
96 {
97 }
98
99 void
100 cancelSelf()
101 {
102 m_scheduler.cancelEvent(m_selfEventId);
103 }
104
105 boost::asio::io_service m_io;
106 Scheduler m_scheduler;
107 EventId m_selfEventId;
108};
109
110BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
111{
112 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0.1),
113 bind(&SelfCancelFixture::cancelSelf, this));
114
115 BOOST_REQUIRE_NO_THROW(m_io.run());
116}
117
118struct SelfRescheduleFixture
119{
120 SelfRescheduleFixture()
121 : m_scheduler(m_io)
122 , m_count(0)
123 {
124 }
125
126 void
127 reschedule()
128 {
129 EventId eventId = m_scheduler.scheduleEvent(time::seconds(0.1),
130 bind(&SelfRescheduleFixture::reschedule, this));
131 m_scheduler.cancelEvent(m_selfEventId);
132 m_selfEventId = eventId;
133
134 if(m_count < 5)
135 m_count++;
136 else
137 m_scheduler.cancelEvent(m_selfEventId);
138 }
139
140 void
141 reschedule2()
142 {
143 m_scheduler.cancelEvent(m_selfEventId);
144
145
146 if(m_count < 5)
147 {
148 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0.1),
149 bind(&SelfRescheduleFixture::reschedule2, this));
150 m_count++;
151 }
152 }
153
154 void
155 doNothing()
156 {
157 m_count++;
158 }
159
160 void
161 reschedule3()
162 {
163 m_scheduler.cancelEvent(m_selfEventId);
164
165 m_scheduler.scheduleEvent(time::seconds(0.1),
166 bind(&SelfRescheduleFixture::doNothing, this));
167 m_scheduler.scheduleEvent(time::seconds(0.1),
168 bind(&SelfRescheduleFixture::doNothing, this));
169 m_scheduler.scheduleEvent(time::seconds(0.1),
170 bind(&SelfRescheduleFixture::doNothing, this));
171 m_scheduler.scheduleEvent(time::seconds(0.1),
172 bind(&SelfRescheduleFixture::doNothing, this));
173 m_scheduler.scheduleEvent(time::seconds(0.1),
174 bind(&SelfRescheduleFixture::doNothing, this));
175 m_scheduler.scheduleEvent(time::seconds(0.1),
176 bind(&SelfRescheduleFixture::doNothing, this));
177 }
178
179 boost::asio::io_service m_io;
180 Scheduler m_scheduler;
181 EventId m_selfEventId;
182 int m_count;
183
184};
185
186BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
187{
188 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
189 bind(&SelfRescheduleFixture::reschedule, this));
190
191 BOOST_REQUIRE_NO_THROW(m_io.run());
192
193 BOOST_CHECK_EQUAL(m_count, 5);
194}
195
196BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
197{
198 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
199 bind(&SelfRescheduleFixture::reschedule2, this));
200
201 BOOST_REQUIRE_NO_THROW(m_io.run());
202
203 BOOST_CHECK_EQUAL(m_count, 5);
204}
205
206BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
207{
208 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
209 bind(&SelfRescheduleFixture::reschedule3, this));
210
211 BOOST_REQUIRE_NO_THROW(m_io.run());
212
213 BOOST_CHECK_EQUAL(m_count, 6);
214}
215
216
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800217BOOST_AUTO_TEST_SUITE_END()
218
219} // namespace ndn