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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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" |
| 23 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 28 | BOOST_AUTO_TEST_SUITE(UtilTestScheduler) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 29 | |
| 30 | struct SchedulerFixture |
| 31 | { |
| 32 | SchedulerFixture() |
| 33 | : count1(0) |
| 34 | , count2(0) |
| 35 | , count3(0) |
| 36 | , count4(0) |
| 37 | { |
| 38 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 40 | void |
| 41 | event1() |
| 42 | { |
| 43 | BOOST_CHECK_EQUAL(count3, 1); |
| 44 | ++count1; |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | event2() |
| 49 | { |
| 50 | ++count2; |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | event3() |
| 55 | { |
| 56 | BOOST_CHECK_EQUAL(count1, 0); |
| 57 | ++count3; |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | event4() |
| 62 | { |
| 63 | ++count4; |
| 64 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 66 | int count1; |
| 67 | int count2; |
| 68 | int count3; |
| 69 | int count4; |
| 70 | }; |
| 71 | |
| 72 | BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture) |
| 73 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 74 | boost::asio::io_service io; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 75 | |
| 76 | Scheduler scheduler(io); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 77 | scheduler.scheduleEvent(time::milliseconds(500), bind(&SchedulerFixture::event1, this)); |
| 78 | |
| 79 | EventId i = scheduler.scheduleEvent(time::seconds(1), bind(&SchedulerFixture::event2, this)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 80 | scheduler.cancelEvent(i); |
| 81 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 82 | scheduler.scheduleEvent(time::milliseconds(250), bind(&SchedulerFixture::event3, this)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 84 | i = scheduler.scheduleEvent(time::milliseconds(50), bind(&SchedulerFixture::event2, this)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 85 | scheduler.cancelEvent(i); |
| 86 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 87 | i = scheduler.schedulePeriodicEvent(time::milliseconds(1500), time::milliseconds(500), bind(&SchedulerFixture::event4, this)); |
Alexander Afanasyev | 83cdc5b | 2014-03-03 16:17:47 +0000 | [diff] [blame] | 88 | scheduler.scheduleEvent(time::seconds(4), bind(&Scheduler::cancelEvent, &scheduler, i)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 90 | io.run(); |
| 91 | |
| 92 | BOOST_CHECK_EQUAL(count1, 1); |
| 93 | BOOST_CHECK_EQUAL(count2, 0); |
| 94 | BOOST_CHECK_EQUAL(count3, 1); |
Alexander Afanasyev | 83cdc5b | 2014-03-03 16:17:47 +0000 | [diff] [blame] | 95 | BOOST_CHECK_GE(count4, 2); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(CancelEmptyEvent) |
| 99 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 100 | boost::asio::io_service io; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 101 | Scheduler scheduler(io); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 102 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 103 | EventId i; |
| 104 | scheduler.cancelEvent(i); |
| 105 | } |
| 106 | |
| 107 | struct SelfCancelFixture |
| 108 | { |
| 109 | SelfCancelFixture() |
| 110 | : m_scheduler(m_io) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | cancelSelf() |
| 116 | { |
| 117 | m_scheduler.cancelEvent(m_selfEventId); |
| 118 | } |
| 119 | |
| 120 | boost::asio::io_service m_io; |
| 121 | Scheduler m_scheduler; |
| 122 | EventId m_selfEventId; |
| 123 | }; |
| 124 | |
| 125 | BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture) |
| 126 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 127 | m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100), |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 128 | bind(&SelfCancelFixture::cancelSelf, this)); |
| 129 | |
| 130 | BOOST_REQUIRE_NO_THROW(m_io.run()); |
| 131 | } |
| 132 | |
| 133 | struct SelfRescheduleFixture |
| 134 | { |
| 135 | SelfRescheduleFixture() |
| 136 | : m_scheduler(m_io) |
| 137 | , m_count(0) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | reschedule() |
| 143 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 144 | EventId eventId = m_scheduler.scheduleEvent(time::milliseconds(100), |
| 145 | bind(&SelfRescheduleFixture::reschedule, this)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 146 | m_scheduler.cancelEvent(m_selfEventId); |
| 147 | m_selfEventId = eventId; |
| 148 | |
| 149 | if(m_count < 5) |
| 150 | m_count++; |
| 151 | else |
| 152 | m_scheduler.cancelEvent(m_selfEventId); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | reschedule2() |
| 157 | { |
| 158 | m_scheduler.cancelEvent(m_selfEventId); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 159 | |
| 160 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 161 | if(m_count < 5) |
| 162 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 163 | m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100), |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 164 | bind(&SelfRescheduleFixture::reschedule2, this)); |
| 165 | m_count++; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | doNothing() |
| 171 | { |
| 172 | m_count++; |
| 173 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 174 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 175 | void |
| 176 | reschedule3() |
| 177 | { |
| 178 | m_scheduler.cancelEvent(m_selfEventId); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 179 | |
| 180 | m_scheduler.scheduleEvent(time::milliseconds(100), |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 181 | bind(&SelfRescheduleFixture::doNothing, this)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 182 | m_scheduler.scheduleEvent(time::milliseconds(100), |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 183 | bind(&SelfRescheduleFixture::doNothing, this)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 184 | m_scheduler.scheduleEvent(time::milliseconds(100), |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 185 | bind(&SelfRescheduleFixture::doNothing, this)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 186 | m_scheduler.scheduleEvent(time::milliseconds(100), |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 187 | bind(&SelfRescheduleFixture::doNothing, this)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 188 | m_scheduler.scheduleEvent(time::milliseconds(100), |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 189 | bind(&SelfRescheduleFixture::doNothing, this)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 190 | m_scheduler.scheduleEvent(time::milliseconds(100), |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 191 | bind(&SelfRescheduleFixture::doNothing, this)); |
| 192 | } |
| 193 | |
| 194 | boost::asio::io_service m_io; |
| 195 | Scheduler m_scheduler; |
| 196 | EventId m_selfEventId; |
| 197 | int m_count; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 198 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture) |
| 202 | { |
| 203 | m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0), |
| 204 | bind(&SelfRescheduleFixture::reschedule, this)); |
| 205 | |
| 206 | BOOST_REQUIRE_NO_THROW(m_io.run()); |
| 207 | |
| 208 | BOOST_CHECK_EQUAL(m_count, 5); |
| 209 | } |
| 210 | |
| 211 | BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture) |
| 212 | { |
| 213 | m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0), |
| 214 | bind(&SelfRescheduleFixture::reschedule2, this)); |
| 215 | |
| 216 | BOOST_REQUIRE_NO_THROW(m_io.run()); |
| 217 | |
| 218 | BOOST_CHECK_EQUAL(m_count, 5); |
| 219 | } |
| 220 | |
| 221 | BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture) |
| 222 | { |
| 223 | m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0), |
| 224 | bind(&SelfRescheduleFixture::reschedule3, this)); |
| 225 | |
| 226 | BOOST_REQUIRE_NO_THROW(m_io.run()); |
| 227 | |
| 228 | BOOST_CHECK_EQUAL(m_count, 6); |
| 229 | } |
| 230 | |
| 231 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 232 | BOOST_AUTO_TEST_SUITE_END() |
| 233 | |
| 234 | } // namespace ndn |