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