blob: 05380f348169ca5301ccfbfea33f1fd5689173f9 [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/*
3 * Copyright (c) 2013-2018 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
22#include "util/scheduler.hpp"
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080023#include "util/scheduler-scoped-event-id.hpp"
Alexander Afanasyevf6468892014-01-29 01:04:14 -080024
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025#include "boost-test.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080026#include "../unit-test-time-fixture.hpp"
Davide Pesavento3a3e1882018-07-17 14:49:15 -040027
Junxiao Shid50f2b42016-08-10 02:59:59 +000028#include <boost/lexical_cast.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080029
30namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080031namespace util {
32namespace scheduler {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080033namespace tests {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080034
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080035using namespace ndn::tests;
36
Junxiao Shid50f2b42016-08-10 02:59:59 +000037class SchedulerFixture : public UnitTestTimeFixture
38{
39public:
40 SchedulerFixture()
41 : scheduler(io)
42 {
43 }
44
45public:
46 Scheduler scheduler;
47};
48
49BOOST_AUTO_TEST_SUITE(Util)
50BOOST_FIXTURE_TEST_SUITE(TestScheduler, SchedulerFixture)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080051
Davide Pesaventoeee3e822016-11-26 19:19:34 +010052BOOST_AUTO_TEST_SUITE(General)
53
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080054BOOST_AUTO_TEST_CASE(Events)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080055{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080056 size_t count1 = 0;
57 size_t count2 = 0;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080058
Davide Pesavento0f830802018-01-16 23:58:58 -050059 scheduler.scheduleEvent(500_ms, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080060 ++count1;
61 BOOST_CHECK_EQUAL(count2, 1);
62 });
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063
Davide Pesavento0f830802018-01-16 23:58:58 -050064 EventId i = scheduler.scheduleEvent(1_s, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080065 BOOST_ERROR("This event should not have been fired");
66 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080067 scheduler.cancelEvent(i);
68
Davide Pesavento0f830802018-01-16 23:58:58 -050069 scheduler.scheduleEvent(250_ms, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080070 BOOST_CHECK_EQUAL(count1, 0);
71 ++count2;
72 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080073
Davide Pesavento0f830802018-01-16 23:58:58 -050074 i = scheduler.scheduleEvent(50_ms, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080075 BOOST_ERROR("This event should not have been fired");
76 });
Alexander Afanasyevf6468892014-01-29 01:04:14 -080077 scheduler.cancelEvent(i);
78
Davide Pesavento0f830802018-01-16 23:58:58 -050079 advanceClocks(25_ms, 1000_ms);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080 BOOST_CHECK_EQUAL(count1, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080081 BOOST_CHECK_EQUAL(count2, 1);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080082}
83
Junxiao Shi86dfa532016-08-10 03:00:11 +000084BOOST_AUTO_TEST_CASE(CallbackException)
85{
86 class MyException : public std::exception
87 {
88 };
Davide Pesavento0f830802018-01-16 23:58:58 -050089 scheduler.scheduleEvent(10_ms, [] { BOOST_THROW_EXCEPTION(MyException()); });
Junxiao Shi86dfa532016-08-10 03:00:11 +000090
91 bool isCallbackInvoked = false;
Davide Pesavento0f830802018-01-16 23:58:58 -050092 scheduler.scheduleEvent(20_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shi86dfa532016-08-10 03:00:11 +000093
Davide Pesavento0f830802018-01-16 23:58:58 -050094 BOOST_CHECK_THROW(this->advanceClocks(6_ms, 2), MyException);
95 this->advanceClocks(6_ms, 2);
Junxiao Shi86dfa532016-08-10 03:00:11 +000096 BOOST_CHECK(isCallbackInvoked);
97}
98
Yingdi Yuf2a82092014-02-03 16:49:15 -080099BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
100{
Yingdi Yuf2a82092014-02-03 16:49:15 -0800101 EventId i;
102 scheduler.cancelEvent(i);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100103
104 // avoid "test case [...] did not check any assertions" message from Boost.Test
105 BOOST_CHECK(true);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800106}
107
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800108BOOST_AUTO_TEST_CASE(SelfCancel)
Yingdi Yuf2a82092014-02-03 16:49:15 -0800109{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800110 EventId selfEventId;
Davide Pesavento0f830802018-01-16 23:58:58 -0500111 selfEventId = scheduler.scheduleEvent(100_ms, [&] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800112 scheduler.cancelEvent(selfEventId);
113 });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800114
Davide Pesavento0f830802018-01-16 23:58:58 -0500115 BOOST_REQUIRE_NO_THROW(advanceClocks(100_ms, 10));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800116}
117
Junxiao Shid50f2b42016-08-10 02:59:59 +0000118class SelfRescheduleFixture : public SchedulerFixture
Yingdi Yuf2a82092014-02-03 16:49:15 -0800119{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800120public:
Yingdi Yuf2a82092014-02-03 16:49:15 -0800121 SelfRescheduleFixture()
Junxiao Shid50f2b42016-08-10 02:59:59 +0000122 : count(0)
Yingdi Yuf2a82092014-02-03 16:49:15 -0800123 {
124 }
125
126 void
127 reschedule()
128 {
Davide Pesavento0f830802018-01-16 23:58:58 -0500129 EventId eventId = scheduler.scheduleEvent(100_ms,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800130 bind(&SelfRescheduleFixture::reschedule, this));
131 scheduler.cancelEvent(selfEventId);
132 selfEventId = eventId;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800133
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800134 if (count < 5)
135 count++;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800136 else
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800137 scheduler.cancelEvent(selfEventId);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800138 }
139
140 void
141 reschedule2()
142 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800143 scheduler.cancelEvent(selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800145 if (count < 5) {
Davide Pesavento0f830802018-01-16 23:58:58 -0500146 selfEventId = scheduler.scheduleEvent(100_ms,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800147 bind(&SelfRescheduleFixture::reschedule2, this));
148 count++;
149 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800150 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700151
Yingdi Yuf2a82092014-02-03 16:49:15 -0800152 void
153 reschedule3()
154 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800155 scheduler.cancelEvent(selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700156
Davide Pesavento0f830802018-01-16 23:58:58 -0500157 scheduler.scheduleEvent(100_ms, [&] { ++count; });
158 scheduler.scheduleEvent(100_ms, [&] { ++count; });
159 scheduler.scheduleEvent(100_ms, [&] { ++count; });
160 scheduler.scheduleEvent(100_ms, [&] { ++count; });
161 scheduler.scheduleEvent(100_ms, [&] { ++count; });
162 scheduler.scheduleEvent(100_ms, [&] { ++count; });
Yingdi Yuf2a82092014-02-03 16:49:15 -0800163 }
164
Junxiao Shid50f2b42016-08-10 02:59:59 +0000165public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800166 EventId selfEventId;
167 size_t count;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800168};
169
170BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
171{
Davide Pesavento0f830802018-01-16 23:58:58 -0500172 selfEventId = scheduler.scheduleEvent(0_s,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800173 bind(&SelfRescheduleFixture::reschedule, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800174
Davide Pesavento0f830802018-01-16 23:58:58 -0500175 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800176
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800177 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800178}
179
180BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
181{
Davide Pesavento0f830802018-01-16 23:58:58 -0500182 selfEventId = scheduler.scheduleEvent(0_s,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800183 bind(&SelfRescheduleFixture::reschedule2, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800184
Davide Pesavento0f830802018-01-16 23:58:58 -0500185 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800186
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800187 BOOST_CHECK_EQUAL(count, 5);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800188}
189
190BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
191{
Davide Pesavento0f830802018-01-16 23:58:58 -0500192 selfEventId = scheduler.scheduleEvent(0_s,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800193 bind(&SelfRescheduleFixture::reschedule3, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800194
Davide Pesavento0f830802018-01-16 23:58:58 -0500195 BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800196
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800197 BOOST_CHECK_EQUAL(count, 6);
Yingdi Yuf2a82092014-02-03 16:49:15 -0800198}
199
Junxiao Shid50f2b42016-08-10 02:59:59 +0000200class CancelAllFixture : public SchedulerFixture
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700201{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000202public:
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700203 CancelAllFixture()
Junxiao Shid50f2b42016-08-10 02:59:59 +0000204 : count(0)
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700205 {
206 }
207
208 void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700209 event()
210 {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800211 ++count;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700212
Davide Pesavento0f830802018-01-16 23:58:58 -0500213 scheduler.scheduleEvent(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700214 }
215
Junxiao Shid50f2b42016-08-10 02:59:59 +0000216public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800217 uint32_t count;
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700218};
219
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700220BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
221{
Davide Pesavento0f830802018-01-16 23:58:58 -0500222 scheduler.scheduleEvent(500_ms, [&] { scheduler.cancelAllEvents(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700223
Davide Pesavento0f830802018-01-16 23:58:58 -0500224 scheduler.scheduleEvent(1_s, [&] { event(); });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700225
Davide Pesavento0f830802018-01-16 23:58:58 -0500226 scheduler.scheduleEvent(3_s, [] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800227 BOOST_ERROR("This event should have been cancelled" );
228 });
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700229
Davide Pesavento0f830802018-01-16 23:58:58 -0500230 advanceClocks(100_ms, 100);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700231
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800232 BOOST_CHECK_EQUAL(count, 0);
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700233}
234
Junxiao Shid50f2b42016-08-10 02:59:59 +0000235BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800236{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000237 Scheduler sched(io);
238 ScopedEventId eid(sched);
Davide Pesavento0f830802018-01-16 23:58:58 -0500239 eid = sched.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000240 sched.cancelAllEvents();
241 eid.cancel(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100242
243 // avoid "test case [...] did not check any assertions" message from Boost.Test
244 BOOST_CHECK(true);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000245}
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700246
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100247BOOST_AUTO_TEST_SUITE_END() // General
248
Junxiao Shid50f2b42016-08-10 02:59:59 +0000249BOOST_AUTO_TEST_SUITE(EventId)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800250
Junxiao Shid50f2b42016-08-10 02:59:59 +0000251using scheduler::EventId;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800252
Junxiao Shid50f2b42016-08-10 02:59:59 +0000253BOOST_AUTO_TEST_CASE(ConstructEmpty)
254{
255 EventId eid;
256 eid = nullptr;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400257 EventId eid2(nullptr);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100258
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400259 BOOST_CHECK(!eid && !eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000260}
261
262BOOST_AUTO_TEST_CASE(Compare)
263{
264 EventId eid, eid2;
265 BOOST_CHECK_EQUAL(eid == eid2, true);
266 BOOST_CHECK_EQUAL(eid != eid2, false);
267 BOOST_CHECK_EQUAL(eid == nullptr, true);
268 BOOST_CHECK_EQUAL(eid != nullptr, false);
269
Davide Pesavento0f830802018-01-16 23:58:58 -0500270 eid = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000271 BOOST_CHECK_EQUAL(eid == eid2, false);
272 BOOST_CHECK_EQUAL(eid != eid2, true);
273 BOOST_CHECK_EQUAL(eid == nullptr, false);
274 BOOST_CHECK_EQUAL(eid != nullptr, true);
275
276 eid2 = eid;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400277 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000278 BOOST_CHECK_EQUAL(eid != eid2, false);
279
Davide Pesavento0f830802018-01-16 23:58:58 -0500280 eid2 = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000281 BOOST_CHECK_EQUAL(eid == eid2, false);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400282 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000283}
284
285BOOST_AUTO_TEST_CASE(Valid)
286{
287 EventId eid;
288 BOOST_CHECK_EQUAL(static_cast<bool>(eid), false);
289 BOOST_CHECK_EQUAL(!eid, true);
290
Davide Pesavento0f830802018-01-16 23:58:58 -0500291 eid = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000292 BOOST_CHECK_EQUAL(static_cast<bool>(eid), true);
293 BOOST_CHECK_EQUAL(!eid, false);
294
295 EventId eid2 = eid;
296 scheduler.cancelEvent(eid2);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400297 BOOST_CHECK(!eid);
298 BOOST_CHECK(!eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000299}
300
301BOOST_AUTO_TEST_CASE(DuringCallback)
302{
303 EventId eid;
Davide Pesavento0f830802018-01-16 23:58:58 -0500304 EventId eid2 = scheduler.scheduleEvent(20_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000305
306 bool isCallbackInvoked = false;
Davide Pesavento0f830802018-01-16 23:58:58 -0500307 eid = scheduler.scheduleEvent(10_ms, [this, &eid, &eid2, &isCallbackInvoked] {
Junxiao Shid50f2b42016-08-10 02:59:59 +0000308 isCallbackInvoked = true;
309
310 // eid is "expired" during callback execution
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400311 BOOST_CHECK(!eid);
312 BOOST_CHECK(eid == nullptr);
313 BOOST_CHECK_NE(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000314
315 scheduler.cancelEvent(eid2);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400316 BOOST_CHECK_EQUAL(eid, eid2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000317 });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400318
Davide Pesavento0f830802018-01-16 23:58:58 -0500319 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000320 BOOST_CHECK(isCallbackInvoked);
321}
322
323BOOST_AUTO_TEST_CASE(Reset)
324{
325 bool isCallbackInvoked = false;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400326 EventId eid = scheduler.scheduleEvent(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
Junxiao Shid50f2b42016-08-10 02:59:59 +0000327 eid.reset();
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400328 BOOST_CHECK(!eid);
329 BOOST_CHECK(eid == nullptr);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000330
Davide Pesavento0f830802018-01-16 23:58:58 -0500331 this->advanceClocks(6_ms, 2);
Junxiao Shid50f2b42016-08-10 02:59:59 +0000332 BOOST_CHECK(isCallbackInvoked);
333}
334
335BOOST_AUTO_TEST_CASE(ToString)
336{
337 std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>());
338
339 EventId eid;
340 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString);
341
Davide Pesavento0f830802018-01-16 23:58:58 -0500342 eid = scheduler.scheduleEvent(10_ms, []{});
Junxiao Shid50f2b42016-08-10 02:59:59 +0000343 BOOST_TEST_MESSAGE("eid=" << eid);
344 BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString);
345}
346
347BOOST_AUTO_TEST_SUITE_END() // EventId
348
349BOOST_AUTO_TEST_SUITE(ScopedEventId)
350
351using scheduler::ScopedEventId;
352
353BOOST_AUTO_TEST_CASE(Destruct)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800354{
355 int hit = 0;
356 {
357 ScopedEventId se(scheduler);
Davide Pesavento0f830802018-01-16 23:58:58 -0500358 se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800359 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500360 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800361 BOOST_CHECK_EQUAL(hit, 0);
362}
363
Junxiao Shid50f2b42016-08-10 02:59:59 +0000364BOOST_AUTO_TEST_CASE(Assign)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800365{
366 int hit1 = 0, hit2 = 0;
367 ScopedEventId se1(scheduler);
Davide Pesavento0f830802018-01-16 23:58:58 -0500368 se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit1; });
369 se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit2; });
370 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800371 BOOST_CHECK_EQUAL(hit1, 0);
372 BOOST_CHECK_EQUAL(hit2, 1);
373}
374
Junxiao Shid50f2b42016-08-10 02:59:59 +0000375BOOST_AUTO_TEST_CASE(Release)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800376{
377 int hit = 0;
378 {
379 ScopedEventId se(scheduler);
Davide Pesavento0f830802018-01-16 23:58:58 -0500380 se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800381 se.release();
382 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500383 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800384 BOOST_CHECK_EQUAL(hit, 1);
385}
386
Junxiao Shid50f2b42016-08-10 02:59:59 +0000387BOOST_AUTO_TEST_CASE(Move)
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800388{
389 int hit = 0;
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400390 unique_ptr<ScopedEventId> se2;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800391 {
392 ScopedEventId se(scheduler);
Davide Pesavento0f830802018-01-16 23:58:58 -0500393 se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400394 se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800395 } // se goes out of scope
Davide Pesavento0f830802018-01-16 23:58:58 -0500396 this->advanceClocks(1_ms, 15);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800397 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesavento3a3e1882018-07-17 14:49:15 -0400398
399 ScopedEventId se3(scheduler);
400 {
401 ScopedEventId se(scheduler);
402 se = scheduler.scheduleEvent(10_ms, [&] { ++hit; });
403 se3 = std::move(se); // move assignment
404 } // se goes out of scope
405 this->advanceClocks(1_ms, 15);
406 BOOST_CHECK_EQUAL(hit, 2);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800407}
408
409BOOST_AUTO_TEST_SUITE_END() // ScopedEventId
410
Junxiao Shid50f2b42016-08-10 02:59:59 +0000411BOOST_AUTO_TEST_SUITE_END() // TestScheduler
412BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800413
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800414} // namespace tests
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800415} // namespace scheduler
416} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800417} // namespace ndn