blob: a046b93283933516745eca9609c7290c9b0d4f9e [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)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080036 {
37 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070038
Alexander Afanasyevf6468892014-01-29 01:04:14 -080039 void
40 event1()
41 {
42 BOOST_CHECK_EQUAL(count3, 1);
43 ++count1;
44 }
45
46 void
47 event2()
48 {
49 ++count2;
50 }
51
52 void
53 event3()
54 {
55 BOOST_CHECK_EQUAL(count1, 0);
56 ++count3;
57 }
58
Alexander Afanasyevf6468892014-01-29 01:04:14 -080059 int count1;
60 int count2;
61 int count3;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080062};
63
64BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
65{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066 boost::asio::io_service io;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080067
68 Scheduler scheduler(io);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 scheduler.scheduleEvent(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
70
71 EventId i = scheduler.scheduleEvent(time::seconds(1), bind(&SchedulerFixture::event2, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080072 scheduler.cancelEvent(i);
73
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 scheduler.scheduleEvent(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076 i = scheduler.scheduleEvent(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080077 scheduler.cancelEvent(i);
78
Alexander Afanasyevf6468892014-01-29 01:04:14 -080079 io.run();
80
81 BOOST_CHECK_EQUAL(count1, 1);
82 BOOST_CHECK_EQUAL(count2, 0);
83 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080084}
85
Yingdi Yuf2a82092014-02-03 16:49:15 -080086BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
87{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 boost::asio::io_service io;
Yingdi Yuf2a82092014-02-03 16:49:15 -080089 Scheduler scheduler(io);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090
Yingdi Yuf2a82092014-02-03 16:49:15 -080091 EventId i;
92 scheduler.cancelEvent(i);
93}
94
95struct SelfCancelFixture
96{
97 SelfCancelFixture()
98 : m_scheduler(m_io)
99 {
100 }
101
102 void
103 cancelSelf()
104 {
105 m_scheduler.cancelEvent(m_selfEventId);
106 }
107
108 boost::asio::io_service m_io;
109 Scheduler m_scheduler;
110 EventId m_selfEventId;
111};
112
113BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
114{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700115 m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800116 bind(&SelfCancelFixture::cancelSelf, this));
117
118 BOOST_REQUIRE_NO_THROW(m_io.run());
119}
120
121struct SelfRescheduleFixture
122{
123 SelfRescheduleFixture()
124 : m_scheduler(m_io)
125 , m_count(0)
126 {
127 }
128
129 void
130 reschedule()
131 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700132 EventId eventId = m_scheduler.scheduleEvent(time::milliseconds(100),
133 bind(&SelfRescheduleFixture::reschedule, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800134 m_scheduler.cancelEvent(m_selfEventId);
135 m_selfEventId = eventId;
136
137 if(m_count < 5)
138 m_count++;
139 else
140 m_scheduler.cancelEvent(m_selfEventId);
141 }
142
143 void
144 reschedule2()
145 {
146 m_scheduler.cancelEvent(m_selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700147
148
Yingdi Yuf2a82092014-02-03 16:49:15 -0800149 if(m_count < 5)
150 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700151 m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800152 bind(&SelfRescheduleFixture::reschedule2, this));
153 m_count++;
154 }
155 }
156
157 void
158 doNothing()
159 {
160 m_count++;
161 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700162
Yingdi Yuf2a82092014-02-03 16:49:15 -0800163 void
164 reschedule3()
165 {
166 m_scheduler.cancelEvent(m_selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700167
168 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800169 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700170 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800171 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700172 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800173 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700174 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800175 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700176 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800177 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700178 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800179 bind(&SelfRescheduleFixture::doNothing, this));
180 }
181
182 boost::asio::io_service m_io;
183 Scheduler m_scheduler;
184 EventId m_selfEventId;
185 int m_count;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700186
Yingdi Yuf2a82092014-02-03 16:49:15 -0800187};
188
189BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
190{
191 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
192 bind(&SelfRescheduleFixture::reschedule, this));
193
194 BOOST_REQUIRE_NO_THROW(m_io.run());
195
196 BOOST_CHECK_EQUAL(m_count, 5);
197}
198
199BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
200{
201 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
202 bind(&SelfRescheduleFixture::reschedule2, this));
203
204 BOOST_REQUIRE_NO_THROW(m_io.run());
205
206 BOOST_CHECK_EQUAL(m_count, 5);
207}
208
209BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
210{
211 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
212 bind(&SelfRescheduleFixture::reschedule3, this));
213
214 BOOST_REQUIRE_NO_THROW(m_io.run());
215
216 BOOST_CHECK_EQUAL(m_count, 6);
217}
218
219
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800220BOOST_AUTO_TEST_SUITE_END()
221
222} // namespace ndn