Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 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 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 45 | BOOST_FIXTURE_TEST_SUITE(TestScheduler, 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 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 77 | public: |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 78 | int count1; |
| 79 | int count2; |
| 80 | int count3; |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture) |
| 84 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 85 | scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this)); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 87 | EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 88 | scheduler::cancel(i); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 90 | scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this)); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 92 | i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 93 | scheduler::cancel(i); |
| 94 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 95 | g_io.run(); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 96 | |
| 97 | BOOST_CHECK_EQUAL(count1, 1); |
| 98 | BOOST_CHECK_EQUAL(count2, 0); |
| 99 | BOOST_CHECK_EQUAL(count3, 1); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 102 | BOOST_AUTO_TEST_CASE(CancelEmptyEvent) |
| 103 | { |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 104 | EventId i; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 105 | scheduler::cancel(i); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 106 | |
| 107 | // Trivial check to avoid "test case did not check any assertions" message from Boost.Test |
| 108 | BOOST_CHECK(true); |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 111 | class SelfCancelFixture : protected BaseFixture |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 112 | { |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 113 | public: |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 114 | void |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 115 | cancelSelf() const |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 116 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 117 | scheduler::cancel(m_selfEventId); |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 118 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 119 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 120 | public: |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 121 | EventId m_selfEventId; |
| 122 | }; |
| 123 | |
| 124 | BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture) |
| 125 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 126 | m_selfEventId = scheduler::schedule(time::milliseconds(100), |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 127 | bind(&SelfCancelFixture::cancelSelf, this)); |
| 128 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 129 | BOOST_REQUIRE_NO_THROW(g_io.run()); |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 132 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdDestruct, UnitTestTimeFixture) |
| 133 | { |
| 134 | int hit = 0; |
| 135 | { |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 136 | ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 137 | } // se goes out of scope |
| 138 | this->advanceClocks(time::milliseconds(1), 15); |
| 139 | BOOST_CHECK_EQUAL(hit, 0); |
| 140 | } |
| 141 | |
| 142 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdAssign, UnitTestTimeFixture) |
| 143 | { |
| 144 | int hit1 = 0, hit2 = 0; |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 145 | ScopedEventId se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit1; }); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 146 | se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit2; }); |
| 147 | this->advanceClocks(time::milliseconds(1), 15); |
| 148 | BOOST_CHECK_EQUAL(hit1, 0); |
| 149 | BOOST_CHECK_EQUAL(hit2, 1); |
| 150 | } |
| 151 | |
| 152 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdRelease, UnitTestTimeFixture) |
| 153 | { |
| 154 | int hit = 0; |
| 155 | { |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 156 | ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 157 | se.release(); |
| 158 | } // se goes out of scope |
| 159 | this->advanceClocks(time::milliseconds(1), 15); |
| 160 | BOOST_CHECK_EQUAL(hit, 1); |
| 161 | } |
| 162 | |
| 163 | BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture) |
| 164 | { |
| 165 | int hit = 0; |
| 166 | unique_ptr<scheduler::ScopedEventId> se2; |
| 167 | { |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 168 | ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); |
| 169 | se2.reset(new ScopedEventId(std::move(se))); |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 170 | } // se goes out of scope |
| 171 | this->advanceClocks(time::milliseconds(1), 15); |
| 172 | BOOST_CHECK_EQUAL(hit, 1); |
| 173 | } |
| 174 | |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 175 | BOOST_AUTO_TEST_CASE(ThreadLocalScheduler) |
| 176 | { |
| 177 | scheduler::Scheduler* s1 = &scheduler::getGlobalScheduler(); |
| 178 | scheduler::Scheduler* s2 = nullptr; |
| 179 | boost::thread t([&s2] { |
| 180 | s2 = &scheduler::getGlobalScheduler(); |
| 181 | }); |
| 182 | |
| 183 | t.join(); |
| 184 | |
| 185 | BOOST_CHECK(s1 != nullptr); |
| 186 | BOOST_CHECK(s2 != nullptr); |
| 187 | BOOST_CHECK(s1 != s2); |
| 188 | } |
| 189 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 190 | BOOST_AUTO_TEST_SUITE_END() // TestScheduler |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 191 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 192 | } // namespace tests |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 193 | } // namespace nfd |