blob: ec59c309adcca87063ed0455fc6d2aa991265904 [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 Pesavento0c526032024-01-31 21:14:01 -05003 * Copyright (c) 2013-2024 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080030
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031using namespace ndn::scheduler;
32
33class SchedulerFixture : public 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
Davide Pesavento2f46d652023-11-09 23:40:01 -050054 EventId eid = scheduler.schedule(1_s, [] { BOOST_ERROR("This event should not have been fired"); });
55 eid.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
Davide Pesavento2f46d652023-11-09 23:40:01 -050062 eid = scheduler.schedule(50_ms, [&] { BOOST_ERROR("This event should not have been fired"); });
63 eid.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
Davide Pesavento2f46d652023-11-09 23:40:01 -050070BOOST_AUTO_TEST_CASE(NegativeDelay)
71{
72 bool wasCallbackInvoked = false;
73 scheduler.schedule(-1_s, [&] { wasCallbackInvoked = true; });
74 advanceClocks(1_ns);
75 BOOST_CHECK(wasCallbackInvoked);
76}
77
78BOOST_AUTO_TEST_CASE(ThrowingCallback)
Junxiao Shi86dfa532016-08-10 03:00:11 +000079{
80 class MyException : public std::exception
81 {
82 };
Junxiao Shia5f233e2019-03-18 09:39:22 -060083 scheduler.schedule(10_ms, [] {
Davide Pesavento923ba442019-02-12 22:00:38 -050084 // use plain 'throw' to ensure that Scheduler does not depend on the
85 // internal machinery of NDN_THROW and that it can catch all exceptions
86 // regardless of how they are thrown by the application
87 throw MyException{};
88 });
Junxiao Shi86dfa532016-08-10 03:00:11 +000089
Davide Pesavento2f46d652023-11-09 23:40:01 -050090 bool wasCallbackInvoked = false;
91 scheduler.schedule(20_ms, [&] { wasCallbackInvoked = 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);
Davide Pesavento2f46d652023-11-09 23:40:01 -050095 BOOST_CHECK(wasCallbackInvoked);
Junxiao Shi86dfa532016-08-10 03:00:11 +000096}
97
Yingdi Yuf2a82092014-02-03 16:49:15 -080098BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
99{
Yingdi Yuf2a82092014-02-03 16:49:15 -0800100 EventId i;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600101 i.cancel();
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;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600110 selfEventId = scheduler.schedule(100_ms, [&] { selfEventId.cancel(); });
Davide Pesavento2f46d652023-11-09 23:40:01 -0500111 BOOST_CHECK_NO_THROW(advanceClocks(100_ms, 10));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800112}
113
Junxiao Shid50f2b42016-08-10 02:59:59 +0000114class SelfRescheduleFixture : public SchedulerFixture
Yingdi Yuf2a82092014-02-03 16:49:15 -0800115{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800116public:
Yingdi Yuf2a82092014-02-03 16:49:15 -0800117 void
118 reschedule()
119 {
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400120 EventId eventId = scheduler.schedule(100_ms, [this] { reschedule(); });
Junxiao Shia5f233e2019-03-18 09:39:22 -0600121 selfEventId.cancel();
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800122 selfEventId = eventId;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800123
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800124 if (count < 5)
125 count++;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800126 else
Junxiao Shia5f233e2019-03-18 09:39:22 -0600127 selfEventId.cancel();
Yingdi Yuf2a82092014-02-03 16:49:15 -0800128 }
129
130 void
131 reschedule2()
132 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600133 selfEventId.cancel();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700134
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800135 if (count < 5) {
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400136 selfEventId = scheduler.schedule(100_ms, [this] { reschedule2(); });
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800137 count++;
138 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800139 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700140
Yingdi Yuf2a82092014-02-03 16:49:15 -0800141 void
142 reschedule3()
143 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600144 selfEventId.cancel();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700145
Junxiao Shia5f233e2019-03-18 09:39:22 -0600146 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; });
151 scheduler.schedule(100_ms, [&] { ++count; });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800152 }
153
Davide Pesavento2f46d652023-11-09 23:40:01 -0500154protected:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800155 EventId selfEventId;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600156 size_t count = 0;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800157};
158
159BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
160{
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400161 selfEventId = scheduler.schedule(0_s, [this] { reschedule(); });
Davide Pesavento2f46d652023-11-09 23:40:01 -0500162 advanceClocks(50_ms, 1000_ms);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800163 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800164}
165
166BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
167{
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400168 selfEventId = scheduler.schedule(0_s, [this] { reschedule2(); });
Davide Pesavento2f46d652023-11-09 23:40:01 -0500169 advanceClocks(50_ms, 1000_ms);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800170 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800171}
172
173BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
174{
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400175 selfEventId = scheduler.schedule(0_s, [this] { reschedule3(); });
Davide Pesavento2f46d652023-11-09 23:40:01 -0500176 advanceClocks(50_ms, 1000_ms);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800177 BOOST_CHECK_EQUAL(count, 6);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800178}
179
Junxiao Shid50f2b42016-08-10 02:59:59 +0000180class CancelAllFixture : public SchedulerFixture
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700181{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000182public:
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700183 void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700184 event()
185 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800186 ++count;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600187 scheduler.schedule(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700188 }
189
Junxiao Shid50f2b42016-08-10 02:59:59 +0000190public:
Junxiao Shia5f233e2019-03-18 09:39:22 -0600191 uint32_t count = 0;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700192};
193
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700194BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
195{
Junxiao Shia5f233e2019-03-18 09:39:22 -0600196 scheduler.schedule(500_ms, [&] { scheduler.cancelAllEvents(); });
197 scheduler.schedule(1_s, [&] { event(); });
198 scheduler.schedule(3_s, [] { BOOST_ERROR("This event should have been cancelled" ); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700199
Davide Pesavento0f830802018-01-16 23:58:58 -0500200 advanceClocks(100_ms, 100);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800201 BOOST_CHECK_EQUAL(count, 0);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700202}
203
Davide Pesavento0c526032024-01-31 21:14:01 -0500204BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId,
205 * ut::description("test for bug #3691"))
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800206{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500207 ScopedEventId eid = scheduler.schedule(10_ms, []{});
208 scheduler.cancelAllEvents();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000209 eid.cancel(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100210
211 // avoid "test case [...] did not check any assertions" message from Boost.Test
212 BOOST_CHECK(true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000213}
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700214
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100215BOOST_AUTO_TEST_SUITE_END() // General
216
Junxiao Shid50f2b42016-08-10 02:59:59 +0000217BOOST_AUTO_TEST_SUITE(EventId)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800218
Junxiao Shid50f2b42016-08-10 02:59:59 +0000219using scheduler::EventId;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800220
Junxiao Shid50f2b42016-08-10 02:59:59 +0000221BOOST_AUTO_TEST_CASE(ConstructEmpty)
222{
223 EventId eid;
Junxiao Shi07115cc2019-01-23 19:00:59 +0000224 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000225}
226
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400227BOOST_AUTO_TEST_CASE(Equality)
Junxiao Shid50f2b42016-08-10 02:59:59 +0000228{
229 EventId eid, eid2;
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400230 BOOST_CHECK(eid == eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000231
Junxiao Shia5f233e2019-03-18 09:39:22 -0600232 eid = scheduler.schedule(10_ms, []{});
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400233 BOOST_CHECK(eid != eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000234
Junxiao Shia5f233e2019-03-18 09:39:22 -0600235 eid2 = scheduler.schedule(10_ms, []{});
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400236 BOOST_CHECK(eid != eid2);
237
238 eid.cancel();
239 BOOST_CHECK(eid != eid2);
240 BOOST_CHECK(eid == EventId{});
241
242 eid2.cancel();
243 BOOST_CHECK(eid == eid2);
244
245 eid = eid2 = scheduler.schedule(20_ms, []{});
246 BOOST_CHECK(eid == eid2);
247 BOOST_CHECK(eid != EventId{});
248
249 eid.cancel();
250 BOOST_CHECK(eid == eid2);
251 BOOST_CHECK(eid == EventId{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000252}
253
Davide Pesavento720e25c2019-07-14 01:33:52 -0400254BOOST_AUTO_TEST_CASE(OperatorBool)
Junxiao Shid50f2b42016-08-10 02:59:59 +0000255{
256 EventId eid;
257 BOOST_CHECK_EQUAL(static_cast<bool>(eid), false);
258 BOOST_CHECK_EQUAL(!eid, true);
259
Junxiao Shia5f233e2019-03-18 09:39:22 -0600260 eid = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000261 BOOST_CHECK_EQUAL(static_cast<bool>(eid), true);
262 BOOST_CHECK_EQUAL(!eid, false);
263
264 EventId eid2 = eid;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600265 eid2.cancel();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400266 BOOST_CHECK(!eid);
267 BOOST_CHECK(!eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000268}
269
270BOOST_AUTO_TEST_CASE(DuringCallback)
271{
272 EventId eid;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600273 EventId eid2 = scheduler.schedule(20_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000274
275 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600276 eid = scheduler.schedule(10_ms, [&eid, &eid2, &isCallbackInvoked] {
Junxiao Shid50f2b42016-08-10 02:59:59 +0000277 isCallbackInvoked = true;
278
279 // eid is "expired" during callback execution
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400280 BOOST_CHECK(!eid);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400281 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000282
Junxiao Shia5f233e2019-03-18 09:39:22 -0600283 eid2.cancel();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400284 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000285 });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400286
Davide Pesavento0f830802018-01-16 23:58:58 -0500287 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000288 BOOST_CHECK(isCallbackInvoked);
289}
290
291BOOST_AUTO_TEST_CASE(Reset)
292{
293 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600294 EventId eid = scheduler.schedule(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shid50f2b42016-08-10 02:59:59 +0000295 eid.reset();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400296 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000297
Davide Pesavento0f830802018-01-16 23:58:58 -0500298 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000299 BOOST_CHECK(isCallbackInvoked);
300}
301
302BOOST_AUTO_TEST_CASE(ToString)
303{
304 std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>());
305
306 EventId eid;
307 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString);
308
Junxiao Shia5f233e2019-03-18 09:39:22 -0600309 eid = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000310 BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString);
311}
312
313BOOST_AUTO_TEST_SUITE_END() // EventId
314
315BOOST_AUTO_TEST_SUITE(ScopedEventId)
316
317using scheduler::ScopedEventId;
318
319BOOST_AUTO_TEST_CASE(Destruct)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800320{
321 int hit = 0;
322 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600323 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800324 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500325 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800326 BOOST_CHECK_EQUAL(hit, 0);
327}
328
Junxiao Shid50f2b42016-08-10 02:59:59 +0000329BOOST_AUTO_TEST_CASE(Assign)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800330{
331 int hit1 = 0, hit2 = 0;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600332 ScopedEventId se1 = scheduler.schedule(10_ms, [&] { ++hit1; });
333 se1 = scheduler.schedule(10_ms, [&] { ++hit2; });
Davide Pesavento0f830802018-01-16 23:58:58 -0500334 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800335 BOOST_CHECK_EQUAL(hit1, 0);
336 BOOST_CHECK_EQUAL(hit2, 1);
337}
338
Junxiao Shid50f2b42016-08-10 02:59:59 +0000339BOOST_AUTO_TEST_CASE(Release)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800340{
341 int hit = 0;
342 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600343 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800344 se.release();
345 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500346 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800347 BOOST_CHECK_EQUAL(hit, 1);
348}
349
Junxiao Shid50f2b42016-08-10 02:59:59 +0000350BOOST_AUTO_TEST_CASE(Move)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800351{
352 int hit = 0;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400353 unique_ptr<ScopedEventId> se2;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800354 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600355 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400356 se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800357 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500358 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800359 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400360
Junxiao Shi07115cc2019-01-23 19:00:59 +0000361 ScopedEventId se3;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400362 {
Junxiao Shia5f233e2019-03-18 09:39:22 -0600363 ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400364 se3 = std::move(se); // move assignment
365 } // se goes out of scope
366 this->advanceClocks(1_ms, 15);
367 BOOST_CHECK_EQUAL(hit, 2);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800368}
369
Davide Pesavento720e25c2019-07-14 01:33:52 -0400370BOOST_AUTO_TEST_CASE(OperatorBool)
371{
372 ScopedEventId se;
373 BOOST_CHECK_EQUAL(static_cast<bool>(se), false);
374 BOOST_CHECK_EQUAL(!se, true);
375
376 se = scheduler.schedule(10_ms, []{});
377 BOOST_CHECK_EQUAL(static_cast<bool>(se), true);
378 BOOST_CHECK_EQUAL(!se, false);
379
380 se.cancel();
381 BOOST_CHECK(!se);
382
383 se = scheduler.schedule(10_ms, []{});
384 BOOST_CHECK(se);
385
386 se.release();
387 BOOST_CHECK(!se);
388}
389
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800390BOOST_AUTO_TEST_SUITE_END() // ScopedEventId
391
Junxiao Shid50f2b42016-08-10 02:59:59 +0000392BOOST_AUTO_TEST_SUITE_END() // TestScheduler
393BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800394
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400395} // namespace ndn::tests