blob: dcafac216dd4d8686847a2751c0e3e97c8437895 [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 Afanasyev18bbf812014-01-29 01:40:23 -080030namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031namespace tests {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080032
Junxiao Shi1e46be32015-01-08 20:18:05 -070033using scheduler::EventId;
34using scheduler::ScopedEventId;
35
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036BOOST_FIXTURE_TEST_SUITE(CoreScheduler, BaseFixture)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080037
Junxiao Shi98e29f42014-03-31 10:27:26 -070038class SchedulerFixture : protected BaseFixture
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080039{
Junxiao Shi98e29f42014-03-31 10:27:26 -070040public:
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080041 SchedulerFixture()
42 : count1(0)
43 , count2(0)
44 , count3(0)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080045 {
46 }
Junxiao Shic041ca32014-02-25 20:01:15 -070047
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080048 void
49 event1()
50 {
51 BOOST_CHECK_EQUAL(count3, 1);
52 ++count1;
53 }
54
55 void
56 event2()
57 {
58 ++count2;
59 }
60
61 void
62 event3()
63 {
64 BOOST_CHECK_EQUAL(count1, 0);
65 ++count3;
66 }
67
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080068 int count1;
69 int count2;
70 int count3;
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080071};
72
73BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
74{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070075 scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080076
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070077 EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070078 scheduler::cancel(i);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080079
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070080 scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080081
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070082 i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070083 scheduler::cancel(i);
84
Junxiao Shid9ee45c2014-02-27 15:38:11 -070085 g_io.run();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080086
87 BOOST_CHECK_EQUAL(count1, 1);
88 BOOST_CHECK_EQUAL(count2, 0);
89 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080090}
91
Junxiao Shi234a5322014-01-30 22:40:48 -070092BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
93{
Junxiao Shi234a5322014-01-30 22:40:48 -070094 EventId i;
Junxiao Shic041ca32014-02-25 20:01:15 -070095 scheduler::cancel(i);
Junxiao Shi234a5322014-01-30 22:40:48 -070096}
97
Junxiao Shi98e29f42014-03-31 10:27:26 -070098class SelfCancelFixture : protected BaseFixture
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080099{
Junxiao Shi98e29f42014-03-31 10:27:26 -0700100public:
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800101 void
102 cancelSelf()
103 {
Junxiao Shic041ca32014-02-25 20:01:15 -0700104 scheduler::cancel(m_selfEventId);
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800105 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700106
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800107 EventId m_selfEventId;
108};
109
110BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
111{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700112 m_selfEventId = scheduler::schedule(time::milliseconds(100),
Junxiao Shic041ca32014-02-25 20:01:15 -0700113 bind(&SelfCancelFixture::cancelSelf, this));
114
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700115 BOOST_REQUIRE_NO_THROW(g_io.run());
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800116}
117
Junxiao Shi27533da2014-12-15 10:56:20 -0700118BOOST_FIXTURE_TEST_CASE(ScopedEventIdDestruct, UnitTestTimeFixture)
119{
120 int hit = 0;
121 {
Junxiao Shi1e46be32015-01-08 20:18:05 -0700122 ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
Junxiao Shi27533da2014-12-15 10:56:20 -0700123 } // se goes out of scope
124 this->advanceClocks(time::milliseconds(1), 15);
125 BOOST_CHECK_EQUAL(hit, 0);
126}
127
128BOOST_FIXTURE_TEST_CASE(ScopedEventIdAssign, UnitTestTimeFixture)
129{
130 int hit1 = 0, hit2 = 0;
Junxiao Shi1e46be32015-01-08 20:18:05 -0700131 ScopedEventId se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit1; });
Junxiao Shi27533da2014-12-15 10:56:20 -0700132 se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit2; });
133 this->advanceClocks(time::milliseconds(1), 15);
134 BOOST_CHECK_EQUAL(hit1, 0);
135 BOOST_CHECK_EQUAL(hit2, 1);
136}
137
138BOOST_FIXTURE_TEST_CASE(ScopedEventIdRelease, UnitTestTimeFixture)
139{
140 int hit = 0;
141 {
Junxiao Shi1e46be32015-01-08 20:18:05 -0700142 ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
Junxiao Shi27533da2014-12-15 10:56:20 -0700143 se.release();
144 } // se goes out of scope
145 this->advanceClocks(time::milliseconds(1), 15);
146 BOOST_CHECK_EQUAL(hit, 1);
147}
148
149BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture)
150{
151 int hit = 0;
152 unique_ptr<scheduler::ScopedEventId> se2;
153 {
Junxiao Shi1e46be32015-01-08 20:18:05 -0700154 ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
155 se2.reset(new ScopedEventId(std::move(se)));
Junxiao Shi27533da2014-12-15 10:56:20 -0700156 } // se goes out of scope
157 this->advanceClocks(time::milliseconds(1), 15);
158 BOOST_CHECK_EQUAL(hit, 1);
159}
160
Alexander Afanasyev920af2f2014-01-25 22:56:11 -0800161BOOST_AUTO_TEST_SUITE_END()
162
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700163} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800164} // namespace nfd