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