blob: 021c1f0ac3f905c46ad47caf68d90f6bc74ee237 [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 scheduler {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080031namespace tests {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080032
Davide Pesaventofd612312019-03-15 18:45:46 -040033class SchedulerFixture : public ndn::tests::UnitTestTimeFixture
Junxiao Shid50f2b42016-08-10 02:59:59 +000034{
35public:
36 SchedulerFixture()
37 : scheduler(io)
38 {
39 }
40
41public:
42 Scheduler scheduler;
43};
44
45BOOST_AUTO_TEST_SUITE(Util)
46BOOST_FIXTURE_TEST_SUITE(TestScheduler, SchedulerFixture)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080047
Davide Pesaventoeee3e822016-11-26 19:19:34 +010048BOOST_AUTO_TEST_SUITE(General)
49
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080050BOOST_AUTO_TEST_CASE(Events)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080051{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080052 size_t count1 = 0;
53 size_t count2 = 0;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080054
Junxiao Shia5f233e2019-03-18 09:39:22 -060055 scheduler.schedule(500_ms, [&] {
56 ++count1;
57 BOOST_CHECK_EQUAL(count2, 1);
58 });
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070059
Junxiao Shia5f233e2019-03-18 09:39:22 -060060 EventId i = scheduler.schedule(1_s, [] { BOOST_ERROR("This event should not have been fired"); });
61 i.cancel();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080062
Junxiao Shia5f233e2019-03-18 09:39:22 -060063 scheduler.schedule(250_ms, [&] {
64 BOOST_CHECK_EQUAL(count1, 0);
65 ++count2;
66 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080067
Junxiao Shia5f233e2019-03-18 09:39:22 -060068 i = scheduler.schedule(50_ms, [&] { BOOST_ERROR("This event should not have been fired"); });
69 i.cancel();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080070
Davide Pesavento0f830802018-01-16 23:58:58 -050071 advanceClocks(25_ms, 1000_ms);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080072 BOOST_CHECK_EQUAL(count1, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080073 BOOST_CHECK_EQUAL(count2, 1);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080074}
75
Junxiao Shi86dfa532016-08-10 03:00:11 +000076BOOST_AUTO_TEST_CASE(CallbackException)
77{
78 class MyException : public std::exception
79 {
80 };
Junxiao Shia5f233e2019-03-18 09:39:22 -060081 scheduler.schedule(10_ms, [] {
Davide Pesavento923ba442019-02-12 22:00:38 -050082 // use plain 'throw' to ensure that Scheduler does not depend on the
83 // internal machinery of NDN_THROW and that it can catch all exceptions
84 // regardless of how they are thrown by the application
85 throw MyException{};
86 });
Junxiao Shi86dfa532016-08-10 03:00:11 +000087
88 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -060089 scheduler.schedule(20_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shi86dfa532016-08-10 03:00:11 +000090
Davide Pesavento0f830802018-01-16 23:58:58 -050091 BOOST_CHECK_THROW(this->advanceClocks(6_ms, 2), MyException);
92 this->advanceClocks(6_ms, 2);
Junxiao Shi86dfa532016-08-10 03:00:11 +000093 BOOST_CHECK(isCallbackInvoked);
94}
95
Yingdi Yuf2a82092014-02-03 16:49:15 -080096BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
97{
Yingdi Yuf2a82092014-02-03 16:49:15 -080098 EventId i;
Junxiao Shia5f233e2019-03-18 09:39:22 -060099 i.cancel();
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100100
101 // avoid "test case [...] did not check any assertions" message from Boost.Test
102 BOOST_CHECK(true);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800103}
104
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800105BOOST_AUTO_TEST_CASE(SelfCancel)
Yingdi Yuf2a82092014-02-03 16:49:15 -0800106{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800107 EventId selfEventId;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600108 selfEventId = scheduler.schedule(100_ms, [&] { selfEventId.cancel(); });
Davide Pesavento0f830802018-01-16 23:58:58 -0500109 BOOST_REQUIRE_NO_THROW(advanceClocks(100_ms, 10));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800110}
111
Junxiao Shid50f2b42016-08-10 02:59:59 +0000112class SelfRescheduleFixture : public SchedulerFixture
Yingdi Yuf2a82092014-02-03 16:49:15 -0800113{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800114public:
Yingdi Yuf2a82092014-02-03 16:49:15 -0800115 void
116 reschedule()
117 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600118 EventId eventId = scheduler.schedule(100_ms, bind(&SelfRescheduleFixture::reschedule, this));
119 selfEventId.cancel();
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800120 selfEventId = eventId;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800121
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800122 if (count < 5)
123 count++;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800124 else
Junxiao Shia5f233e2019-03-18 09:39:22 -0600125 selfEventId.cancel();
Yingdi Yuf2a82092014-02-03 16:49:15 -0800126 }
127
128 void
129 reschedule2()
130 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600131 selfEventId.cancel();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700132
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800133 if (count < 5) {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600134 selfEventId = scheduler.schedule(100_ms, bind(&SelfRescheduleFixture::reschedule2, this));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800135 count++;
136 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800137 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700138
Yingdi Yuf2a82092014-02-03 16:49:15 -0800139 void
140 reschedule3()
141 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600142 selfEventId.cancel();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700143
Junxiao Shia5f233e2019-03-18 09:39:22 -0600144 scheduler.schedule(100_ms, [&] { ++count; });
145 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; });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800150 }
151
Junxiao Shid50f2b42016-08-10 02:59:59 +0000152public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800153 EventId selfEventId;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600154 size_t count = 0;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800155};
156
157BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
158{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600159 selfEventId = scheduler.schedule(0_s, bind(&SelfRescheduleFixture::reschedule, this));
Davide Pesavento0f830802018-01-16 23:58:58 -0500160 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800161 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800162}
163
164BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
165{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600166 selfEventId = scheduler.schedule(0_s, bind(&SelfRescheduleFixture::reschedule2, this));
Davide Pesavento0f830802018-01-16 23:58:58 -0500167 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800168 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800169}
170
171BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
172{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600173 selfEventId = scheduler.schedule(0_s, bind(&SelfRescheduleFixture::reschedule3, this));
Davide Pesavento0f830802018-01-16 23:58:58 -0500174 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800175 BOOST_CHECK_EQUAL(count, 6);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800176}
177
Junxiao Shid50f2b42016-08-10 02:59:59 +0000178class CancelAllFixture : public SchedulerFixture
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700179{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000180public:
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700181 void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700182 event()
183 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800184 ++count;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600185 scheduler.schedule(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700186 }
187
Junxiao Shid50f2b42016-08-10 02:59:59 +0000188public:
Junxiao Shia5f233e2019-03-18 09:39:22 -0600189 uint32_t count = 0;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700190};
191
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700192BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
193{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600194 scheduler.schedule(500_ms, [&] { scheduler.cancelAllEvents(); });
195 scheduler.schedule(1_s, [&] { event(); });
196 scheduler.schedule(3_s, [] { BOOST_ERROR("This event should have been cancelled" ); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700197
Davide Pesavento0f830802018-01-16 23:58:58 -0500198 advanceClocks(100_ms, 100);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800199 BOOST_CHECK_EQUAL(count, 0);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700200}
201
Junxiao Shid50f2b42016-08-10 02:59:59 +0000202BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800203{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000204 Scheduler sched(io);
Junxiao Shia5f233e2019-03-18 09:39:22 -0600205 ScopedEventId eid = sched.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000206 sched.cancelAllEvents();
207 eid.cancel(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100208
209 // avoid "test case [...] did not check any assertions" message from Boost.Test
210 BOOST_CHECK(true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000211}
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700212
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100213BOOST_AUTO_TEST_SUITE_END() // General
214
Junxiao Shid50f2b42016-08-10 02:59:59 +0000215BOOST_AUTO_TEST_SUITE(EventId)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800216
Junxiao Shid50f2b42016-08-10 02:59:59 +0000217using scheduler::EventId;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800218
Junxiao Shid50f2b42016-08-10 02:59:59 +0000219BOOST_AUTO_TEST_CASE(ConstructEmpty)
220{
221 EventId eid;
Junxiao Shi07115cc2019-01-23 19:00:59 +0000222 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000223}
224
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400225BOOST_AUTO_TEST_CASE(Equality)
Junxiao Shid50f2b42016-08-10 02:59:59 +0000226{
227 EventId eid, eid2;
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400228 BOOST_CHECK(eid == eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000229
Junxiao Shia5f233e2019-03-18 09:39:22 -0600230 eid = scheduler.schedule(10_ms, []{});
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400231 BOOST_CHECK(eid != eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000232
Junxiao Shia5f233e2019-03-18 09:39:22 -0600233 eid2 = scheduler.schedule(10_ms, []{});
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400234 BOOST_CHECK(eid != eid2);
235
236 eid.cancel();
237 BOOST_CHECK(eid != eid2);
238 BOOST_CHECK(eid == EventId{});
239
240 eid2.cancel();
241 BOOST_CHECK(eid == eid2);
242
243 eid = eid2 = scheduler.schedule(20_ms, []{});
244 BOOST_CHECK(eid == eid2);
245 BOOST_CHECK(eid != EventId{});
246
247 eid.cancel();
248 BOOST_CHECK(eid == eid2);
249 BOOST_CHECK(eid == EventId{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000250}
251
Davide Pesavento720e25c2019-07-14 01:33:52 -0400252BOOST_AUTO_TEST_CASE(OperatorBool)
Junxiao Shid50f2b42016-08-10 02:59:59 +0000253{
254 EventId eid;
255 BOOST_CHECK_EQUAL(static_cast<bool>(eid), false);
256 BOOST_CHECK_EQUAL(!eid, true);
257
Junxiao Shia5f233e2019-03-18 09:39:22 -0600258 eid = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000259 BOOST_CHECK_EQUAL(static_cast<bool>(eid), true);
260 BOOST_CHECK_EQUAL(!eid, false);
261
262 EventId eid2 = eid;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600263 eid2.cancel();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400264 BOOST_CHECK(!eid);
265 BOOST_CHECK(!eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000266}
267
268BOOST_AUTO_TEST_CASE(DuringCallback)
269{
270 EventId eid;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600271 EventId eid2 = scheduler.schedule(20_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000272
273 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600274 eid = scheduler.schedule(10_ms, [&eid, &eid2, &isCallbackInvoked] {
Junxiao Shid50f2b42016-08-10 02:59:59 +0000275 isCallbackInvoked = true;
276
277 // eid is "expired" during callback execution
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400278 BOOST_CHECK(!eid);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400279 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000280
Junxiao Shia5f233e2019-03-18 09:39:22 -0600281 eid2.cancel();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400282 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000283 });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400284
Davide Pesavento0f830802018-01-16 23:58:58 -0500285 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000286 BOOST_CHECK(isCallbackInvoked);
287}
288
289BOOST_AUTO_TEST_CASE(Reset)
290{
291 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600292 EventId eid = scheduler.schedule(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shid50f2b42016-08-10 02:59:59 +0000293 eid.reset();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400294 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000295
Davide Pesavento0f830802018-01-16 23:58:58 -0500296 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000297 BOOST_CHECK(isCallbackInvoked);
298}
299
300BOOST_AUTO_TEST_CASE(ToString)
301{
302 std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>());
303
304 EventId eid;
305 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString);
306
Junxiao Shia5f233e2019-03-18 09:39:22 -0600307 eid = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000308 BOOST_TEST_MESSAGE("eid=" << eid);
309 BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString);
310}
311
312BOOST_AUTO_TEST_SUITE_END() // EventId
313
314BOOST_AUTO_TEST_SUITE(ScopedEventId)
315
316using scheduler::ScopedEventId;
317
318BOOST_AUTO_TEST_CASE(Destruct)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800319{
320 int hit = 0;
321 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600322 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800323 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500324 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800325 BOOST_CHECK_EQUAL(hit, 0);
326}
327
Junxiao Shid50f2b42016-08-10 02:59:59 +0000328BOOST_AUTO_TEST_CASE(Assign)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800329{
330 int hit1 = 0, hit2 = 0;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600331 ScopedEventId se1 = scheduler.schedule(10_ms, [&] { ++hit1; });
332 se1 = scheduler.schedule(10_ms, [&] { ++hit2; });
Davide Pesavento0f830802018-01-16 23:58:58 -0500333 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800334 BOOST_CHECK_EQUAL(hit1, 0);
335 BOOST_CHECK_EQUAL(hit2, 1);
336}
337
Junxiao Shid50f2b42016-08-10 02:59:59 +0000338BOOST_AUTO_TEST_CASE(Release)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800339{
340 int hit = 0;
341 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600342 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800343 se.release();
344 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500345 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800346 BOOST_CHECK_EQUAL(hit, 1);
347}
348
Junxiao Shid50f2b42016-08-10 02:59:59 +0000349BOOST_AUTO_TEST_CASE(Move)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800350{
351 int hit = 0;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400352 unique_ptr<ScopedEventId> se2;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800353 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600354 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400355 se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800356 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500357 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800358 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400359
Junxiao Shi07115cc2019-01-23 19:00:59 +0000360 ScopedEventId se3;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400361 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600362 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400363 se3 = std::move(se); // move assignment
364 } // se goes out of scope
365 this->advanceClocks(1_ms, 15);
366 BOOST_CHECK_EQUAL(hit, 2);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800367}
368
Davide Pesavento720e25c2019-07-14 01:33:52 -0400369BOOST_AUTO_TEST_CASE(OperatorBool)
370{
371 ScopedEventId se;
372 BOOST_CHECK_EQUAL(static_cast<bool>(se), false);
373 BOOST_CHECK_EQUAL(!se, true);
374
375 se = scheduler.schedule(10_ms, []{});
376 BOOST_CHECK_EQUAL(static_cast<bool>(se), true);
377 BOOST_CHECK_EQUAL(!se, false);
378
379 se.cancel();
380 BOOST_CHECK(!se);
381
382 se = scheduler.schedule(10_ms, []{});
383 BOOST_CHECK(se);
384
385 se.release();
386 BOOST_CHECK(!se);
387}
388
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800389BOOST_AUTO_TEST_SUITE_END() // ScopedEventId
390
Junxiao Shid50f2b42016-08-10 02:59:59 +0000391BOOST_AUTO_TEST_SUITE_END() // TestScheduler
392BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800393
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800394} // namespace tests
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800395} // namespace scheduler
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800396} // namespace ndn