blob: 9807637d64849367755a995cf36d87ef89f8b275 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyevf6468892014-01-29 01:04:14 -080011 */
12
13#include "util/scheduler.hpp"
14
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070015#include "boost-test.hpp"
Alexander Afanasyevf6468892014-01-29 01:04:14 -080016
17namespace ndn {
18
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070019BOOST_AUTO_TEST_SUITE(UtilTestScheduler)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080020
21struct SchedulerFixture
22{
23 SchedulerFixture()
24 : count1(0)
25 , count2(0)
26 , count3(0)
27 , count4(0)
28 {
29 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070030
Alexander Afanasyevf6468892014-01-29 01:04:14 -080031 void
32 event1()
33 {
34 BOOST_CHECK_EQUAL(count3, 1);
35 ++count1;
36 }
37
38 void
39 event2()
40 {
41 ++count2;
42 }
43
44 void
45 event3()
46 {
47 BOOST_CHECK_EQUAL(count1, 0);
48 ++count3;
49 }
50
51 void
52 event4()
53 {
54 ++count4;
55 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070056
Alexander Afanasyevf6468892014-01-29 01:04:14 -080057 int count1;
58 int count2;
59 int count3;
60 int count4;
61};
62
63BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
64{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070065 boost::asio::io_service io;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066
67 Scheduler scheduler(io);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070068 scheduler.scheduleEvent(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
69
70 EventId i = scheduler.scheduleEvent(time::seconds(1), bind(&SchedulerFixture::event2, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080071 scheduler.cancelEvent(i);
72
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073 scheduler.scheduleEvent(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080074
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070075 i = scheduler.scheduleEvent(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080076 scheduler.cancelEvent(i);
77
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070078 i = scheduler.schedulePeriodicEvent(time::milliseconds(1500), time::milliseconds(500), bind(&SchedulerFixture::event4, this));
Alexander Afanasyev83cdc5b2014-03-03 16:17:47 +000079 scheduler.scheduleEvent(time::seconds(4), bind(&Scheduler::cancelEvent, &scheduler, i));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080
Alexander Afanasyevf6468892014-01-29 01:04:14 -080081 io.run();
82
83 BOOST_CHECK_EQUAL(count1, 1);
84 BOOST_CHECK_EQUAL(count2, 0);
85 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyev83cdc5b2014-03-03 16:17:47 +000086 BOOST_CHECK_GE(count4, 2);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080087}
88
Yingdi Yuf2a82092014-02-03 16:49:15 -080089BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
90{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091 boost::asio::io_service io;
Yingdi Yuf2a82092014-02-03 16:49:15 -080092 Scheduler scheduler(io);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070093
Yingdi Yuf2a82092014-02-03 16:49:15 -080094 EventId i;
95 scheduler.cancelEvent(i);
96}
97
98struct SelfCancelFixture
99{
100 SelfCancelFixture()
101 : m_scheduler(m_io)
102 {
103 }
104
105 void
106 cancelSelf()
107 {
108 m_scheduler.cancelEvent(m_selfEventId);
109 }
110
111 boost::asio::io_service m_io;
112 Scheduler m_scheduler;
113 EventId m_selfEventId;
114};
115
116BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
117{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700118 m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800119 bind(&SelfCancelFixture::cancelSelf, this));
120
121 BOOST_REQUIRE_NO_THROW(m_io.run());
122}
123
124struct SelfRescheduleFixture
125{
126 SelfRescheduleFixture()
127 : m_scheduler(m_io)
128 , m_count(0)
129 {
130 }
131
132 void
133 reschedule()
134 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700135 EventId eventId = m_scheduler.scheduleEvent(time::milliseconds(100),
136 bind(&SelfRescheduleFixture::reschedule, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800137 m_scheduler.cancelEvent(m_selfEventId);
138 m_selfEventId = eventId;
139
140 if(m_count < 5)
141 m_count++;
142 else
143 m_scheduler.cancelEvent(m_selfEventId);
144 }
145
146 void
147 reschedule2()
148 {
149 m_scheduler.cancelEvent(m_selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700150
151
Yingdi Yuf2a82092014-02-03 16:49:15 -0800152 if(m_count < 5)
153 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700154 m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800155 bind(&SelfRescheduleFixture::reschedule2, this));
156 m_count++;
157 }
158 }
159
160 void
161 doNothing()
162 {
163 m_count++;
164 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700165
Yingdi Yuf2a82092014-02-03 16:49:15 -0800166 void
167 reschedule3()
168 {
169 m_scheduler.cancelEvent(m_selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700170
171 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800172 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700173 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800174 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700175 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800176 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700177 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800178 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700179 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800180 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700181 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800182 bind(&SelfRescheduleFixture::doNothing, this));
183 }
184
185 boost::asio::io_service m_io;
186 Scheduler m_scheduler;
187 EventId m_selfEventId;
188 int m_count;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700189
Yingdi Yuf2a82092014-02-03 16:49:15 -0800190};
191
192BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
193{
194 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
195 bind(&SelfRescheduleFixture::reschedule, this));
196
197 BOOST_REQUIRE_NO_THROW(m_io.run());
198
199 BOOST_CHECK_EQUAL(m_count, 5);
200}
201
202BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
203{
204 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
205 bind(&SelfRescheduleFixture::reschedule2, this));
206
207 BOOST_REQUIRE_NO_THROW(m_io.run());
208
209 BOOST_CHECK_EQUAL(m_count, 5);
210}
211
212BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
213{
214 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
215 bind(&SelfRescheduleFixture::reschedule3, this));
216
217 BOOST_REQUIRE_NO_THROW(m_io.run());
218
219 BOOST_CHECK_EQUAL(m_count, 6);
220}
221
222
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800223BOOST_AUTO_TEST_SUITE_END()
224
225} // namespace ndn