blob: 4ef4ed1c683aeb7c6402a50162a0a48cb46ebb2e [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevf6468892014-01-29 01:04:14 -080020 */
21
22#include "util/scheduler.hpp"
23
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
Alexander Afanasyevf6468892014-01-29 01:04:14 -080025
26namespace ndn {
27
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070028BOOST_AUTO_TEST_SUITE(UtilTestScheduler)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080029
30struct SchedulerFixture
31{
32 SchedulerFixture()
33 : count1(0)
34 , count2(0)
35 , count3(0)
36 , count4(0)
37 {
38 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039
Alexander Afanasyevf6468892014-01-29 01:04:14 -080040 void
41 event1()
42 {
43 BOOST_CHECK_EQUAL(count3, 1);
44 ++count1;
45 }
46
47 void
48 event2()
49 {
50 ++count2;
51 }
52
53 void
54 event3()
55 {
56 BOOST_CHECK_EQUAL(count1, 0);
57 ++count3;
58 }
59
60 void
61 event4()
62 {
63 ++count4;
64 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070065
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066 int count1;
67 int count2;
68 int count3;
69 int count4;
70};
71
72BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
73{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 boost::asio::io_service io;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075
76 Scheduler scheduler(io);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077 scheduler.scheduleEvent(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
78
79 EventId i = scheduler.scheduleEvent(time::seconds(1), bind(&SchedulerFixture::event2, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080 scheduler.cancelEvent(i);
81
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070082 scheduler.scheduleEvent(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080083
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084 i = scheduler.scheduleEvent(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080085 scheduler.cancelEvent(i);
86
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070087 i = scheduler.schedulePeriodicEvent(time::milliseconds(1500), time::milliseconds(500), bind(&SchedulerFixture::event4, this));
Alexander Afanasyev83cdc5b2014-03-03 16:17:47 +000088 scheduler.scheduleEvent(time::seconds(4), bind(&Scheduler::cancelEvent, &scheduler, i));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070089
Alexander Afanasyevf6468892014-01-29 01:04:14 -080090 io.run();
91
92 BOOST_CHECK_EQUAL(count1, 1);
93 BOOST_CHECK_EQUAL(count2, 0);
94 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyev83cdc5b2014-03-03 16:17:47 +000095 BOOST_CHECK_GE(count4, 2);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080096}
97
Yingdi Yuf2a82092014-02-03 16:49:15 -080098BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
99{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100 boost::asio::io_service io;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800101 Scheduler scheduler(io);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700102
Yingdi Yuf2a82092014-02-03 16:49:15 -0800103 EventId i;
104 scheduler.cancelEvent(i);
105}
106
107struct SelfCancelFixture
108{
109 SelfCancelFixture()
110 : m_scheduler(m_io)
111 {
112 }
113
114 void
115 cancelSelf()
116 {
117 m_scheduler.cancelEvent(m_selfEventId);
118 }
119
120 boost::asio::io_service m_io;
121 Scheduler m_scheduler;
122 EventId m_selfEventId;
123};
124
125BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
126{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700127 m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800128 bind(&SelfCancelFixture::cancelSelf, this));
129
130 BOOST_REQUIRE_NO_THROW(m_io.run());
131}
132
133struct SelfRescheduleFixture
134{
135 SelfRescheduleFixture()
136 : m_scheduler(m_io)
137 , m_count(0)
138 {
139 }
140
141 void
142 reschedule()
143 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144 EventId eventId = m_scheduler.scheduleEvent(time::milliseconds(100),
145 bind(&SelfRescheduleFixture::reschedule, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800146 m_scheduler.cancelEvent(m_selfEventId);
147 m_selfEventId = eventId;
148
149 if(m_count < 5)
150 m_count++;
151 else
152 m_scheduler.cancelEvent(m_selfEventId);
153 }
154
155 void
156 reschedule2()
157 {
158 m_scheduler.cancelEvent(m_selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700159
160
Yingdi Yuf2a82092014-02-03 16:49:15 -0800161 if(m_count < 5)
162 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700163 m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800164 bind(&SelfRescheduleFixture::reschedule2, this));
165 m_count++;
166 }
167 }
168
169 void
170 doNothing()
171 {
172 m_count++;
173 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700174
Yingdi Yuf2a82092014-02-03 16:49:15 -0800175 void
176 reschedule3()
177 {
178 m_scheduler.cancelEvent(m_selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700179
180 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800181 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700182 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800183 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700184 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800185 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700186 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800187 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700188 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800189 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700190 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800191 bind(&SelfRescheduleFixture::doNothing, this));
192 }
193
194 boost::asio::io_service m_io;
195 Scheduler m_scheduler;
196 EventId m_selfEventId;
197 int m_count;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700198
Yingdi Yuf2a82092014-02-03 16:49:15 -0800199};
200
201BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
202{
203 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
204 bind(&SelfRescheduleFixture::reschedule, this));
205
206 BOOST_REQUIRE_NO_THROW(m_io.run());
207
208 BOOST_CHECK_EQUAL(m_count, 5);
209}
210
211BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
212{
213 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
214 bind(&SelfRescheduleFixture::reschedule2, this));
215
216 BOOST_REQUIRE_NO_THROW(m_io.run());
217
218 BOOST_CHECK_EQUAL(m_count, 5);
219}
220
221BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
222{
223 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
224 bind(&SelfRescheduleFixture::reschedule3, this));
225
226 BOOST_REQUIRE_NO_THROW(m_io.run());
227
228 BOOST_CHECK_EQUAL(m_count, 6);
229}
230
231
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800232BOOST_AUTO_TEST_SUITE_END()
233
234} // namespace ndn