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" |
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); |
| 237 | ScopedEventId eid(sched); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 238 | eid = sched.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 239 | sched.cancelAllEvents(); |
| 240 | eid.cancel(); // should not crash |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 241 | |
| 242 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 243 | BOOST_CHECK(true); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 244 | } |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 245 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 246 | BOOST_AUTO_TEST_SUITE_END() // General |
| 247 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 248 | BOOST_AUTO_TEST_SUITE(EventId) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 249 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 250 | using scheduler::EventId; |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 251 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 252 | BOOST_AUTO_TEST_CASE(ConstructEmpty) |
| 253 | { |
| 254 | EventId eid; |
| 255 | eid = nullptr; |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 256 | |
| 257 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 258 | BOOST_CHECK(true); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | BOOST_AUTO_TEST_CASE(Compare) |
| 262 | { |
| 263 | EventId eid, eid2; |
| 264 | BOOST_CHECK_EQUAL(eid == eid2, true); |
| 265 | BOOST_CHECK_EQUAL(eid != eid2, false); |
| 266 | BOOST_CHECK_EQUAL(eid == nullptr, true); |
| 267 | BOOST_CHECK_EQUAL(eid != nullptr, false); |
| 268 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 269 | eid = scheduler.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 270 | BOOST_CHECK_EQUAL(eid == eid2, false); |
| 271 | BOOST_CHECK_EQUAL(eid != eid2, true); |
| 272 | BOOST_CHECK_EQUAL(eid == nullptr, false); |
| 273 | BOOST_CHECK_EQUAL(eid != nullptr, true); |
| 274 | |
| 275 | eid2 = eid; |
| 276 | BOOST_CHECK_EQUAL(eid == eid2, true); |
| 277 | BOOST_CHECK_EQUAL(eid != eid2, false); |
| 278 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 279 | eid2 = scheduler.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 280 | BOOST_CHECK_EQUAL(eid == eid2, false); |
| 281 | BOOST_CHECK_EQUAL(eid != eid2, true); |
| 282 | } |
| 283 | |
| 284 | BOOST_AUTO_TEST_CASE(Valid) |
| 285 | { |
| 286 | EventId eid; |
| 287 | BOOST_CHECK_EQUAL(static_cast<bool>(eid), false); |
| 288 | BOOST_CHECK_EQUAL(!eid, true); |
| 289 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 290 | eid = scheduler.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 291 | BOOST_CHECK_EQUAL(static_cast<bool>(eid), true); |
| 292 | BOOST_CHECK_EQUAL(!eid, false); |
| 293 | |
| 294 | EventId eid2 = eid; |
| 295 | scheduler.cancelEvent(eid2); |
| 296 | BOOST_CHECK_EQUAL(static_cast<bool>(eid), false); |
| 297 | BOOST_CHECK_EQUAL(!eid, true); |
| 298 | BOOST_CHECK_EQUAL(static_cast<bool>(eid2), false); |
| 299 | BOOST_CHECK_EQUAL(!eid2, true); |
| 300 | } |
| 301 | |
| 302 | BOOST_AUTO_TEST_CASE(DuringCallback) |
| 303 | { |
| 304 | EventId eid; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 305 | EventId eid2 = scheduler.scheduleEvent(20_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 306 | |
| 307 | bool isCallbackInvoked = false; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 308 | eid = scheduler.scheduleEvent(10_ms, [this, &eid, &eid2, &isCallbackInvoked] { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 309 | isCallbackInvoked = true; |
| 310 | |
| 311 | // eid is "expired" during callback execution |
| 312 | BOOST_CHECK_EQUAL(static_cast<bool>(eid), false); |
| 313 | BOOST_CHECK_EQUAL(!eid, true); |
| 314 | BOOST_CHECK_EQUAL(eid == eid2, false); |
| 315 | BOOST_CHECK_EQUAL(eid != eid2, true); |
| 316 | BOOST_CHECK_EQUAL(eid == nullptr, true); |
| 317 | BOOST_CHECK_EQUAL(eid != nullptr, false); |
| 318 | |
| 319 | scheduler.cancelEvent(eid2); |
| 320 | BOOST_CHECK_EQUAL(eid == eid2, true); |
| 321 | BOOST_CHECK_EQUAL(eid != eid2, false); |
| 322 | }); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 323 | this->advanceClocks(6_ms, 2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 324 | BOOST_CHECK(isCallbackInvoked); |
| 325 | } |
| 326 | |
| 327 | BOOST_AUTO_TEST_CASE(Reset) |
| 328 | { |
| 329 | bool isCallbackInvoked = false; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 330 | EventId eid = scheduler.scheduleEvent(10_ms, |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 331 | [&isCallbackInvoked]{ isCallbackInvoked = true; }); |
| 332 | eid.reset(); |
| 333 | BOOST_CHECK_EQUAL(!eid, true); |
| 334 | BOOST_CHECK_EQUAL(eid == nullptr, true); |
| 335 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 336 | this->advanceClocks(6_ms, 2); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 337 | BOOST_CHECK(isCallbackInvoked); |
| 338 | } |
| 339 | |
| 340 | BOOST_AUTO_TEST_CASE(ToString) |
| 341 | { |
| 342 | std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>()); |
| 343 | |
| 344 | EventId eid; |
| 345 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString); |
| 346 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 347 | eid = scheduler.scheduleEvent(10_ms, []{}); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 348 | BOOST_TEST_MESSAGE("eid=" << eid); |
| 349 | BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString); |
| 350 | } |
| 351 | |
| 352 | BOOST_AUTO_TEST_SUITE_END() // EventId |
| 353 | |
| 354 | BOOST_AUTO_TEST_SUITE(ScopedEventId) |
| 355 | |
| 356 | using scheduler::ScopedEventId; |
| 357 | |
| 358 | BOOST_AUTO_TEST_CASE(Destruct) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 359 | { |
| 360 | int hit = 0; |
| 361 | { |
| 362 | ScopedEventId se(scheduler); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 363 | se = scheduler.scheduleEvent(10_ms, [&] { ++hit; }); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 364 | } // se goes out of scope |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 365 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 366 | BOOST_CHECK_EQUAL(hit, 0); |
| 367 | } |
| 368 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 369 | BOOST_AUTO_TEST_CASE(Assign) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 370 | { |
| 371 | int hit1 = 0, hit2 = 0; |
| 372 | ScopedEventId se1(scheduler); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 373 | se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit1; }); |
| 374 | se1 = scheduler.scheduleEvent(10_ms, [&] { ++hit2; }); |
| 375 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 376 | BOOST_CHECK_EQUAL(hit1, 0); |
| 377 | BOOST_CHECK_EQUAL(hit2, 1); |
| 378 | } |
| 379 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 380 | BOOST_AUTO_TEST_CASE(Release) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 381 | { |
| 382 | int hit = 0; |
| 383 | { |
| 384 | ScopedEventId se(scheduler); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 385 | se = scheduler.scheduleEvent(10_ms, [&] { ++hit; }); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 386 | se.release(); |
| 387 | } // se goes out of scope |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 388 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 389 | BOOST_CHECK_EQUAL(hit, 1); |
| 390 | } |
| 391 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 392 | BOOST_AUTO_TEST_CASE(Move) |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 393 | { |
| 394 | int hit = 0; |
| 395 | unique_ptr<scheduler::ScopedEventId> se2; |
| 396 | { |
| 397 | ScopedEventId se(scheduler); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 398 | se = scheduler.scheduleEvent(10_ms, [&] { ++hit; }); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 399 | se2.reset(new ScopedEventId(std::move(se))); |
| 400 | } // se goes out of scope |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 401 | this->advanceClocks(1_ms, 15); |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 402 | BOOST_CHECK_EQUAL(hit, 1); |
| 403 | } |
| 404 | |
| 405 | BOOST_AUTO_TEST_SUITE_END() // ScopedEventId |
| 406 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 407 | BOOST_AUTO_TEST_SUITE_END() // TestScheduler |
| 408 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 409 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 410 | } // namespace tests |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 411 | } // namespace scheduler |
| 412 | } // namespace util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 413 | } // namespace ndn |