blob: a2ad752677f7bb7fba1c1c615cbb2f5ad3f40a31 [file] [log] [blame]
Alexander Afanasyev920af2f2014-01-25 22:56:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento97210d52016-10-14 15:45:48 +02003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi1e46be32015-01-08 20:18:05 -070024 */
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080025
26#include "core/scheduler.hpp"
27
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080029
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080030#include <boost/thread.hpp>
31
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080033
34namespace scheduler {
35// defined in scheduler.cpp
36Scheduler&
37getGlobalScheduler();
38} // namespace scheduler
39
Junxiao Shid9ee45c2014-02-27 15:38:11 -070040namespace tests {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080041
Junxiao Shi1e46be32015-01-08 20:18:05 -070042using scheduler::EventId;
43using scheduler::ScopedEventId;
44
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080045BOOST_FIXTURE_TEST_SUITE(TestScheduler, BaseFixture)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080046
Junxiao Shi98e29f42014-03-31 10:27:26 -070047class SchedulerFixture : protected BaseFixture
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080048{
Junxiao Shi98e29f42014-03-31 10:27:26 -070049public:
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080050 SchedulerFixture()
51 : count1(0)
52 , count2(0)
53 , count3(0)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080054 {
55 }
Junxiao Shic041ca32014-02-25 20:01:15 -070056
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080057 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 Pesavento97210d52016-10-14 15:45:48 +020077public:
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080078 int count1;
79 int count2;
80 int count3;
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080081};
82
83BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
84{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070085 scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080086
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070087 EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070088 scheduler::cancel(i);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080089
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070090 scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080091
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070092 i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070093 scheduler::cancel(i);
94
Junxiao Shid9ee45c2014-02-27 15:38:11 -070095 g_io.run();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080096
97 BOOST_CHECK_EQUAL(count1, 1);
98 BOOST_CHECK_EQUAL(count2, 0);
99 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -0800100}
101
Junxiao Shi234a5322014-01-30 22:40:48 -0700102BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
103{
Junxiao Shi234a5322014-01-30 22:40:48 -0700104 EventId i;
Junxiao Shic041ca32014-02-25 20:01:15 -0700105 scheduler::cancel(i);
Davide Pesavento97210d52016-10-14 15:45:48 +0200106
107 // Trivial check to avoid "test case did not check any assertions" message from Boost.Test
108 BOOST_CHECK(true);
Junxiao Shi234a5322014-01-30 22:40:48 -0700109}
110
Junxiao Shi98e29f42014-03-31 10:27:26 -0700111class SelfCancelFixture : protected BaseFixture
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800112{
Junxiao Shi98e29f42014-03-31 10:27:26 -0700113public:
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800114 void
Davide Pesavento97210d52016-10-14 15:45:48 +0200115 cancelSelf() const
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800116 {
Junxiao Shic041ca32014-02-25 20:01:15 -0700117 scheduler::cancel(m_selfEventId);
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800118 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700119
Davide Pesavento97210d52016-10-14 15:45:48 +0200120public:
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800121 EventId m_selfEventId;
122};
123
124BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
125{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700126 m_selfEventId = scheduler::schedule(time::milliseconds(100),
Junxiao Shic041ca32014-02-25 20:01:15 -0700127 bind(&SelfCancelFixture::cancelSelf, this));
128
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700129 BOOST_REQUIRE_NO_THROW(g_io.run());
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800130}
131
Junxiao Shi27533da2014-12-15 10:56:20 -0700132BOOST_FIXTURE_TEST_CASE(ScopedEventIdDestruct, UnitTestTimeFixture)
133{
134 int hit = 0;
135 {
Junxiao Shi1e46be32015-01-08 20:18:05 -0700136 ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
Junxiao Shi27533da2014-12-15 10:56:20 -0700137 } // se goes out of scope
138 this->advanceClocks(time::milliseconds(1), 15);
139 BOOST_CHECK_EQUAL(hit, 0);
140}
141
142BOOST_FIXTURE_TEST_CASE(ScopedEventIdAssign, UnitTestTimeFixture)
143{
144 int hit1 = 0, hit2 = 0;
Junxiao Shi1e46be32015-01-08 20:18:05 -0700145 ScopedEventId se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit1; });
Junxiao Shi27533da2014-12-15 10:56:20 -0700146 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
152BOOST_FIXTURE_TEST_CASE(ScopedEventIdRelease, UnitTestTimeFixture)
153{
154 int hit = 0;
155 {
Junxiao Shi1e46be32015-01-08 20:18:05 -0700156 ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
Junxiao Shi27533da2014-12-15 10:56:20 -0700157 se.release();
158 } // se goes out of scope
159 this->advanceClocks(time::milliseconds(1), 15);
160 BOOST_CHECK_EQUAL(hit, 1);
161}
162
163BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture)
164{
165 int hit = 0;
166 unique_ptr<scheduler::ScopedEventId> se2;
167 {
Junxiao Shi1e46be32015-01-08 20:18:05 -0700168 ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
169 se2.reset(new ScopedEventId(std::move(se)));
Junxiao Shi27533da2014-12-15 10:56:20 -0700170 } // se goes out of scope
171 this->advanceClocks(time::milliseconds(1), 15);
172 BOOST_CHECK_EQUAL(hit, 1);
173}
174
Alexander Afanasyev5d57f972015-02-11 21:04:29 -0800175BOOST_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 Pesavento97210d52016-10-14 15:45:48 +0200190BOOST_AUTO_TEST_SUITE_END() // TestScheduler
Alexander Afanasyev920af2f2014-01-25 22:56:11 -0800191
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700192} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800193} // namespace nfd