Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/scheduler.hpp" |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
| 25 | #include "tests/unit/unit-test-time-fixture.hpp" |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 26 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 30 | namespace scheduler { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 31 | namespace tests { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 32 | |
Davide Pesavento | fd61231 | 2019-03-15 18:45:46 -0400 | [diff] [blame] | 33 | class SchedulerFixture : public ndn::tests::UnitTestTimeFixture |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 34 | { |
| 35 | public: |
| 36 | SchedulerFixture() |
| 37 | : scheduler(io) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | public: |
| 42 | Scheduler scheduler; |
| 43 | }; |
| 44 | |
| 45 | BOOST_AUTO_TEST_SUITE(Util) |
| 46 | BOOST_FIXTURE_TEST_SUITE(TestScheduler, SchedulerFixture) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 47 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 48 | BOOST_AUTO_TEST_SUITE(General) |
| 49 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 50 | BOOST_AUTO_TEST_CASE(Events) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 51 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 52 | size_t count1 = 0; |
| 53 | size_t count2 = 0; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 54 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 55 | scheduler.schedule(500_ms, [&] { |
| 56 | ++count1; |
| 57 | BOOST_CHECK_EQUAL(count2, 1); |
| 58 | }); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 60 | EventId i = scheduler.schedule(1_s, [] { BOOST_ERROR("This event should not have been fired"); }); |
| 61 | i.cancel(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 62 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 63 | scheduler.schedule(250_ms, [&] { |
| 64 | BOOST_CHECK_EQUAL(count1, 0); |
| 65 | ++count2; |
| 66 | }); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 67 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 68 | i = scheduler.schedule(50_ms, [&] { BOOST_ERROR("This event should not have been fired"); }); |
| 69 | i.cancel(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 70 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 71 | advanceClocks(25_ms, 1000_ms); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(count1, 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(count2, 1); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 76 | BOOST_AUTO_TEST_CASE(CallbackException) |
| 77 | { |
| 78 | class MyException : public std::exception |
| 79 | { |
| 80 | }; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 81 | scheduler.schedule(10_ms, [] { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 82 | // 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 Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 87 | |
| 88 | bool isCallbackInvoked = false; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 89 | scheduler.schedule(20_ms, [&isCallbackInvoked] { isCallbackInvoked = true; }); |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 90 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 91 | BOOST_CHECK_THROW(this->advanceClocks(6_ms, 2), MyException); |
| 92 | this->advanceClocks(6_ms, 2); |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 93 | BOOST_CHECK(isCallbackInvoked); |
| 94 | } |
| 95 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 96 | BOOST_AUTO_TEST_CASE(CancelEmptyEvent) |
| 97 | { |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 98 | EventId i; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 99 | i.cancel(); |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 100 | |
| 101 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 102 | BOOST_CHECK(true); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(SelfCancel) |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 106 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 107 | EventId selfEventId; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 108 | selfEventId = scheduler.schedule(100_ms, [&] { selfEventId.cancel(); }); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 109 | BOOST_REQUIRE_NO_THROW(advanceClocks(100_ms, 10)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 112 | class SelfRescheduleFixture : public SchedulerFixture |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 113 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 114 | public: |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 115 | void |
| 116 | reschedule() |
| 117 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 118 | EventId eventId = scheduler.schedule(100_ms, bind(&SelfRescheduleFixture::reschedule, this)); |
| 119 | selfEventId.cancel(); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 120 | selfEventId = eventId; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 122 | if (count < 5) |
| 123 | count++; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 124 | else |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 125 | selfEventId.cancel(); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void |
| 129 | reschedule2() |
| 130 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 131 | selfEventId.cancel(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 133 | if (count < 5) { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 134 | selfEventId = scheduler.schedule(100_ms, bind(&SelfRescheduleFixture::reschedule2, this)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 135 | count++; |
| 136 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 137 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 138 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 139 | void |
| 140 | reschedule3() |
| 141 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 142 | selfEventId.cancel(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 143 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 144 | 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 Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 152 | public: |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 153 | EventId selfEventId; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 154 | size_t count = 0; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture) |
| 158 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 159 | selfEventId = scheduler.schedule(0_s, bind(&SelfRescheduleFixture::reschedule, this)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 160 | BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(count, 5); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture) |
| 165 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 166 | selfEventId = scheduler.schedule(0_s, bind(&SelfRescheduleFixture::reschedule2, this)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 167 | BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(count, 5); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture) |
| 172 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 173 | selfEventId = scheduler.schedule(0_s, bind(&SelfRescheduleFixture::reschedule3, this)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 174 | BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 175 | BOOST_CHECK_EQUAL(count, 6); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 178 | class CancelAllFixture : public SchedulerFixture |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 179 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 180 | public: |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 181 | void |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 182 | event() |
| 183 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 184 | ++count; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 185 | scheduler.schedule(1_s, [&] { event(); }); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 188 | public: |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 189 | uint32_t count = 0; |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 192 | BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture) |
| 193 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 194 | 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 Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 197 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 198 | advanceClocks(100_ms, 100); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 199 | BOOST_CHECK_EQUAL(count, 0); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 202 | BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691 |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 203 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 204 | Scheduler sched(io); |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 205 | ScopedEventId eid = sched.schedule(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 206 | sched.cancelAllEvents(); |
| 207 | eid.cancel(); // should not crash |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 208 | |
| 209 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 210 | BOOST_CHECK(true); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 211 | } |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 212 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 213 | BOOST_AUTO_TEST_SUITE_END() // General |
| 214 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 215 | BOOST_AUTO_TEST_SUITE(EventId) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 216 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 217 | using scheduler::EventId; |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 218 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 219 | BOOST_AUTO_TEST_CASE(ConstructEmpty) |
| 220 | { |
| 221 | EventId eid; |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 222 | BOOST_CHECK(!eid); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Davide Pesavento | ecfb391 | 2019-07-02 23:08:22 -0400 | [diff] [blame] | 225 | BOOST_AUTO_TEST_CASE(Equality) |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 226 | { |
| 227 | EventId eid, eid2; |
Davide Pesavento | ecfb391 | 2019-07-02 23:08:22 -0400 | [diff] [blame] | 228 | BOOST_CHECK(eid == eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 229 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 230 | eid = scheduler.schedule(10_ms, []{}); |
Davide Pesavento | ecfb391 | 2019-07-02 23:08:22 -0400 | [diff] [blame] | 231 | BOOST_CHECK(eid != eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 232 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 233 | eid2 = scheduler.schedule(10_ms, []{}); |
Davide Pesavento | ecfb391 | 2019-07-02 23:08:22 -0400 | [diff] [blame] | 234 | 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 Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 252 | BOOST_AUTO_TEST_CASE(OperatorBool) |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 253 | { |
| 254 | EventId eid; |
| 255 | BOOST_CHECK_EQUAL(static_cast<bool>(eid), false); |
| 256 | BOOST_CHECK_EQUAL(!eid, true); |
| 257 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 258 | eid = scheduler.schedule(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 259 | BOOST_CHECK_EQUAL(static_cast<bool>(eid), true); |
| 260 | BOOST_CHECK_EQUAL(!eid, false); |
| 261 | |
| 262 | EventId eid2 = eid; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 263 | eid2.cancel(); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 264 | BOOST_CHECK(!eid); |
| 265 | BOOST_CHECK(!eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | BOOST_AUTO_TEST_CASE(DuringCallback) |
| 269 | { |
| 270 | EventId eid; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 271 | EventId eid2 = scheduler.schedule(20_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 272 | |
| 273 | bool isCallbackInvoked = false; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 274 | eid = scheduler.schedule(10_ms, [&eid, &eid2, &isCallbackInvoked] { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 275 | isCallbackInvoked = true; |
| 276 | |
| 277 | // eid is "expired" during callback execution |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 278 | BOOST_CHECK(!eid); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 279 | BOOST_CHECK_NE(eid, eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 280 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 281 | eid2.cancel(); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(eid, eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 283 | }); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 284 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 285 | this->advanceClocks(6_ms, 2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 286 | BOOST_CHECK(isCallbackInvoked); |
| 287 | } |
| 288 | |
| 289 | BOOST_AUTO_TEST_CASE(Reset) |
| 290 | { |
| 291 | bool isCallbackInvoked = false; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 292 | EventId eid = scheduler.schedule(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; }); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 293 | eid.reset(); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 294 | BOOST_CHECK(!eid); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 295 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 296 | this->advanceClocks(6_ms, 2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 297 | BOOST_CHECK(isCallbackInvoked); |
| 298 | } |
| 299 | |
| 300 | BOOST_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 Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 307 | eid = scheduler.schedule(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 308 | BOOST_TEST_MESSAGE("eid=" << eid); |
| 309 | BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString); |
| 310 | } |
| 311 | |
| 312 | BOOST_AUTO_TEST_SUITE_END() // EventId |
| 313 | |
| 314 | BOOST_AUTO_TEST_SUITE(ScopedEventId) |
| 315 | |
| 316 | using scheduler::ScopedEventId; |
| 317 | |
| 318 | BOOST_AUTO_TEST_CASE(Destruct) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 319 | { |
| 320 | int hit = 0; |
| 321 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 322 | ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; }); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 323 | } // se goes out of scope |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 324 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 325 | BOOST_CHECK_EQUAL(hit, 0); |
| 326 | } |
| 327 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 328 | BOOST_AUTO_TEST_CASE(Assign) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 329 | { |
| 330 | int hit1 = 0, hit2 = 0; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 331 | ScopedEventId se1 = scheduler.schedule(10_ms, [&] { ++hit1; }); |
| 332 | se1 = scheduler.schedule(10_ms, [&] { ++hit2; }); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 333 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 334 | BOOST_CHECK_EQUAL(hit1, 0); |
| 335 | BOOST_CHECK_EQUAL(hit2, 1); |
| 336 | } |
| 337 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 338 | BOOST_AUTO_TEST_CASE(Release) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 339 | { |
| 340 | int hit = 0; |
| 341 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 342 | ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; }); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 343 | se.release(); |
| 344 | } // se goes out of scope |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 345 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 346 | BOOST_CHECK_EQUAL(hit, 1); |
| 347 | } |
| 348 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 349 | BOOST_AUTO_TEST_CASE(Move) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 350 | { |
| 351 | int hit = 0; |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 352 | unique_ptr<ScopedEventId> se2; |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 353 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 354 | ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; }); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 355 | se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 356 | } // se goes out of scope |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 357 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 358 | BOOST_CHECK_EQUAL(hit, 1); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 359 | |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 360 | ScopedEventId se3; |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 361 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 362 | ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; }); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 363 | 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 Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 369 | BOOST_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 Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 389 | BOOST_AUTO_TEST_SUITE_END() // ScopedEventId |
| 390 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 391 | BOOST_AUTO_TEST_SUITE_END() // TestScheduler |
| 392 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 393 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 394 | } // namespace tests |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 395 | } // namespace scheduler |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 396 | } // namespace ndn |