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