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