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 util { |
| 31 | namespace scheduler { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 32 | namespace tests { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 33 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 34 | using namespace ndn::tests; |
| 35 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 36 | class SchedulerFixture : public UnitTestTimeFixture |
| 37 | { |
| 38 | public: |
| 39 | SchedulerFixture() |
| 40 | : scheduler(io) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | public: |
| 45 | Scheduler scheduler; |
| 46 | }; |
| 47 | |
| 48 | BOOST_AUTO_TEST_SUITE(Util) |
| 49 | BOOST_FIXTURE_TEST_SUITE(TestScheduler, SchedulerFixture) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 50 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 51 | BOOST_AUTO_TEST_SUITE(General) |
| 52 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 53 | BOOST_AUTO_TEST_CASE(Events) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 54 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 55 | size_t count1 = 0; |
| 56 | size_t count2 = 0; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 57 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 58 | scheduler.scheduleEvent(500_ms, [&] { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 59 | ++count1; |
| 60 | BOOST_CHECK_EQUAL(count2, 1); |
| 61 | }); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 62 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 63 | EventId i = scheduler.scheduleEvent(1_s, [&] { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 64 | BOOST_ERROR("This event should not have been fired"); |
| 65 | }); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 66 | scheduler.cancelEvent(i); |
| 67 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 68 | scheduler.scheduleEvent(250_ms, [&] { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(count1, 0); |
| 70 | ++count2; |
| 71 | }); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 72 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 73 | i = scheduler.scheduleEvent(50_ms, [&] { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 74 | BOOST_ERROR("This event should not have been fired"); |
| 75 | }); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 76 | scheduler.cancelEvent(i); |
| 77 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 78 | advanceClocks(25_ms, 1000_ms); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(count1, 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(count2, 1); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 83 | BOOST_AUTO_TEST_CASE(CallbackException) |
| 84 | { |
| 85 | class MyException : public std::exception |
| 86 | { |
| 87 | }; |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 88 | scheduler.scheduleEvent(10_ms, [] { |
| 89 | // use plain 'throw' to ensure that Scheduler does not depend on the |
| 90 | // internal machinery of NDN_THROW and that it can catch all exceptions |
| 91 | // regardless of how they are thrown by the application |
| 92 | throw MyException{}; |
| 93 | }); |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 94 | |
| 95 | bool isCallbackInvoked = false; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 96 | scheduler.scheduleEvent(20_ms, [&isCallbackInvoked] { isCallbackInvoked = true; }); |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 97 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 98 | BOOST_CHECK_THROW(this->advanceClocks(6_ms, 2), MyException); |
| 99 | this->advanceClocks(6_ms, 2); |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 100 | BOOST_CHECK(isCallbackInvoked); |
| 101 | } |
| 102 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 103 | BOOST_AUTO_TEST_CASE(CancelEmptyEvent) |
| 104 | { |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 105 | EventId i; |
| 106 | scheduler.cancelEvent(i); |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 107 | |
| 108 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 109 | BOOST_CHECK(true); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 112 | BOOST_AUTO_TEST_CASE(SelfCancel) |
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 | EventId selfEventId; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 115 | selfEventId = scheduler.scheduleEvent(100_ms, [&] { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 116 | scheduler.cancelEvent(selfEventId); |
| 117 | }); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 118 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 119 | BOOST_REQUIRE_NO_THROW(advanceClocks(100_ms, 10)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 122 | class SelfRescheduleFixture : public SchedulerFixture |
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 | public: |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 125 | SelfRescheduleFixture() |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 126 | : count(0) |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 127 | { |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | reschedule() |
| 132 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 133 | EventId eventId = scheduler.scheduleEvent(100_ms, |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 134 | bind(&SelfRescheduleFixture::reschedule, this)); |
| 135 | scheduler.cancelEvent(selfEventId); |
| 136 | selfEventId = eventId; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 137 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 138 | if (count < 5) |
| 139 | count++; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 140 | else |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 141 | scheduler.cancelEvent(selfEventId); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
| 145 | reschedule2() |
| 146 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 147 | scheduler.cancelEvent(selfEventId); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 149 | if (count < 5) { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 150 | selfEventId = scheduler.scheduleEvent(100_ms, |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 151 | bind(&SelfRescheduleFixture::reschedule2, this)); |
| 152 | count++; |
| 153 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 154 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 155 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 156 | void |
| 157 | reschedule3() |
| 158 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 159 | scheduler.cancelEvent(selfEventId); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 160 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 161 | scheduler.scheduleEvent(100_ms, [&] { ++count; }); |
| 162 | scheduler.scheduleEvent(100_ms, [&] { ++count; }); |
| 163 | scheduler.scheduleEvent(100_ms, [&] { ++count; }); |
| 164 | scheduler.scheduleEvent(100_ms, [&] { ++count; }); |
| 165 | scheduler.scheduleEvent(100_ms, [&] { ++count; }); |
| 166 | scheduler.scheduleEvent(100_ms, [&] { ++count; }); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 169 | public: |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 170 | EventId selfEventId; |
| 171 | size_t count; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture) |
| 175 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 176 | selfEventId = scheduler.scheduleEvent(0_s, |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 177 | bind(&SelfRescheduleFixture::reschedule, this)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 178 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 179 | BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 180 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 181 | BOOST_CHECK_EQUAL(count, 5); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture) |
| 185 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 186 | selfEventId = scheduler.scheduleEvent(0_s, |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 187 | bind(&SelfRescheduleFixture::reschedule2, this)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 188 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 189 | BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 190 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 191 | BOOST_CHECK_EQUAL(count, 5); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture) |
| 195 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 196 | selfEventId = scheduler.scheduleEvent(0_s, |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 197 | bind(&SelfRescheduleFixture::reschedule3, this)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 198 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 199 | BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 200 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(count, 6); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 204 | class CancelAllFixture : public SchedulerFixture |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 205 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 206 | public: |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 207 | CancelAllFixture() |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 208 | : count(0) |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 209 | { |
| 210 | } |
| 211 | |
| 212 | void |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 213 | event() |
| 214 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 215 | ++count; |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 216 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 217 | scheduler.scheduleEvent(1_s, [&] { event(); }); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 220 | public: |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 221 | uint32_t count; |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 224 | BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture) |
| 225 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 226 | scheduler.scheduleEvent(500_ms, [&] { scheduler.cancelAllEvents(); }); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 227 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 228 | scheduler.scheduleEvent(1_s, [&] { event(); }); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 229 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 230 | scheduler.scheduleEvent(3_s, [] { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 231 | BOOST_ERROR("This event should have been cancelled" ); |
| 232 | }); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 233 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 234 | advanceClocks(100_ms, 100); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 235 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(count, 0); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 239 | BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691 |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 240 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 241 | Scheduler sched(io); |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 242 | ScopedEventId eid = sched.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 243 | sched.cancelAllEvents(); |
| 244 | eid.cancel(); // should not crash |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 245 | |
| 246 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 247 | BOOST_CHECK(true); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 248 | } |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 249 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 250 | BOOST_AUTO_TEST_SUITE_END() // General |
| 251 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 252 | BOOST_AUTO_TEST_SUITE(EventId) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 253 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 254 | using scheduler::EventId; |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 255 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 256 | BOOST_AUTO_TEST_CASE(ConstructEmpty) |
| 257 | { |
| 258 | EventId eid; |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 259 | BOOST_CHECK(!eid); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | BOOST_AUTO_TEST_CASE(Compare) |
| 263 | { |
| 264 | EventId eid, eid2; |
| 265 | BOOST_CHECK_EQUAL(eid == eid2, true); |
| 266 | BOOST_CHECK_EQUAL(eid != eid2, false); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 267 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 268 | eid = scheduler.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 269 | BOOST_CHECK_EQUAL(eid == eid2, false); |
| 270 | BOOST_CHECK_EQUAL(eid != eid2, true); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 271 | |
| 272 | eid2 = eid; |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 273 | BOOST_CHECK_EQUAL(eid, eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 274 | BOOST_CHECK_EQUAL(eid != eid2, false); |
| 275 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 276 | eid2 = scheduler.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 277 | BOOST_CHECK_EQUAL(eid == eid2, false); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 278 | BOOST_CHECK_NE(eid, eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | BOOST_AUTO_TEST_CASE(Valid) |
| 282 | { |
| 283 | EventId eid; |
| 284 | BOOST_CHECK_EQUAL(static_cast<bool>(eid), false); |
| 285 | BOOST_CHECK_EQUAL(!eid, true); |
| 286 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 287 | eid = scheduler.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 288 | BOOST_CHECK_EQUAL(static_cast<bool>(eid), true); |
| 289 | BOOST_CHECK_EQUAL(!eid, false); |
| 290 | |
| 291 | EventId eid2 = eid; |
| 292 | scheduler.cancelEvent(eid2); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 293 | BOOST_CHECK(!eid); |
| 294 | BOOST_CHECK(!eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | BOOST_AUTO_TEST_CASE(DuringCallback) |
| 298 | { |
| 299 | EventId eid; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 300 | EventId eid2 = scheduler.scheduleEvent(20_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 301 | |
| 302 | bool isCallbackInvoked = false; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 303 | eid = scheduler.scheduleEvent(10_ms, [this, &eid, &eid2, &isCallbackInvoked] { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 304 | isCallbackInvoked = true; |
| 305 | |
| 306 | // eid is "expired" during callback execution |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 307 | BOOST_CHECK(!eid); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 308 | BOOST_CHECK_NE(eid, eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 309 | |
| 310 | scheduler.cancelEvent(eid2); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 311 | BOOST_CHECK_EQUAL(eid, eid2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 312 | }); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 313 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 314 | this->advanceClocks(6_ms, 2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 315 | BOOST_CHECK(isCallbackInvoked); |
| 316 | } |
| 317 | |
| 318 | BOOST_AUTO_TEST_CASE(Reset) |
| 319 | { |
| 320 | bool isCallbackInvoked = false; |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 321 | EventId eid = scheduler.scheduleEvent(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; }); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 322 | eid.reset(); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 323 | BOOST_CHECK(!eid); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 324 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 325 | this->advanceClocks(6_ms, 2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 326 | BOOST_CHECK(isCallbackInvoked); |
| 327 | } |
| 328 | |
| 329 | BOOST_AUTO_TEST_CASE(ToString) |
| 330 | { |
| 331 | std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>()); |
| 332 | |
| 333 | EventId eid; |
| 334 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString); |
| 335 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 336 | eid = scheduler.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 337 | BOOST_TEST_MESSAGE("eid=" << eid); |
| 338 | BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString); |
| 339 | } |
| 340 | |
| 341 | BOOST_AUTO_TEST_SUITE_END() // EventId |
| 342 | |
| 343 | BOOST_AUTO_TEST_SUITE(ScopedEventId) |
| 344 | |
| 345 | using scheduler::ScopedEventId; |
| 346 | |
| 347 | BOOST_AUTO_TEST_CASE(Destruct) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 348 | { |
| 349 | int hit = 0; |
| 350 | { |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 351 | ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; }); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 352 | } // se goes out of scope |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 353 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 354 | BOOST_CHECK_EQUAL(hit, 0); |
| 355 | } |
| 356 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 357 | BOOST_AUTO_TEST_CASE(Assign) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 358 | { |
| 359 | int hit1 = 0, hit2 = 0; |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 360 | ScopedEventId se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit1; }); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 361 | se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit2; }); |
| 362 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 363 | BOOST_CHECK_EQUAL(hit1, 0); |
| 364 | BOOST_CHECK_EQUAL(hit2, 1); |
| 365 | } |
| 366 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 367 | BOOST_AUTO_TEST_CASE(Release) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 368 | { |
| 369 | int hit = 0; |
| 370 | { |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 371 | ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; }); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 372 | se.release(); |
| 373 | } // se goes out of scope |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 374 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 375 | BOOST_CHECK_EQUAL(hit, 1); |
| 376 | } |
| 377 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 378 | BOOST_AUTO_TEST_CASE(Move) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 379 | { |
| 380 | int hit = 0; |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 381 | unique_ptr<ScopedEventId> se2; |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 382 | { |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 383 | ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; }); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 384 | se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 385 | } // se goes out of scope |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 386 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 387 | BOOST_CHECK_EQUAL(hit, 1); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 388 | |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 389 | ScopedEventId se3; |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 390 | { |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 391 | ScopedEventId se = scheduler.scheduleEvent(10_ms, [&] { ++hit; }); |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 392 | se3 = std::move(se); // move assignment |
| 393 | } // se goes out of scope |
| 394 | this->advanceClocks(1_ms, 15); |
| 395 | BOOST_CHECK_EQUAL(hit, 2); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | BOOST_AUTO_TEST_SUITE_END() // ScopedEventId |
| 399 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 400 | BOOST_AUTO_TEST_SUITE_END() // TestScheduler |
| 401 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 402 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 403 | } // namespace tests |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 404 | } // namespace scheduler |
| 405 | } // namespace util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 406 | } // namespace ndn |