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