blob: c8c8179b79d65fc6e1dc99f0a6f81b9f457f117a [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 Afanasyevd3a55b22014-11-18 19:23:28 -080025#include "../unit-test-time-fixture.hpp"
Alexander Afanasyevf6468892014-01-29 01:04:14 -080026
27namespace ndn {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080028namespace tests {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080029
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080030BOOST_FIXTURE_TEST_SUITE(UtilTestScheduler, UnitTestTimeFixture)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080031
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080032BOOST_AUTO_TEST_CASE(Events)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080033{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080034 size_t count1 = 0;
35 size_t count2 = 0;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080036
37 Scheduler scheduler(io);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080038 scheduler.scheduleEvent(time::milliseconds(500), [&] {
39 ++count1;
40 BOOST_CHECK_EQUAL(count2, 1);
41 });
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080043 EventId i = scheduler.scheduleEvent(time::seconds(1), [&] {
44 BOOST_ERROR("This event should not have been fired");
45 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080046 scheduler.cancelEvent(i);
47
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080048 scheduler.scheduleEvent(time::milliseconds(250), [&] {
49 BOOST_CHECK_EQUAL(count1, 0);
50 ++count2;
51 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080052
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080053 i = scheduler.scheduleEvent(time::milliseconds(50), [&] {
54 BOOST_ERROR("This event should not have been fired");
55 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080056 scheduler.cancelEvent(i);
57
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080058 advanceClocks(time::milliseconds(1), 1000);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080059 BOOST_CHECK_EQUAL(count1, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080060 BOOST_CHECK_EQUAL(count2, 1);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080061}
62
Yingdi Yuf2a82092014-02-03 16:49:15 -080063BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
64{
Yingdi Yuf2a82092014-02-03 16:49:15 -080065 Scheduler scheduler(io);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066
Yingdi Yuf2a82092014-02-03 16:49:15 -080067 EventId i;
68 scheduler.cancelEvent(i);
69}
70
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080071BOOST_AUTO_TEST_CASE(SelfCancel)
Yingdi Yuf2a82092014-02-03 16:49:15 -080072{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080073 Scheduler scheduler(io);
Yingdi Yuf2a82092014-02-03 16:49:15 -080074
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080075 EventId selfEventId;
76 selfEventId = scheduler.scheduleEvent(time::milliseconds(100), [&] {
77 scheduler.cancelEvent(selfEventId);
78 });
Yingdi Yuf2a82092014-02-03 16:49:15 -080079
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080080 BOOST_REQUIRE_NO_THROW(advanceClocks(time::milliseconds(100), 10));
Yingdi Yuf2a82092014-02-03 16:49:15 -080081}
82
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080083class SelfRescheduleFixture : public UnitTestTimeFixture
Yingdi Yuf2a82092014-02-03 16:49:15 -080084{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080085public:
Yingdi Yuf2a82092014-02-03 16:49:15 -080086 SelfRescheduleFixture()
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080087 : scheduler(io)
88 , count(0)
Yingdi Yuf2a82092014-02-03 16:49:15 -080089 {
90 }
91
92 void
93 reschedule()
94 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080095 EventId eventId = scheduler.scheduleEvent(time::milliseconds(100),
96 bind(&SelfRescheduleFixture::reschedule, this));
97 scheduler.cancelEvent(selfEventId);
98 selfEventId = eventId;
Yingdi Yuf2a82092014-02-03 16:49:15 -080099
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800100 if (count < 5)
101 count++;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800102 else
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800103 scheduler.cancelEvent(selfEventId);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800104 }
105
106 void
107 reschedule2()
108 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800109 scheduler.cancelEvent(selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800111 if (count < 5) {
112 selfEventId = scheduler.scheduleEvent(time::milliseconds(100),
113 bind(&SelfRescheduleFixture::reschedule2, this));
114 count++;
115 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800116 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117
Yingdi Yuf2a82092014-02-03 16:49:15 -0800118 void
119 reschedule3()
120 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800121 scheduler.cancelEvent(selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700122
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800123 scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
124 scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
125 scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
126 scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
127 scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
128 scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800129 }
130
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800131 Scheduler scheduler;
132 EventId selfEventId;
133 size_t count;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800134};
135
136BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
137{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800138 selfEventId = scheduler.scheduleEvent(time::seconds(0),
139 bind(&SelfRescheduleFixture::reschedule, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800140
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800141 BOOST_REQUIRE_NO_THROW(advanceClocks(time::milliseconds(10), 1000));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800142
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800143 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800144}
145
146BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
147{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800148 selfEventId = scheduler.scheduleEvent(time::seconds(0),
149 bind(&SelfRescheduleFixture::reschedule2, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800150
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800151 BOOST_REQUIRE_NO_THROW(advanceClocks(time::milliseconds(10), 1000));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800152
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800153 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800154}
155
156BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
157{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800158 selfEventId = scheduler.scheduleEvent(time::seconds(0),
159 bind(&SelfRescheduleFixture::reschedule3, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800160
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800161 BOOST_REQUIRE_NO_THROW(advanceClocks(time::milliseconds(10), 1000));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800162
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800163 BOOST_CHECK_EQUAL(count, 6);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800164}
165
166
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800167struct CancelAllFixture : public UnitTestTimeFixture
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700168{
169 CancelAllFixture()
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800170 : scheduler(io)
171 , count(0)
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700172 {
173 }
174
175 void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700176 event()
177 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800178 ++count;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700179
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800180 scheduler.scheduleEvent(time::seconds(1), [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700181 }
182
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800183 Scheduler scheduler;
184 uint32_t count;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700185};
186
187
188BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
189{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800190 scheduler.scheduleEvent(time::milliseconds(500), [&] { scheduler.cancelAllEvents(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700191
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800192 scheduler.scheduleEvent(time::seconds(1), [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700193
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800194 scheduler.scheduleEvent(time::seconds(3), [] {
195 BOOST_ERROR("This event should have been cancelled" );
196 });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700197
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800198 advanceClocks(time::milliseconds(100), 100);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700199
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800200 BOOST_CHECK_EQUAL(count, 0);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700201}
202
203
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800204BOOST_AUTO_TEST_SUITE_END()
205
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800206} // namespace tests
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800207} // namespace ndn