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