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 | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 30 | #include <boost/thread.hpp> |
| 31 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 32 | namespace nfd { |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 33 | |
| 34 | namespace scheduler { |
| 35 | // defined in scheduler.cpp |
| 36 | Scheduler& |
| 37 | getGlobalScheduler(); |
| 38 | } // namespace scheduler |
| 39 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 40 | namespace tests { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 41 | |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 42 | using scheduler::EventId; |
| 43 | using scheduler::ScopedEventId; |
| 44 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 45 | BOOST_FIXTURE_TEST_SUITE(CoreScheduler, BaseFixture) |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 46 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 47 | class SchedulerFixture : protected BaseFixture |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 48 | { |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 49 | public: |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 50 | SchedulerFixture() |
| 51 | : count1(0) |
| 52 | , count2(0) |
| 53 | , count3(0) |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 54 | { |
| 55 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 57 | void |
| 58 | event1() |
| 59 | { |
| 60 | BOOST_CHECK_EQUAL(count3, 1); |
| 61 | ++count1; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | event2() |
| 66 | { |
| 67 | ++count2; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | event3() |
| 72 | { |
| 73 | BOOST_CHECK_EQUAL(count1, 0); |
| 74 | ++count3; |
| 75 | } |
| 76 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 77 | int count1; |
| 78 | int count2; |
| 79 | int count3; |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture) |
| 83 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 84 | scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this)); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 86 | EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 87 | scheduler::cancel(i); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 89 | scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this)); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 91 | i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 92 | scheduler::cancel(i); |
| 93 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 94 | g_io.run(); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 95 | |
| 96 | BOOST_CHECK_EQUAL(count1, 1); |
| 97 | BOOST_CHECK_EQUAL(count2, 0); |
| 98 | BOOST_CHECK_EQUAL(count3, 1); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 101 | BOOST_AUTO_TEST_CASE(CancelEmptyEvent) |
| 102 | { |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 103 | EventId i; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 104 | scheduler::cancel(i); |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 107 | class SelfCancelFixture : protected BaseFixture |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 108 | { |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 109 | public: |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 110 | void |
| 111 | cancelSelf() |
| 112 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 113 | scheduler::cancel(m_selfEventId); |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 114 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 115 | |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 116 | EventId m_selfEventId; |
| 117 | }; |
| 118 | |
| 119 | BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture) |
| 120 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 121 | m_selfEventId = scheduler::schedule(time::milliseconds(100), |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 122 | bind(&SelfCancelFixture::cancelSelf, this)); |
| 123 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 124 | BOOST_REQUIRE_NO_THROW(g_io.run()); |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 127 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdDestruct, UnitTestTimeFixture) |
| 128 | { |
| 129 | int hit = 0; |
| 130 | { |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 131 | ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 132 | } // se goes out of scope |
| 133 | this->advanceClocks(time::milliseconds(1), 15); |
| 134 | BOOST_CHECK_EQUAL(hit, 0); |
| 135 | } |
| 136 | |
| 137 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdAssign, UnitTestTimeFixture) |
| 138 | { |
| 139 | int hit1 = 0, hit2 = 0; |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 140 | ScopedEventId se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit1; }); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 141 | se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit2; }); |
| 142 | this->advanceClocks(time::milliseconds(1), 15); |
| 143 | BOOST_CHECK_EQUAL(hit1, 0); |
| 144 | BOOST_CHECK_EQUAL(hit2, 1); |
| 145 | } |
| 146 | |
| 147 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdRelease, UnitTestTimeFixture) |
| 148 | { |
| 149 | int hit = 0; |
| 150 | { |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 151 | ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 152 | se.release(); |
| 153 | } // se goes out of scope |
| 154 | this->advanceClocks(time::milliseconds(1), 15); |
| 155 | BOOST_CHECK_EQUAL(hit, 1); |
| 156 | } |
| 157 | |
| 158 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture) |
| 159 | { |
| 160 | int hit = 0; |
| 161 | unique_ptr<scheduler::ScopedEventId> se2; |
| 162 | { |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 163 | ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); |
| 164 | se2.reset(new ScopedEventId(std::move(se))); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 165 | } // se goes out of scope |
| 166 | this->advanceClocks(time::milliseconds(1), 15); |
| 167 | BOOST_CHECK_EQUAL(hit, 1); |
| 168 | } |
| 169 | |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 170 | BOOST_AUTO_TEST_CASE(ThreadLocalScheduler) |
| 171 | { |
| 172 | scheduler::Scheduler* s1 = &scheduler::getGlobalScheduler(); |
| 173 | scheduler::Scheduler* s2 = nullptr; |
| 174 | boost::thread t([&s2] { |
| 175 | s2 = &scheduler::getGlobalScheduler(); |
| 176 | }); |
| 177 | |
| 178 | t.join(); |
| 179 | |
| 180 | BOOST_CHECK(s1 != nullptr); |
| 181 | BOOST_CHECK(s2 != nullptr); |
| 182 | BOOST_CHECK(s1 != s2); |
| 183 | } |
| 184 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 185 | BOOST_AUTO_TEST_SUITE_END() |
| 186 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 187 | } // namespace tests |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 188 | } // namespace nfd |