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