blob: 15c02a9433bcb2d48093a0d171156402547d616c [file] [log] [blame]
Alexander Afanasyev920af2f2014-01-25 22:56:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * 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 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
Junxiao Shid9ee45c2014-02-27 15:38:11 -070045BOOST_FIXTURE_TEST_SUITE(CoreScheduler, 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
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080077 int count1;
78 int count2;
79 int count3;
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080080};
81
82BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
83{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070084 scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080085
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070086 EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070087 scheduler::cancel(i);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080088
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070089 scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080090
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070091 i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070092 scheduler::cancel(i);
93
Junxiao Shid9ee45c2014-02-27 15:38:11 -070094 g_io.run();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080095
96 BOOST_CHECK_EQUAL(count1, 1);
97 BOOST_CHECK_EQUAL(count2, 0);
98 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080099}
100
Junxiao Shi234a5322014-01-30 22:40:48 -0700101BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
102{
Junxiao Shi234a5322014-01-30 22:40:48 -0700103 EventId i;
Junxiao Shic041ca32014-02-25 20:01:15 -0700104 scheduler::cancel(i);
Junxiao Shi234a5322014-01-30 22:40:48 -0700105}
106
Junxiao Shi98e29f42014-03-31 10:27:26 -0700107class SelfCancelFixture : protected BaseFixture
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800108{
Junxiao Shi98e29f42014-03-31 10:27:26 -0700109public:
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800110 void
111 cancelSelf()
112 {
Junxiao Shic041ca32014-02-25 20:01:15 -0700113 scheduler::cancel(m_selfEventId);
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800114 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700115
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800116 EventId m_selfEventId;
117};
118
119BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
120{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700121 m_selfEventId = scheduler::schedule(time::milliseconds(100),
Junxiao Shic041ca32014-02-25 20:01:15 -0700122 bind(&SelfCancelFixture::cancelSelf, this));
123
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700124 BOOST_REQUIRE_NO_THROW(g_io.run());
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800125}
126
Junxiao Shi27533da2014-12-15 10:56:20 -0700127BOOST_FIXTURE_TEST_CASE(ScopedEventIdDestruct, UnitTestTimeFixture)
128{
129 int hit = 0;
130 {
Junxiao Shi1e46be32015-01-08 20:18:05 -0700131 ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
Junxiao Shi27533da2014-12-15 10:56:20 -0700132 } // se goes out of scope
133 this->advanceClocks(time::milliseconds(1), 15);
134 BOOST_CHECK_EQUAL(hit, 0);
135}
136
137BOOST_FIXTURE_TEST_CASE(ScopedEventIdAssign, UnitTestTimeFixture)
138{
139 int hit1 = 0, hit2 = 0;
Junxiao Shi1e46be32015-01-08 20:18:05 -0700140 ScopedEventId se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit1; });
Junxiao Shi27533da2014-12-15 10:56:20 -0700141 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
147BOOST_FIXTURE_TEST_CASE(ScopedEventIdRelease, UnitTestTimeFixture)
148{
149 int hit = 0;
150 {
Junxiao Shi1e46be32015-01-08 20:18:05 -0700151 ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
Junxiao Shi27533da2014-12-15 10:56:20 -0700152 se.release();
153 } // se goes out of scope
154 this->advanceClocks(time::milliseconds(1), 15);
155 BOOST_CHECK_EQUAL(hit, 1);
156}
157
158BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture)
159{
160 int hit = 0;
161 unique_ptr<scheduler::ScopedEventId> se2;
162 {
Junxiao Shi1e46be32015-01-08 20:18:05 -0700163 ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
164 se2.reset(new ScopedEventId(std::move(se)));
Junxiao Shi27533da2014-12-15 10:56:20 -0700165 } // se goes out of scope
166 this->advanceClocks(time::milliseconds(1), 15);
167 BOOST_CHECK_EQUAL(hit, 1);
168}
169
Alexander Afanasyev5d57f972015-02-11 21:04:29 -0800170BOOST_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 Afanasyev920af2f2014-01-25 22:56:11 -0800185BOOST_AUTO_TEST_SUITE_END()
186
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700187} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800188} // namespace nfd