Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame^] | 24 | */ |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 25 | |
| 26 | #include "core/scheduler.hpp" |
| 27 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 30 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 31 | namespace tests { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 32 | |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame^] | 33 | using scheduler::EventId; |
| 34 | using scheduler::ScopedEventId; |
| 35 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(CoreScheduler, BaseFixture) |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 37 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 38 | class SchedulerFixture : protected BaseFixture |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 39 | { |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 40 | public: |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 41 | SchedulerFixture() |
| 42 | : count1(0) |
| 43 | , count2(0) |
| 44 | , count3(0) |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 45 | { |
| 46 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 48 | void |
| 49 | event1() |
| 50 | { |
| 51 | BOOST_CHECK_EQUAL(count3, 1); |
| 52 | ++count1; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | event2() |
| 57 | { |
| 58 | ++count2; |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | event3() |
| 63 | { |
| 64 | BOOST_CHECK_EQUAL(count1, 0); |
| 65 | ++count3; |
| 66 | } |
| 67 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 68 | int count1; |
| 69 | int count2; |
| 70 | int count3; |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture) |
| 74 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 75 | scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this)); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 77 | EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 78 | scheduler::cancel(i); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 80 | scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this)); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 81 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 82 | i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 83 | scheduler::cancel(i); |
| 84 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 85 | g_io.run(); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 86 | |
| 87 | BOOST_CHECK_EQUAL(count1, 1); |
| 88 | BOOST_CHECK_EQUAL(count2, 0); |
| 89 | BOOST_CHECK_EQUAL(count3, 1); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 92 | BOOST_AUTO_TEST_CASE(CancelEmptyEvent) |
| 93 | { |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 94 | EventId i; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 95 | scheduler::cancel(i); |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 98 | class SelfCancelFixture : protected BaseFixture |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 99 | { |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 100 | public: |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 101 | void |
| 102 | cancelSelf() |
| 103 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 104 | scheduler::cancel(m_selfEventId); |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 105 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 106 | |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 107 | EventId m_selfEventId; |
| 108 | }; |
| 109 | |
| 110 | BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture) |
| 111 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 112 | m_selfEventId = scheduler::schedule(time::milliseconds(100), |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 113 | bind(&SelfCancelFixture::cancelSelf, this)); |
| 114 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 115 | BOOST_REQUIRE_NO_THROW(g_io.run()); |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 118 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdDestruct, UnitTestTimeFixture) |
| 119 | { |
| 120 | int hit = 0; |
| 121 | { |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame^] | 122 | ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 123 | } // se goes out of scope |
| 124 | this->advanceClocks(time::milliseconds(1), 15); |
| 125 | BOOST_CHECK_EQUAL(hit, 0); |
| 126 | } |
| 127 | |
| 128 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdAssign, UnitTestTimeFixture) |
| 129 | { |
| 130 | int hit1 = 0, hit2 = 0; |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame^] | 131 | ScopedEventId se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit1; }); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 132 | se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit2; }); |
| 133 | this->advanceClocks(time::milliseconds(1), 15); |
| 134 | BOOST_CHECK_EQUAL(hit1, 0); |
| 135 | BOOST_CHECK_EQUAL(hit2, 1); |
| 136 | } |
| 137 | |
| 138 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdRelease, UnitTestTimeFixture) |
| 139 | { |
| 140 | int hit = 0; |
| 141 | { |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame^] | 142 | ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 143 | se.release(); |
| 144 | } // se goes out of scope |
| 145 | this->advanceClocks(time::milliseconds(1), 15); |
| 146 | BOOST_CHECK_EQUAL(hit, 1); |
| 147 | } |
| 148 | |
| 149 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture) |
| 150 | { |
| 151 | int hit = 0; |
| 152 | unique_ptr<scheduler::ScopedEventId> se2; |
| 153 | { |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame^] | 154 | ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); |
| 155 | se2.reset(new ScopedEventId(std::move(se))); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 156 | } // se goes out of scope |
| 157 | this->advanceClocks(time::milliseconds(1), 15); |
| 158 | BOOST_CHECK_EQUAL(hit, 1); |
| 159 | } |
| 160 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 161 | BOOST_AUTO_TEST_SUITE_END() |
| 162 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 163 | } // namespace tests |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 164 | } // namespace nfd |