blob: 954c31e21d5efc6d5f9cc95ea75da69f64bf5400 [file] [log] [blame]
Alexander Afanasyev920af2f2014-01-25 22:56:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 Afanasyev920af2f2014-01-25 22:56:11 -080024
25#include "core/scheduler.hpp"
26
Junxiao Shid9ee45c2014-02-27 15:38:11 -070027#include "tests/test-common.hpp"
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080028
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030namespace tests {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080031
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032BOOST_FIXTURE_TEST_SUITE(CoreScheduler, BaseFixture)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080033
Junxiao Shi98e29f42014-03-31 10:27:26 -070034class SchedulerFixture : protected BaseFixture
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080035{
Junxiao Shi98e29f42014-03-31 10:27:26 -070036public:
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080037 SchedulerFixture()
38 : count1(0)
39 , count2(0)
40 , count3(0)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080041 {
42 }
Junxiao Shic041ca32014-02-25 20:01:15 -070043
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080044 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 Afanasyev920af2f2014-01-25 22:56:11 -080064 int count1;
65 int count2;
66 int count3;
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080067};
68
69BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
70{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070071 scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080072
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070073 EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070074 scheduler::cancel(i);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080075
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070076 scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080077
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070078 i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070079 scheduler::cancel(i);
80
Junxiao Shid9ee45c2014-02-27 15:38:11 -070081 g_io.run();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080082
83 BOOST_CHECK_EQUAL(count1, 1);
84 BOOST_CHECK_EQUAL(count2, 0);
85 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080086}
87
Junxiao Shi234a5322014-01-30 22:40:48 -070088BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
89{
Junxiao Shi234a5322014-01-30 22:40:48 -070090 EventId i;
Junxiao Shic041ca32014-02-25 20:01:15 -070091 scheduler::cancel(i);
Junxiao Shi234a5322014-01-30 22:40:48 -070092}
93
Junxiao Shi98e29f42014-03-31 10:27:26 -070094class SelfCancelFixture : protected BaseFixture
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080095{
Junxiao Shi98e29f42014-03-31 10:27:26 -070096public:
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080097 void
98 cancelSelf()
99 {
Junxiao Shic041ca32014-02-25 20:01:15 -0700100 scheduler::cancel(m_selfEventId);
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800101 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700102
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800103 EventId m_selfEventId;
104};
105
106BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
107{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700108 m_selfEventId = scheduler::schedule(time::milliseconds(100),
Junxiao Shic041ca32014-02-25 20:01:15 -0700109 bind(&SelfCancelFixture::cancelSelf, this));
110
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700111 BOOST_REQUIRE_NO_THROW(g_io.run());
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800112}
113
Junxiao Shi27533da2014-12-15 10:56:20 -0700114BOOST_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
124BOOST_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
134BOOST_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
145BOOST_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 Afanasyev920af2f2014-01-25 22:56:11 -0800157BOOST_AUTO_TEST_SUITE_END()
158
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700159} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800160} // namespace nfd