blob: 1040b92b40e548390d4d59689f1ce57d2a12822a [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/*
Davide Pesavento2e481fc2021-07-02 18:20:03 -04003 * Copyright (c) 2013-2021 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"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/unit/io-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 Pesavento4c1ad4c2020-11-16 21:12:02 -050033class SchedulerFixture : public ndn::tests::IoFixture
Junxiao Shid50f2b42016-08-10 02:59:59 +000034{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050035protected:
36 Scheduler scheduler{m_io};
Junxiao Shid50f2b42016-08-10 02:59:59 +000037};
38
39BOOST_AUTO_TEST_SUITE(Util)
40BOOST_FIXTURE_TEST_SUITE(TestScheduler, SchedulerFixture)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080041
Davide Pesaventoeee3e822016-11-26 19:19:34 +010042BOOST_AUTO_TEST_SUITE(General)
43
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080044BOOST_AUTO_TEST_CASE(Events)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080045{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080046 size_t count1 = 0;
47 size_t count2 = 0;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080048
Junxiao Shia5f233e2019-03-18 09:39:22 -060049 scheduler.schedule(500_ms, [&] {
50 ++count1;
51 BOOST_CHECK_EQUAL(count2, 1);
52 });
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053
Junxiao Shia5f233e2019-03-18 09:39:22 -060054 EventId i = scheduler.schedule(1_s, [] { BOOST_ERROR("This event should not have been fired"); });
55 i.cancel();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080056
Junxiao Shia5f233e2019-03-18 09:39:22 -060057 scheduler.schedule(250_ms, [&] {
58 BOOST_CHECK_EQUAL(count1, 0);
59 ++count2;
60 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080061
Junxiao Shia5f233e2019-03-18 09:39:22 -060062 i = scheduler.schedule(50_ms, [&] { BOOST_ERROR("This event should not have been fired"); });
63 i.cancel();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080064
Davide Pesavento0f830802018-01-16 23:58:58 -050065 advanceClocks(25_ms, 1000_ms);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066 BOOST_CHECK_EQUAL(count1, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080067 BOOST_CHECK_EQUAL(count2, 1);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068}
69
Junxiao Shi86dfa532016-08-10 03:00:11 +000070BOOST_AUTO_TEST_CASE(CallbackException)
71{
72 class MyException : public std::exception
73 {
74 };
Junxiao Shia5f233e2019-03-18 09:39:22 -060075 scheduler.schedule(10_ms, [] {
Davide Pesavento923ba442019-02-12 22:00:38 -050076 // use plain 'throw' to ensure that Scheduler does not depend on the
77 // internal machinery of NDN_THROW and that it can catch all exceptions
78 // regardless of how they are thrown by the application
79 throw MyException{};
80 });
Junxiao Shi86dfa532016-08-10 03:00:11 +000081
82 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -060083 scheduler.schedule(20_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shi86dfa532016-08-10 03:00:11 +000084
Davide Pesavento0f830802018-01-16 23:58:58 -050085 BOOST_CHECK_THROW(this->advanceClocks(6_ms, 2), MyException);
86 this->advanceClocks(6_ms, 2);
Junxiao Shi86dfa532016-08-10 03:00:11 +000087 BOOST_CHECK(isCallbackInvoked);
88}
89
Yingdi Yuf2a82092014-02-03 16:49:15 -080090BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
91{
Yingdi Yuf2a82092014-02-03 16:49:15 -080092 EventId i;
Junxiao Shia5f233e2019-03-18 09:39:22 -060093 i.cancel();
Davide Pesaventoeee3e822016-11-26 19:19:34 +010094
95 // avoid "test case [...] did not check any assertions" message from Boost.Test
96 BOOST_CHECK(true);
Yingdi Yuf2a82092014-02-03 16:49:15 -080097}
98
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080099BOOST_AUTO_TEST_CASE(SelfCancel)
Yingdi Yuf2a82092014-02-03 16:49:15 -0800100{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800101 EventId selfEventId;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600102 selfEventId = scheduler.schedule(100_ms, [&] { selfEventId.cancel(); });
Davide Pesavento0f830802018-01-16 23:58:58 -0500103 BOOST_REQUIRE_NO_THROW(advanceClocks(100_ms, 10));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800104}
105
Junxiao Shid50f2b42016-08-10 02:59:59 +0000106class SelfRescheduleFixture : public SchedulerFixture
Yingdi Yuf2a82092014-02-03 16:49:15 -0800107{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800108public:
Yingdi Yuf2a82092014-02-03 16:49:15 -0800109 void
110 reschedule()
111 {
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400112 EventId eventId = scheduler.schedule(100_ms, [this] { reschedule(); });
Junxiao Shia5f233e2019-03-18 09:39:22 -0600113 selfEventId.cancel();
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800114 selfEventId = eventId;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800115
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800116 if (count < 5)
117 count++;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800118 else
Junxiao Shia5f233e2019-03-18 09:39:22 -0600119 selfEventId.cancel();
Yingdi Yuf2a82092014-02-03 16:49:15 -0800120 }
121
122 void
123 reschedule2()
124 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600125 selfEventId.cancel();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700126
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800127 if (count < 5) {
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400128 selfEventId = scheduler.schedule(100_ms, [this] { reschedule2(); });
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800129 count++;
130 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800131 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700132
Yingdi Yuf2a82092014-02-03 16:49:15 -0800133 void
134 reschedule3()
135 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600136 selfEventId.cancel();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700137
Junxiao Shia5f233e2019-03-18 09:39:22 -0600138 scheduler.schedule(100_ms, [&] { ++count; });
139 scheduler.schedule(100_ms, [&] { ++count; });
140 scheduler.schedule(100_ms, [&] { ++count; });
141 scheduler.schedule(100_ms, [&] { ++count; });
142 scheduler.schedule(100_ms, [&] { ++count; });
143 scheduler.schedule(100_ms, [&] { ++count; });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800144 }
145
Junxiao Shid50f2b42016-08-10 02:59:59 +0000146public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800147 EventId selfEventId;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600148 size_t count = 0;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800149};
150
151BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
152{
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400153 selfEventId = scheduler.schedule(0_s, [this] { reschedule(); });
Davide Pesavento0f830802018-01-16 23:58:58 -0500154 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800155 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800156}
157
158BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
159{
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400160 selfEventId = scheduler.schedule(0_s, [this] { reschedule2(); });
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(Reschedule3, SelfRescheduleFixture)
166{
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400167 selfEventId = scheduler.schedule(0_s, [this] { reschedule3(); });
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, 6);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800170}
171
Junxiao Shid50f2b42016-08-10 02:59:59 +0000172class CancelAllFixture : public SchedulerFixture
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700173{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000174public:
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700175 void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700176 event()
177 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800178 ++count;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600179 scheduler.schedule(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700180 }
181
Junxiao Shid50f2b42016-08-10 02:59:59 +0000182public:
Junxiao Shia5f233e2019-03-18 09:39:22 -0600183 uint32_t count = 0;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700184};
185
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700186BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
187{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600188 scheduler.schedule(500_ms, [&] { scheduler.cancelAllEvents(); });
189 scheduler.schedule(1_s, [&] { event(); });
190 scheduler.schedule(3_s, [] { BOOST_ERROR("This event should have been cancelled" ); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700191
Davide Pesavento0f830802018-01-16 23:58:58 -0500192 advanceClocks(100_ms, 100);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800193 BOOST_CHECK_EQUAL(count, 0);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700194}
195
Junxiao Shid50f2b42016-08-10 02:59:59 +0000196BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800197{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500198 ScopedEventId eid = scheduler.schedule(10_ms, []{});
199 scheduler.cancelAllEvents();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000200 eid.cancel(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100201
202 // avoid "test case [...] did not check any assertions" message from Boost.Test
203 BOOST_CHECK(true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000204}
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700205
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100206BOOST_AUTO_TEST_SUITE_END() // General
207
Junxiao Shid50f2b42016-08-10 02:59:59 +0000208BOOST_AUTO_TEST_SUITE(EventId)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800209
Junxiao Shid50f2b42016-08-10 02:59:59 +0000210using scheduler::EventId;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800211
Junxiao Shid50f2b42016-08-10 02:59:59 +0000212BOOST_AUTO_TEST_CASE(ConstructEmpty)
213{
214 EventId eid;
Junxiao Shi07115cc2019-01-23 19:00:59 +0000215 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000216}
217
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400218BOOST_AUTO_TEST_CASE(Equality)
Junxiao Shid50f2b42016-08-10 02:59:59 +0000219{
220 EventId eid, eid2;
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400221 BOOST_CHECK(eid == eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000222
Junxiao Shia5f233e2019-03-18 09:39:22 -0600223 eid = scheduler.schedule(10_ms, []{});
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400224 BOOST_CHECK(eid != eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000225
Junxiao Shia5f233e2019-03-18 09:39:22 -0600226 eid2 = scheduler.schedule(10_ms, []{});
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400227 BOOST_CHECK(eid != eid2);
228
229 eid.cancel();
230 BOOST_CHECK(eid != eid2);
231 BOOST_CHECK(eid == EventId{});
232
233 eid2.cancel();
234 BOOST_CHECK(eid == eid2);
235
236 eid = eid2 = scheduler.schedule(20_ms, []{});
237 BOOST_CHECK(eid == eid2);
238 BOOST_CHECK(eid != EventId{});
239
240 eid.cancel();
241 BOOST_CHECK(eid == eid2);
242 BOOST_CHECK(eid == EventId{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000243}
244
Davide Pesavento720e25c2019-07-14 01:33:52 -0400245BOOST_AUTO_TEST_CASE(OperatorBool)
Junxiao Shid50f2b42016-08-10 02:59:59 +0000246{
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_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString);
302}
303
304BOOST_AUTO_TEST_SUITE_END() // EventId
305
306BOOST_AUTO_TEST_SUITE(ScopedEventId)
307
308using scheduler::ScopedEventId;
309
310BOOST_AUTO_TEST_CASE(Destruct)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800311{
312 int hit = 0;
313 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600314 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800315 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500316 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800317 BOOST_CHECK_EQUAL(hit, 0);
318}
319
Junxiao Shid50f2b42016-08-10 02:59:59 +0000320BOOST_AUTO_TEST_CASE(Assign)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800321{
322 int hit1 = 0, hit2 = 0;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600323 ScopedEventId se1 = scheduler.schedule(10_ms, [&] { ++hit1; });
324 se1 = scheduler.schedule(10_ms, [&] { ++hit2; });
Davide Pesavento0f830802018-01-16 23:58:58 -0500325 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800326 BOOST_CHECK_EQUAL(hit1, 0);
327 BOOST_CHECK_EQUAL(hit2, 1);
328}
329
Junxiao Shid50f2b42016-08-10 02:59:59 +0000330BOOST_AUTO_TEST_CASE(Release)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800331{
332 int hit = 0;
333 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600334 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800335 se.release();
336 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500337 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800338 BOOST_CHECK_EQUAL(hit, 1);
339}
340
Junxiao Shid50f2b42016-08-10 02:59:59 +0000341BOOST_AUTO_TEST_CASE(Move)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800342{
343 int hit = 0;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400344 unique_ptr<ScopedEventId> se2;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800345 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600346 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400347 se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800348 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500349 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800350 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400351
Junxiao Shi07115cc2019-01-23 19:00:59 +0000352 ScopedEventId se3;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400353 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600354 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400355 se3 = std::move(se); // move assignment
356 } // se goes out of scope
357 this->advanceClocks(1_ms, 15);
358 BOOST_CHECK_EQUAL(hit, 2);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800359}
360
Davide Pesavento720e25c2019-07-14 01:33:52 -0400361BOOST_AUTO_TEST_CASE(OperatorBool)
362{
363 ScopedEventId se;
364 BOOST_CHECK_EQUAL(static_cast<bool>(se), false);
365 BOOST_CHECK_EQUAL(!se, true);
366
367 se = scheduler.schedule(10_ms, []{});
368 BOOST_CHECK_EQUAL(static_cast<bool>(se), true);
369 BOOST_CHECK_EQUAL(!se, false);
370
371 se.cancel();
372 BOOST_CHECK(!se);
373
374 se = scheduler.schedule(10_ms, []{});
375 BOOST_CHECK(se);
376
377 se.release();
378 BOOST_CHECK(!se);
379}
380
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800381BOOST_AUTO_TEST_SUITE_END() // ScopedEventId
382
Junxiao Shid50f2b42016-08-10 02:59:59 +0000383BOOST_AUTO_TEST_SUITE_END() // TestScheduler
384BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800385
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800386} // namespace tests
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800387} // namespace scheduler
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800388} // namespace ndn