blob: 88c431318f086fe3b2d7129d3e10713503366dd9 [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Junxiao Shid50f2b42016-08-10 02:59:59 +0000204BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800205{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500206 ScopedEventId eid = scheduler.schedule(10_ms, []{});
207 scheduler.cancelAllEvents();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000208 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
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400226BOOST_AUTO_TEST_CASE(Equality)
Junxiao Shid50f2b42016-08-10 02:59:59 +0000227{
228 EventId eid, eid2;
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400229 BOOST_CHECK(eid == eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000230
Junxiao Shia5f233e2019-03-18 09:39:22 -0600231 eid = scheduler.schedule(10_ms, []{});
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400232 BOOST_CHECK(eid != eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000233
Junxiao Shia5f233e2019-03-18 09:39:22 -0600234 eid2 = scheduler.schedule(10_ms, []{});
Davide Pesaventoecfb3912019-07-02 23:08:22 -0400235 BOOST_CHECK(eid != eid2);
236
237 eid.cancel();
238 BOOST_CHECK(eid != eid2);
239 BOOST_CHECK(eid == EventId{});
240
241 eid2.cancel();
242 BOOST_CHECK(eid == eid2);
243
244 eid = eid2 = scheduler.schedule(20_ms, []{});
245 BOOST_CHECK(eid == eid2);
246 BOOST_CHECK(eid != EventId{});
247
248 eid.cancel();
249 BOOST_CHECK(eid == eid2);
250 BOOST_CHECK(eid == EventId{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000251}
252
Davide Pesavento720e25c2019-07-14 01:33:52 -0400253BOOST_AUTO_TEST_CASE(OperatorBool)
Junxiao Shid50f2b42016-08-10 02:59:59 +0000254{
255 EventId eid;
256 BOOST_CHECK_EQUAL(static_cast<bool>(eid), false);
257 BOOST_CHECK_EQUAL(!eid, true);
258
Junxiao Shia5f233e2019-03-18 09:39:22 -0600259 eid = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000260 BOOST_CHECK_EQUAL(static_cast<bool>(eid), true);
261 BOOST_CHECK_EQUAL(!eid, false);
262
263 EventId eid2 = eid;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600264 eid2.cancel();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400265 BOOST_CHECK(!eid);
266 BOOST_CHECK(!eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000267}
268
269BOOST_AUTO_TEST_CASE(DuringCallback)
270{
271 EventId eid;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600272 EventId eid2 = scheduler.schedule(20_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000273
274 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600275 eid = scheduler.schedule(10_ms, [&eid, &eid2, &isCallbackInvoked] {
Junxiao Shid50f2b42016-08-10 02:59:59 +0000276 isCallbackInvoked = true;
277
278 // eid is "expired" during callback execution
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400279 BOOST_CHECK(!eid);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400280 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000281
Junxiao Shia5f233e2019-03-18 09:39:22 -0600282 eid2.cancel();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400283 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000284 });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400285
Davide Pesavento0f830802018-01-16 23:58:58 -0500286 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000287 BOOST_CHECK(isCallbackInvoked);
288}
289
290BOOST_AUTO_TEST_CASE(Reset)
291{
292 bool isCallbackInvoked = false;
Junxiao Shia5f233e2019-03-18 09:39:22 -0600293 EventId eid = scheduler.schedule(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shid50f2b42016-08-10 02:59:59 +0000294 eid.reset();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400295 BOOST_CHECK(!eid);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000296
Davide Pesavento0f830802018-01-16 23:58:58 -0500297 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000298 BOOST_CHECK(isCallbackInvoked);
299}
300
301BOOST_AUTO_TEST_CASE(ToString)
302{
303 std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>());
304
305 EventId eid;
306 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString);
307
Junxiao Shia5f233e2019-03-18 09:39:22 -0600308 eid = scheduler.schedule(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000309 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400394} // namespace ndn::tests