blob: 49c90b6cec37e7f7b7550fd495e21fc3afc16e1b [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 Pesavento0f830802018-01-16 23:58:58 -050088 scheduler.scheduleEvent(10_ms, [] { BOOST_THROW_EXCEPTION(MyException()); });
Junxiao Shi86dfa532016-08-10 03:00:11 +000089
90 bool isCallbackInvoked = false;
Davide Pesavento0f830802018-01-16 23:58:58 -050091 scheduler.scheduleEvent(20_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shi86dfa532016-08-10 03:00:11 +000092
Davide Pesavento0f830802018-01-16 23:58:58 -050093 BOOST_CHECK_THROW(this->advanceClocks(6_ms, 2), MyException);
94 this->advanceClocks(6_ms, 2);
Junxiao Shi86dfa532016-08-10 03:00:11 +000095 BOOST_CHECK(isCallbackInvoked);
96}
97
Yingdi Yuf2a82092014-02-03 16:49:15 -080098BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
99{
Yingdi Yuf2a82092014-02-03 16:49:15 -0800100 EventId i;
101 scheduler.cancelEvent(i);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100102
103 // avoid "test case [...] did not check any assertions" message from Boost.Test
104 BOOST_CHECK(true);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800105}
106
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800107BOOST_AUTO_TEST_CASE(SelfCancel)
Yingdi Yuf2a82092014-02-03 16:49:15 -0800108{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800109 EventId selfEventId;
Davide Pesavento0f830802018-01-16 23:58:58 -0500110 selfEventId = scheduler.scheduleEvent(100_ms, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800111 scheduler.cancelEvent(selfEventId);
112 });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800113
Davide Pesavento0f830802018-01-16 23:58:58 -0500114 BOOST_REQUIRE_NO_THROW(advanceClocks(100_ms, 10));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800115}
116
Junxiao Shid50f2b42016-08-10 02:59:59 +0000117class SelfRescheduleFixture : public SchedulerFixture
Yingdi Yuf2a82092014-02-03 16:49:15 -0800118{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800119public:
Yingdi Yuf2a82092014-02-03 16:49:15 -0800120 SelfRescheduleFixture()
Junxiao Shid50f2b42016-08-10 02:59:59 +0000121 : count(0)
Yingdi Yuf2a82092014-02-03 16:49:15 -0800122 {
123 }
124
125 void
126 reschedule()
127 {
Davide Pesavento0f830802018-01-16 23:58:58 -0500128 EventId eventId = scheduler.scheduleEvent(100_ms,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800129 bind(&SelfRescheduleFixture::reschedule, this));
130 scheduler.cancelEvent(selfEventId);
131 selfEventId = eventId;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800132
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800133 if (count < 5)
134 count++;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800135 else
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800136 scheduler.cancelEvent(selfEventId);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800137 }
138
139 void
140 reschedule2()
141 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800142 scheduler.cancelEvent(selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700143
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800144 if (count < 5) {
Davide Pesavento0f830802018-01-16 23:58:58 -0500145 selfEventId = scheduler.scheduleEvent(100_ms,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800146 bind(&SelfRescheduleFixture::reschedule2, this));
147 count++;
148 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800149 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700150
Yingdi Yuf2a82092014-02-03 16:49:15 -0800151 void
152 reschedule3()
153 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800154 scheduler.cancelEvent(selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700155
Davide Pesavento0f830802018-01-16 23:58:58 -0500156 scheduler.scheduleEvent(100_ms, [&] { ++count; });
157 scheduler.scheduleEvent(100_ms, [&] { ++count; });
158 scheduler.scheduleEvent(100_ms, [&] { ++count; });
159 scheduler.scheduleEvent(100_ms, [&] { ++count; });
160 scheduler.scheduleEvent(100_ms, [&] { ++count; });
161 scheduler.scheduleEvent(100_ms, [&] { ++count; });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800162 }
163
Junxiao Shid50f2b42016-08-10 02:59:59 +0000164public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800165 EventId selfEventId;
166 size_t count;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800167};
168
169BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
170{
Davide Pesavento0f830802018-01-16 23:58:58 -0500171 selfEventId = scheduler.scheduleEvent(0_s,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800172 bind(&SelfRescheduleFixture::reschedule, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800173
Davide Pesavento0f830802018-01-16 23:58:58 -0500174 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800175
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800176 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800177}
178
179BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
180{
Davide Pesavento0f830802018-01-16 23:58:58 -0500181 selfEventId = scheduler.scheduleEvent(0_s,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800182 bind(&SelfRescheduleFixture::reschedule2, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800183
Davide Pesavento0f830802018-01-16 23:58:58 -0500184 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800185
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800186 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800187}
188
189BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
190{
Davide Pesavento0f830802018-01-16 23:58:58 -0500191 selfEventId = scheduler.scheduleEvent(0_s,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800192 bind(&SelfRescheduleFixture::reschedule3, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800193
Davide Pesavento0f830802018-01-16 23:58:58 -0500194 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800195
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800196 BOOST_CHECK_EQUAL(count, 6);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800197}
198
Junxiao Shid50f2b42016-08-10 02:59:59 +0000199class CancelAllFixture : public SchedulerFixture
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700200{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000201public:
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700202 CancelAllFixture()
Junxiao Shid50f2b42016-08-10 02:59:59 +0000203 : count(0)
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700204 {
205 }
206
207 void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700208 event()
209 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800210 ++count;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700211
Davide Pesavento0f830802018-01-16 23:58:58 -0500212 scheduler.scheduleEvent(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700213 }
214
Junxiao Shid50f2b42016-08-10 02:59:59 +0000215public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800216 uint32_t count;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700217};
218
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700219BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
220{
Davide Pesavento0f830802018-01-16 23:58:58 -0500221 scheduler.scheduleEvent(500_ms, [&] { scheduler.cancelAllEvents(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700222
Davide Pesavento0f830802018-01-16 23:58:58 -0500223 scheduler.scheduleEvent(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700224
Davide Pesavento0f830802018-01-16 23:58:58 -0500225 scheduler.scheduleEvent(3_s, [] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800226 BOOST_ERROR("This event should have been cancelled" );
227 });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700228
Davide Pesavento0f830802018-01-16 23:58:58 -0500229 advanceClocks(100_ms, 100);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700230
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800231 BOOST_CHECK_EQUAL(count, 0);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700232}
233
Junxiao Shid50f2b42016-08-10 02:59:59 +0000234BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800235{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000236 Scheduler sched(io);
Junxiao Shi07115cc2019-01-23 19:00:59 +0000237 ScopedEventId eid = sched.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000238 sched.cancelAllEvents();
239 eid.cancel(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100240
241 // avoid "test case [...] did not check any assertions" message from Boost.Test
242 BOOST_CHECK(true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000243}
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700244
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100245BOOST_AUTO_TEST_SUITE_END() // General
246
Junxiao Shid50f2b42016-08-10 02:59:59 +0000247BOOST_AUTO_TEST_SUITE(EventId)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800248
Junxiao Shid50f2b42016-08-10 02:59:59 +0000249using scheduler::EventId;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800250
Junxiao Shid50f2b42016-08-10 02:59:59 +0000251BOOST_AUTO_TEST_CASE(ConstructEmpty)
252{
253 EventId eid;
Junxiao Shi07115cc2019-01-23 19:00:59 +0000254 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000255}
256
257BOOST_AUTO_TEST_CASE(Compare)
258{
259 EventId eid, eid2;
260 BOOST_CHECK_EQUAL(eid == eid2, true);
261 BOOST_CHECK_EQUAL(eid != eid2, false);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000262
Davide Pesavento0f830802018-01-16 23:58:58 -0500263 eid = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000264 BOOST_CHECK_EQUAL(eid == eid2, false);
265 BOOST_CHECK_EQUAL(eid != eid2, true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000266
267 eid2 = eid;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400268 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000269 BOOST_CHECK_EQUAL(eid != eid2, false);
270
Davide Pesavento0f830802018-01-16 23:58:58 -0500271 eid2 = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000272 BOOST_CHECK_EQUAL(eid == eid2, false);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400273 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000274}
275
276BOOST_AUTO_TEST_CASE(Valid)
277{
278 EventId eid;
279 BOOST_CHECK_EQUAL(static_cast<bool>(eid), false);
280 BOOST_CHECK_EQUAL(!eid, true);
281
Davide Pesavento0f830802018-01-16 23:58:58 -0500282 eid = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000283 BOOST_CHECK_EQUAL(static_cast<bool>(eid), true);
284 BOOST_CHECK_EQUAL(!eid, false);
285
286 EventId eid2 = eid;
287 scheduler.cancelEvent(eid2);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400288 BOOST_CHECK(!eid);
289 BOOST_CHECK(!eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000290}
291
292BOOST_AUTO_TEST_CASE(DuringCallback)
293{
294 EventId eid;
Davide Pesavento0f830802018-01-16 23:58:58 -0500295 EventId eid2 = scheduler.scheduleEvent(20_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000296
297 bool isCallbackInvoked = false;
Davide Pesavento0f830802018-01-16 23:58:58 -0500298 eid = scheduler.scheduleEvent(10_ms, [this, &eid, &eid2, &isCallbackInvoked] {
Junxiao Shid50f2b42016-08-10 02:59:59 +0000299 isCallbackInvoked = true;
300
301 // eid is "expired" during callback execution
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400302 BOOST_CHECK(!eid);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400303 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000304
305 scheduler.cancelEvent(eid2);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400306 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000307 });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400308
Davide Pesavento0f830802018-01-16 23:58:58 -0500309 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000310 BOOST_CHECK(isCallbackInvoked);
311}
312
313BOOST_AUTO_TEST_CASE(Reset)
314{
315 bool isCallbackInvoked = false;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400316 EventId eid = scheduler.scheduleEvent(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shid50f2b42016-08-10 02:59:59 +0000317 eid.reset();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400318 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000319
Davide Pesavento0f830802018-01-16 23:58:58 -0500320 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000321 BOOST_CHECK(isCallbackInvoked);
322}
323
324BOOST_AUTO_TEST_CASE(ToString)
325{
326 std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>());
327
328 EventId eid;
329 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString);
330
Davide Pesavento0f830802018-01-16 23:58:58 -0500331 eid = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000332 BOOST_TEST_MESSAGE("eid=" << eid);
333 BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString);
334}
335
336BOOST_AUTO_TEST_SUITE_END() // EventId
337
338BOOST_AUTO_TEST_SUITE(ScopedEventId)
339
340using scheduler::ScopedEventId;
341
342BOOST_AUTO_TEST_CASE(Destruct)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800343{
344 int hit = 0;
345 {
Junxiao Shi07115cc2019-01-23 19:00:59 +0000346 ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800347 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500348 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800349 BOOST_CHECK_EQUAL(hit, 0);
350}
351
Junxiao Shid50f2b42016-08-10 02:59:59 +0000352BOOST_AUTO_TEST_CASE(Assign)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800353{
354 int hit1 = 0, hit2 = 0;
Junxiao Shi07115cc2019-01-23 19:00:59 +0000355 ScopedEventId se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit1; });
Davide Pesavento0f830802018-01-16 23:58:58 -0500356 se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit2; });
357 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800358 BOOST_CHECK_EQUAL(hit1, 0);
359 BOOST_CHECK_EQUAL(hit2, 1);
360}
361
Junxiao Shid50f2b42016-08-10 02:59:59 +0000362BOOST_AUTO_TEST_CASE(Release)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800363{
364 int hit = 0;
365 {
Junxiao Shi07115cc2019-01-23 19:00:59 +0000366 ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800367 se.release();
368 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500369 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800370 BOOST_CHECK_EQUAL(hit, 1);
371}
372
Junxiao Shid50f2b42016-08-10 02:59:59 +0000373BOOST_AUTO_TEST_CASE(Move)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800374{
375 int hit = 0;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400376 unique_ptr<ScopedEventId> se2;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800377 {
Junxiao Shi07115cc2019-01-23 19:00:59 +0000378 ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400379 se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800380 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500381 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800382 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400383
Junxiao Shi07115cc2019-01-23 19:00:59 +0000384 ScopedEventId se3;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400385 {
Junxiao Shi07115cc2019-01-23 19:00:59 +0000386 ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400387 se3 = std::move(se); // move assignment
388 } // se goes out of scope
389 this->advanceClocks(1_ms, 15);
390 BOOST_CHECK_EQUAL(hit, 2);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800391}
392
393BOOST_AUTO_TEST_SUITE_END() // ScopedEventId
394
Junxiao Shid50f2b42016-08-10 02:59:59 +0000395BOOST_AUTO_TEST_SUITE_END() // TestScheduler
396BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800397
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800398} // namespace tests
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800399} // namespace scheduler
400} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800401} // namespace ndn