blob: d8335c49127499615903b61adfd789e12063cdd5 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
Junxiao Shi07115cc2019-01-23 19:00:59 +00003 * Copyright (c) 2013-2019 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/util/scheduler.hpp"
Alexander Afanasyevf6468892014-01-29 01:04:14 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25#include "tests/unit/unit-test-time-fixture.hpp"
Davide Pesavento3a3e1882018-07-17 14:49:15 -040026
Junxiao Shid50f2b42016-08-10 02:59:59 +000027#include <boost/lexical_cast.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080028
29namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080030namespace util {
31namespace scheduler {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080032namespace tests {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080033
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080034using namespace ndn::tests;
35
Junxiao Shid50f2b42016-08-10 02:59:59 +000036class SchedulerFixture : public UnitTestTimeFixture
37{
38public:
39 SchedulerFixture()
40 : scheduler(io)
41 {
42 }
43
44public:
45 Scheduler scheduler;
46};
47
48BOOST_AUTO_TEST_SUITE(Util)
49BOOST_FIXTURE_TEST_SUITE(TestScheduler, SchedulerFixture)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080050
Davide Pesaventoeee3e822016-11-26 19:19:34 +010051BOOST_AUTO_TEST_SUITE(General)
52
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080053BOOST_AUTO_TEST_CASE(Events)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080054{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080055 size_t count1 = 0;
56 size_t count2 = 0;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080057
Davide Pesavento0f830802018-01-16 23:58:58 -050058 scheduler.scheduleEvent(500_ms, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080059 ++count1;
60 BOOST_CHECK_EQUAL(count2, 1);
61 });
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062
Davide Pesavento0f830802018-01-16 23:58:58 -050063 EventId i = scheduler.scheduleEvent(1_s, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080064 BOOST_ERROR("This event should not have been fired");
65 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066 scheduler.cancelEvent(i);
67
Davide Pesavento0f830802018-01-16 23:58:58 -050068 scheduler.scheduleEvent(250_ms, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080069 BOOST_CHECK_EQUAL(count1, 0);
70 ++count2;
71 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080072
Davide Pesavento0f830802018-01-16 23:58:58 -050073 i = scheduler.scheduleEvent(50_ms, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080074 BOOST_ERROR("This event should not have been fired");
75 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080076 scheduler.cancelEvent(i);
77
Davide Pesavento0f830802018-01-16 23:58:58 -050078 advanceClocks(25_ms, 1000_ms);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080079 BOOST_CHECK_EQUAL(count1, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080080 BOOST_CHECK_EQUAL(count2, 1);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080081}
82
Junxiao Shi86dfa532016-08-10 03:00:11 +000083BOOST_AUTO_TEST_CASE(CallbackException)
84{
85 class MyException : public std::exception
86 {
87 };
Davide Pesavento923ba442019-02-12 22:00:38 -050088 scheduler.scheduleEvent(10_ms, [] {
89 // use plain 'throw' to ensure that Scheduler does not depend on the
90 // internal machinery of NDN_THROW and that it can catch all exceptions
91 // regardless of how they are thrown by the application
92 throw MyException{};
93 });
Junxiao Shi86dfa532016-08-10 03:00:11 +000094
95 bool isCallbackInvoked = false;
Davide Pesavento0f830802018-01-16 23:58:58 -050096 scheduler.scheduleEvent(20_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shi86dfa532016-08-10 03:00:11 +000097
Davide Pesavento0f830802018-01-16 23:58:58 -050098 BOOST_CHECK_THROW(this->advanceClocks(6_ms, 2), MyException);
99 this->advanceClocks(6_ms, 2);
Junxiao Shi86dfa532016-08-10 03:00:11 +0000100 BOOST_CHECK(isCallbackInvoked);
101}
102
Yingdi Yuf2a82092014-02-03 16:49:15 -0800103BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
104{
Yingdi Yuf2a82092014-02-03 16:49:15 -0800105 EventId i;
106 scheduler.cancelEvent(i);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100107
108 // avoid "test case [...] did not check any assertions" message from Boost.Test
109 BOOST_CHECK(true);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800110}
111
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800112BOOST_AUTO_TEST_CASE(SelfCancel)
Yingdi Yuf2a82092014-02-03 16:49:15 -0800113{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800114 EventId selfEventId;
Davide Pesavento0f830802018-01-16 23:58:58 -0500115 selfEventId = scheduler.scheduleEvent(100_ms, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800116 scheduler.cancelEvent(selfEventId);
117 });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800118
Davide Pesavento0f830802018-01-16 23:58:58 -0500119 BOOST_REQUIRE_NO_THROW(advanceClocks(100_ms, 10));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800120}
121
Junxiao Shid50f2b42016-08-10 02:59:59 +0000122class SelfRescheduleFixture : public SchedulerFixture
Yingdi Yuf2a82092014-02-03 16:49:15 -0800123{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800124public:
Yingdi Yuf2a82092014-02-03 16:49:15 -0800125 SelfRescheduleFixture()
Junxiao Shid50f2b42016-08-10 02:59:59 +0000126 : count(0)
Yingdi Yuf2a82092014-02-03 16:49:15 -0800127 {
128 }
129
130 void
131 reschedule()
132 {
Davide Pesavento0f830802018-01-16 23:58:58 -0500133 EventId eventId = scheduler.scheduleEvent(100_ms,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800134 bind(&SelfRescheduleFixture::reschedule, this));
135 scheduler.cancelEvent(selfEventId);
136 selfEventId = eventId;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800137
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800138 if (count < 5)
139 count++;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800140 else
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800141 scheduler.cancelEvent(selfEventId);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800142 }
143
144 void
145 reschedule2()
146 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800147 scheduler.cancelEvent(selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700148
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800149 if (count < 5) {
Davide Pesavento0f830802018-01-16 23:58:58 -0500150 selfEventId = scheduler.scheduleEvent(100_ms,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800151 bind(&SelfRescheduleFixture::reschedule2, this));
152 count++;
153 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800154 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700155
Yingdi Yuf2a82092014-02-03 16:49:15 -0800156 void
157 reschedule3()
158 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800159 scheduler.cancelEvent(selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700160
Davide Pesavento0f830802018-01-16 23:58:58 -0500161 scheduler.scheduleEvent(100_ms, [&] { ++count; });
162 scheduler.scheduleEvent(100_ms, [&] { ++count; });
163 scheduler.scheduleEvent(100_ms, [&] { ++count; });
164 scheduler.scheduleEvent(100_ms, [&] { ++count; });
165 scheduler.scheduleEvent(100_ms, [&] { ++count; });
166 scheduler.scheduleEvent(100_ms, [&] { ++count; });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800167 }
168
Junxiao Shid50f2b42016-08-10 02:59:59 +0000169public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800170 EventId selfEventId;
171 size_t count;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800172};
173
174BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
175{
Davide Pesavento0f830802018-01-16 23:58:58 -0500176 selfEventId = scheduler.scheduleEvent(0_s,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800177 bind(&SelfRescheduleFixture::reschedule, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800178
Davide Pesavento0f830802018-01-16 23:58:58 -0500179 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800180
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800181 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800182}
183
184BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
185{
Davide Pesavento0f830802018-01-16 23:58:58 -0500186 selfEventId = scheduler.scheduleEvent(0_s,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800187 bind(&SelfRescheduleFixture::reschedule2, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800188
Davide Pesavento0f830802018-01-16 23:58:58 -0500189 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800190
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800191 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800192}
193
194BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
195{
Davide Pesavento0f830802018-01-16 23:58:58 -0500196 selfEventId = scheduler.scheduleEvent(0_s,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800197 bind(&SelfRescheduleFixture::reschedule3, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800198
Davide Pesavento0f830802018-01-16 23:58:58 -0500199 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800200
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800201 BOOST_CHECK_EQUAL(count, 6);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800202}
203
Junxiao Shid50f2b42016-08-10 02:59:59 +0000204class CancelAllFixture : public SchedulerFixture
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700205{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000206public:
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700207 CancelAllFixture()
Junxiao Shid50f2b42016-08-10 02:59:59 +0000208 : count(0)
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700209 {
210 }
211
212 void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700213 event()
214 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800215 ++count;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700216
Davide Pesavento0f830802018-01-16 23:58:58 -0500217 scheduler.scheduleEvent(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700218 }
219
Junxiao Shid50f2b42016-08-10 02:59:59 +0000220public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800221 uint32_t count;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700222};
223
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700224BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
225{
Davide Pesavento0f830802018-01-16 23:58:58 -0500226 scheduler.scheduleEvent(500_ms, [&] { scheduler.cancelAllEvents(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700227
Davide Pesavento0f830802018-01-16 23:58:58 -0500228 scheduler.scheduleEvent(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700229
Davide Pesavento0f830802018-01-16 23:58:58 -0500230 scheduler.scheduleEvent(3_s, [] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800231 BOOST_ERROR("This event should have been cancelled" );
232 });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700233
Davide Pesavento0f830802018-01-16 23:58:58 -0500234 advanceClocks(100_ms, 100);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700235
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800236 BOOST_CHECK_EQUAL(count, 0);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700237}
238
Junxiao Shid50f2b42016-08-10 02:59:59 +0000239BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800240{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000241 Scheduler sched(io);
Junxiao Shi07115cc2019-01-23 19:00:59 +0000242 ScopedEventId eid = sched.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000243 sched.cancelAllEvents();
244 eid.cancel(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100245
246 // avoid "test case [...] did not check any assertions" message from Boost.Test
247 BOOST_CHECK(true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000248}
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700249
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100250BOOST_AUTO_TEST_SUITE_END() // General
251
Junxiao Shid50f2b42016-08-10 02:59:59 +0000252BOOST_AUTO_TEST_SUITE(EventId)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800253
Junxiao Shid50f2b42016-08-10 02:59:59 +0000254using scheduler::EventId;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800255
Junxiao Shid50f2b42016-08-10 02:59:59 +0000256BOOST_AUTO_TEST_CASE(ConstructEmpty)
257{
258 EventId eid;
Junxiao Shi07115cc2019-01-23 19:00:59 +0000259 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000260}
261
262BOOST_AUTO_TEST_CASE(Compare)
263{
264 EventId eid, eid2;
265 BOOST_CHECK_EQUAL(eid == eid2, true);
266 BOOST_CHECK_EQUAL(eid != eid2, false);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000267
Davide Pesavento0f830802018-01-16 23:58:58 -0500268 eid = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000269 BOOST_CHECK_EQUAL(eid == eid2, false);
270 BOOST_CHECK_EQUAL(eid != eid2, true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000271
272 eid2 = eid;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400273 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000274 BOOST_CHECK_EQUAL(eid != eid2, false);
275
Davide Pesavento0f830802018-01-16 23:58:58 -0500276 eid2 = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000277 BOOST_CHECK_EQUAL(eid == eid2, false);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400278 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000279}
280
281BOOST_AUTO_TEST_CASE(Valid)
282{
283 EventId eid;
284 BOOST_CHECK_EQUAL(static_cast<bool>(eid), false);
285 BOOST_CHECK_EQUAL(!eid, true);
286
Davide Pesavento0f830802018-01-16 23:58:58 -0500287 eid = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000288 BOOST_CHECK_EQUAL(static_cast<bool>(eid), true);
289 BOOST_CHECK_EQUAL(!eid, false);
290
291 EventId eid2 = eid;
292 scheduler.cancelEvent(eid2);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400293 BOOST_CHECK(!eid);
294 BOOST_CHECK(!eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000295}
296
297BOOST_AUTO_TEST_CASE(DuringCallback)
298{
299 EventId eid;
Davide Pesavento0f830802018-01-16 23:58:58 -0500300 EventId eid2 = scheduler.scheduleEvent(20_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000301
302 bool isCallbackInvoked = false;
Davide Pesavento0f830802018-01-16 23:58:58 -0500303 eid = scheduler.scheduleEvent(10_ms, [this, &eid, &eid2, &isCallbackInvoked] {
Junxiao Shid50f2b42016-08-10 02:59:59 +0000304 isCallbackInvoked = true;
305
306 // eid is "expired" during callback execution
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400307 BOOST_CHECK(!eid);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400308 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000309
310 scheduler.cancelEvent(eid2);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400311 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000312 });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400313
Davide Pesavento0f830802018-01-16 23:58:58 -0500314 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000315 BOOST_CHECK(isCallbackInvoked);
316}
317
318BOOST_AUTO_TEST_CASE(Reset)
319{
320 bool isCallbackInvoked = false;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400321 EventId eid = scheduler.scheduleEvent(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shid50f2b42016-08-10 02:59:59 +0000322 eid.reset();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400323 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000324
Davide Pesavento0f830802018-01-16 23:58:58 -0500325 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000326 BOOST_CHECK(isCallbackInvoked);
327}
328
329BOOST_AUTO_TEST_CASE(ToString)
330{
331 std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>());
332
333 EventId eid;
334 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString);
335
Davide Pesavento0f830802018-01-16 23:58:58 -0500336 eid = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000337 BOOST_TEST_MESSAGE("eid=" << eid);
338 BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString);
339}
340
341BOOST_AUTO_TEST_SUITE_END() // EventId
342
343BOOST_AUTO_TEST_SUITE(ScopedEventId)
344
345using scheduler::ScopedEventId;
346
347BOOST_AUTO_TEST_CASE(Destruct)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800348{
349 int hit = 0;
350 {
Junxiao Shi07115cc2019-01-23 19:00:59 +0000351 ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800352 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500353 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800354 BOOST_CHECK_EQUAL(hit, 0);
355}
356
Junxiao Shid50f2b42016-08-10 02:59:59 +0000357BOOST_AUTO_TEST_CASE(Assign)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800358{
359 int hit1 = 0, hit2 = 0;
Junxiao Shi07115cc2019-01-23 19:00:59 +0000360 ScopedEventId se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit1; });
Davide Pesavento0f830802018-01-16 23:58:58 -0500361 se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit2; });
362 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800363 BOOST_CHECK_EQUAL(hit1, 0);
364 BOOST_CHECK_EQUAL(hit2, 1);
365}
366
Junxiao Shid50f2b42016-08-10 02:59:59 +0000367BOOST_AUTO_TEST_CASE(Release)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800368{
369 int hit = 0;
370 {
Junxiao Shi07115cc2019-01-23 19:00:59 +0000371 ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800372 se.release();
373 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500374 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800375 BOOST_CHECK_EQUAL(hit, 1);
376}
377
Junxiao Shid50f2b42016-08-10 02:59:59 +0000378BOOST_AUTO_TEST_CASE(Move)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800379{
380 int hit = 0;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400381 unique_ptr<ScopedEventId> se2;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800382 {
Junxiao Shi07115cc2019-01-23 19:00:59 +0000383 ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400384 se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800385 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500386 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800387 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400388
Junxiao Shi07115cc2019-01-23 19:00:59 +0000389 ScopedEventId se3;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400390 {
Junxiao Shi07115cc2019-01-23 19:00:59 +0000391 ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400392 se3 = std::move(se); // move assignment
393 } // se goes out of scope
394 this->advanceClocks(1_ms, 15);
395 BOOST_CHECK_EQUAL(hit, 2);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800396}
397
398BOOST_AUTO_TEST_SUITE_END() // ScopedEventId
399
Junxiao Shid50f2b42016-08-10 02:59:59 +0000400BOOST_AUTO_TEST_SUITE_END() // TestScheduler
401BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800402
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800403} // namespace tests
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800404} // namespace scheduler
405} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800406} // namespace ndn