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