blob: 64971368b85ad4ab633941d0fc22c0a9efc871f2 [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
Davide Pesaventofd612312019-03-15 18:45:46 -040034class SchedulerFixture : public ndn::tests::UnitTestTimeFixture
Junxiao Shid50f2b42016-08-10 02:59:59 +000035{
36public:
37 SchedulerFixture()
38 : scheduler(io)
39 {
40 }
41
42public:
43 Scheduler scheduler;
44};
45
46BOOST_AUTO_TEST_SUITE(Util)
47BOOST_FIXTURE_TEST_SUITE(TestScheduler, SchedulerFixture)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080048
Davide Pesaventoeee3e822016-11-26 19:19:34 +010049BOOST_AUTO_TEST_SUITE(General)
50
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080051BOOST_AUTO_TEST_CASE(Events)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080052{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080053 size_t count1 = 0;
54 size_t count2 = 0;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080055
Junxiao Shia5f233e2019-03-18 09:39:22 -060056 scheduler.schedule(500_ms, [&] {
57 ++count1;
58 BOOST_CHECK_EQUAL(count2, 1);
59 });
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060
Junxiao Shia5f233e2019-03-18 09:39:22 -060061 EventId i = scheduler.schedule(1_s, [] { BOOST_ERROR("This event should not have been fired"); });
62 i.cancel();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080063
Junxiao Shia5f233e2019-03-18 09:39:22 -060064 scheduler.schedule(250_ms, [&] {
65 BOOST_CHECK_EQUAL(count1, 0);
66 ++count2;
67 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068
Junxiao Shia5f233e2019-03-18 09:39:22 -060069 i = scheduler.schedule(50_ms, [&] { BOOST_ERROR("This event should not have been fired"); });
70 i.cancel();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080071
Davide Pesavento0f830802018-01-16 23:58:58 -050072 advanceClocks(25_ms, 1000_ms);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080073 BOOST_CHECK_EQUAL(count1, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080074 BOOST_CHECK_EQUAL(count2, 1);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075}
76
Junxiao Shi86dfa532016-08-10 03:00:11 +000077BOOST_AUTO_TEST_CASE(CallbackException)
78{
79 class MyException : public std::exception
80 {
81 };
Junxiao Shia5f233e2019-03-18 09:39:22 -060082 scheduler.schedule(10_ms, [] {
Davide Pesavento923ba442019-02-12 22:00:38 -050083 // use plain 'throw' to ensure that Scheduler does not depend on the
84 // internal machinery of NDN_THROW and that it can catch all exceptions
85 // regardless of how they are thrown by the application
86 throw MyException{};
87 });
Junxiao Shi86dfa532016-08-10 03:00:11 +000088
89 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -060090 scheduler.schedule(20_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shi86dfa532016-08-10 03:00:11 +000091
Davide Pesavento0f830802018-01-16 23:58:58 -050092 BOOST_CHECK_THROW(this->advanceClocks(6_ms, 2), MyException);
93 this->advanceClocks(6_ms, 2);
Junxiao Shi86dfa532016-08-10 03:00:11 +000094 BOOST_CHECK(isCallbackInvoked);
95}
96
Yingdi Yuf2a82092014-02-03 16:49:15 -080097BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
98{
Yingdi Yuf2a82092014-02-03 16:49:15 -080099 EventId i;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600100 i.cancel();
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100101
102 // avoid "test case [...] did not check any assertions" message from Boost.Test
103 BOOST_CHECK(true);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800104}
105
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800106BOOST_AUTO_TEST_CASE(SelfCancel)
Yingdi Yuf2a82092014-02-03 16:49:15 -0800107{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800108 EventId selfEventId;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600109 selfEventId = scheduler.schedule(100_ms, [&] { selfEventId.cancel(); });
Davide Pesavento0f830802018-01-16 23:58:58 -0500110 BOOST_REQUIRE_NO_THROW(advanceClocks(100_ms, 10));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800111}
112
Junxiao Shid50f2b42016-08-10 02:59:59 +0000113class SelfRescheduleFixture : public SchedulerFixture
Yingdi Yuf2a82092014-02-03 16:49:15 -0800114{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800115public:
Yingdi Yuf2a82092014-02-03 16:49:15 -0800116 void
117 reschedule()
118 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600119 EventId eventId = scheduler.schedule(100_ms, bind(&SelfRescheduleFixture::reschedule, this));
120 selfEventId.cancel();
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800121 selfEventId = eventId;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800122
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800123 if (count < 5)
124 count++;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800125 else
Junxiao Shia5f233e2019-03-18 09:39:22 -0600126 selfEventId.cancel();
Yingdi Yuf2a82092014-02-03 16:49:15 -0800127 }
128
129 void
130 reschedule2()
131 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600132 selfEventId.cancel();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700133
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800134 if (count < 5) {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600135 selfEventId = scheduler.schedule(100_ms, bind(&SelfRescheduleFixture::reschedule2, this));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800136 count++;
137 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800138 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700139
Yingdi Yuf2a82092014-02-03 16:49:15 -0800140 void
141 reschedule3()
142 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600143 selfEventId.cancel();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144
Junxiao Shia5f233e2019-03-18 09:39:22 -0600145 scheduler.schedule(100_ms, [&] { ++count; });
146 scheduler.schedule(100_ms, [&] { ++count; });
147 scheduler.schedule(100_ms, [&] { ++count; });
148 scheduler.schedule(100_ms, [&] { ++count; });
149 scheduler.schedule(100_ms, [&] { ++count; });
150 scheduler.schedule(100_ms, [&] { ++count; });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800151 }
152
Junxiao Shid50f2b42016-08-10 02:59:59 +0000153public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800154 EventId selfEventId;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600155 size_t count = 0;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800156};
157
158BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
159{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600160 selfEventId = scheduler.schedule(0_s, bind(&SelfRescheduleFixture::reschedule, this));
Davide Pesavento0f830802018-01-16 23:58:58 -0500161 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800162 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800163}
164
165BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
166{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600167 selfEventId = scheduler.schedule(0_s, bind(&SelfRescheduleFixture::reschedule2, this));
Davide Pesavento0f830802018-01-16 23:58:58 -0500168 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800169 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800170}
171
172BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
173{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600174 selfEventId = scheduler.schedule(0_s, bind(&SelfRescheduleFixture::reschedule3, this));
Davide Pesavento0f830802018-01-16 23:58:58 -0500175 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800176 BOOST_CHECK_EQUAL(count, 6);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800177}
178
Junxiao Shid50f2b42016-08-10 02:59:59 +0000179class CancelAllFixture : public SchedulerFixture
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700180{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000181public:
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700182 void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700183 event()
184 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800185 ++count;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600186 scheduler.schedule(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700187 }
188
Junxiao Shid50f2b42016-08-10 02:59:59 +0000189public:
Junxiao Shia5f233e2019-03-18 09:39:22 -0600190 uint32_t count = 0;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700191};
192
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700193BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
194{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600195 scheduler.schedule(500_ms, [&] { scheduler.cancelAllEvents(); });
196 scheduler.schedule(1_s, [&] { event(); });
197 scheduler.schedule(3_s, [] { BOOST_ERROR("This event should have been cancelled" ); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700198
Davide Pesavento0f830802018-01-16 23:58:58 -0500199 advanceClocks(100_ms, 100);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800200 BOOST_CHECK_EQUAL(count, 0);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700201}
202
Junxiao Shid50f2b42016-08-10 02:59:59 +0000203BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800204{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000205 Scheduler sched(io);
Junxiao Shia5f233e2019-03-18 09:39:22 -0600206 ScopedEventId eid = sched.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000207 sched.cancelAllEvents();
208 eid.cancel(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100209
210 // avoid "test case [...] did not check any assertions" message from Boost.Test
211 BOOST_CHECK(true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000212}
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700213
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100214BOOST_AUTO_TEST_SUITE_END() // General
215
Junxiao Shid50f2b42016-08-10 02:59:59 +0000216BOOST_AUTO_TEST_SUITE(EventId)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800217
Junxiao Shid50f2b42016-08-10 02:59:59 +0000218using scheduler::EventId;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800219
Junxiao Shid50f2b42016-08-10 02:59:59 +0000220BOOST_AUTO_TEST_CASE(ConstructEmpty)
221{
222 EventId eid;
Junxiao Shi07115cc2019-01-23 19:00:59 +0000223 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000224}
225
226BOOST_AUTO_TEST_CASE(Compare)
227{
228 EventId eid, eid2;
229 BOOST_CHECK_EQUAL(eid == eid2, true);
230 BOOST_CHECK_EQUAL(eid != eid2, false);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000231
Junxiao Shia5f233e2019-03-18 09:39:22 -0600232 eid = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000233 BOOST_CHECK_EQUAL(eid == eid2, false);
234 BOOST_CHECK_EQUAL(eid != eid2, true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000235
236 eid2 = eid;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400237 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000238 BOOST_CHECK_EQUAL(eid != eid2, false);
239
Junxiao Shia5f233e2019-03-18 09:39:22 -0600240 eid2 = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000241 BOOST_CHECK_EQUAL(eid == eid2, false);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400242 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000243}
244
245BOOST_AUTO_TEST_CASE(Valid)
246{
247 EventId eid;
248 BOOST_CHECK_EQUAL(static_cast<bool>(eid), false);
249 BOOST_CHECK_EQUAL(!eid, true);
250
Junxiao Shia5f233e2019-03-18 09:39:22 -0600251 eid = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000252 BOOST_CHECK_EQUAL(static_cast<bool>(eid), true);
253 BOOST_CHECK_EQUAL(!eid, false);
254
255 EventId eid2 = eid;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600256 eid2.cancel();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400257 BOOST_CHECK(!eid);
258 BOOST_CHECK(!eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000259}
260
261BOOST_AUTO_TEST_CASE(DuringCallback)
262{
263 EventId eid;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600264 EventId eid2 = scheduler.schedule(20_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000265
266 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600267 eid = scheduler.schedule(10_ms, [&eid, &eid2, &isCallbackInvoked] {
Junxiao Shid50f2b42016-08-10 02:59:59 +0000268 isCallbackInvoked = true;
269
270 // eid is "expired" during callback execution
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400271 BOOST_CHECK(!eid);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400272 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000273
Junxiao Shia5f233e2019-03-18 09:39:22 -0600274 eid2.cancel();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400275 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000276 });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400277
Davide Pesavento0f830802018-01-16 23:58:58 -0500278 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000279 BOOST_CHECK(isCallbackInvoked);
280}
281
282BOOST_AUTO_TEST_CASE(Reset)
283{
284 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600285 EventId eid = scheduler.schedule(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shid50f2b42016-08-10 02:59:59 +0000286 eid.reset();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400287 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000288
Davide Pesavento0f830802018-01-16 23:58:58 -0500289 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000290 BOOST_CHECK(isCallbackInvoked);
291}
292
293BOOST_AUTO_TEST_CASE(ToString)
294{
295 std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>());
296
297 EventId eid;
298 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString);
299
Junxiao Shia5f233e2019-03-18 09:39:22 -0600300 eid = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000301 BOOST_TEST_MESSAGE("eid=" << eid);
302 BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString);
303}
304
305BOOST_AUTO_TEST_SUITE_END() // EventId
306
307BOOST_AUTO_TEST_SUITE(ScopedEventId)
308
309using scheduler::ScopedEventId;
310
311BOOST_AUTO_TEST_CASE(Destruct)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800312{
313 int hit = 0;
314 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600315 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800316 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500317 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800318 BOOST_CHECK_EQUAL(hit, 0);
319}
320
Junxiao Shid50f2b42016-08-10 02:59:59 +0000321BOOST_AUTO_TEST_CASE(Assign)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800322{
323 int hit1 = 0, hit2 = 0;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600324 ScopedEventId se1 = scheduler.schedule(10_ms, [&] { ++hit1; });
325 se1 = scheduler.schedule(10_ms, [&] { ++hit2; });
Davide Pesavento0f830802018-01-16 23:58:58 -0500326 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800327 BOOST_CHECK_EQUAL(hit1, 0);
328 BOOST_CHECK_EQUAL(hit2, 1);
329}
330
Junxiao Shid50f2b42016-08-10 02:59:59 +0000331BOOST_AUTO_TEST_CASE(Release)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800332{
333 int hit = 0;
334 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600335 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800336 se.release();
337 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500338 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800339 BOOST_CHECK_EQUAL(hit, 1);
340}
341
Junxiao Shid50f2b42016-08-10 02:59:59 +0000342BOOST_AUTO_TEST_CASE(Move)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800343{
344 int hit = 0;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400345 unique_ptr<ScopedEventId> se2;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800346 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600347 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400348 se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800349 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500350 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800351 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400352
Junxiao Shi07115cc2019-01-23 19:00:59 +0000353 ScopedEventId se3;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400354 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600355 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400356 se3 = std::move(se); // move assignment
357 } // se goes out of scope
358 this->advanceClocks(1_ms, 15);
359 BOOST_CHECK_EQUAL(hit, 2);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800360}
361
362BOOST_AUTO_TEST_SUITE_END() // ScopedEventId
363
Junxiao Shid50f2b42016-08-10 02:59:59 +0000364BOOST_AUTO_TEST_SUITE_END() // TestScheduler
365BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800366
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800367} // namespace tests
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800368} // namespace scheduler
369} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800370} // namespace ndn