blob: 88adb038d1691b9dfb7f80c48da3f2ca6af72235 [file] [log] [blame]
Alexander Afanasyev920af2f2014-01-25 22:56:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifeddc3c2019-01-17 19:06:00 +00002/*
3 * Copyright (c) 2014-2019, 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 -080033namespace scheduler {
34// defined in scheduler.cpp
35Scheduler&
36getGlobalScheduler();
37} // namespace scheduler
38
Junxiao Shid9ee45c2014-02-27 15:38:11 -070039namespace tests {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080040
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080041BOOST_FIXTURE_TEST_SUITE(TestScheduler, BaseFixture)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080042
Junxiao Shifeddc3c2019-01-17 19:06:00 +000043BOOST_AUTO_TEST_CASE(ScheduleCancel)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080044{
Junxiao Shifeddc3c2019-01-17 19:06:00 +000045 int count1 = 0, count2 = 0, count3 = 0;
Junxiao Shic041ca32014-02-25 20:01:15 -070046
Junxiao Shifeddc3c2019-01-17 19:06:00 +000047 scheduler::schedule(500_ms, [&] {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080048 BOOST_CHECK_EQUAL(count3, 1);
49 ++count1;
Junxiao Shifeddc3c2019-01-17 19:06:00 +000050 });
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080051
Junxiao Shifeddc3c2019-01-17 19:06:00 +000052 scheduler::EventId eid = scheduler::schedule(1_s, [&] { ++count2; });
53 scheduler::cancel(eid);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080054
Junxiao Shifeddc3c2019-01-17 19:06:00 +000055 scheduler::schedule(250_ms, [&] {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080056 BOOST_CHECK_EQUAL(count1, 0);
57 ++count3;
Junxiao Shifeddc3c2019-01-17 19:06:00 +000058 });
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080059
Junxiao Shifeddc3c2019-01-17 19:06:00 +000060 {
61 scheduler::ScopedEventId se = scheduler::schedule(50_ms, [&] { ++count2; });
62 } // se goes out of scope, canceling the event
Junxiao Shic041ca32014-02-25 20:01:15 -070063
Junxiao Shid9ee45c2014-02-27 15:38:11 -070064 g_io.run();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080065
66 BOOST_CHECK_EQUAL(count1, 1);
67 BOOST_CHECK_EQUAL(count2, 0);
68 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080069}
70
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080071BOOST_AUTO_TEST_CASE(ThreadLocalScheduler)
72{
73 scheduler::Scheduler* s1 = &scheduler::getGlobalScheduler();
74 scheduler::Scheduler* s2 = nullptr;
Junxiao Shifeddc3c2019-01-17 19:06:00 +000075 boost::thread t([&s2] { s2 = &scheduler::getGlobalScheduler(); });
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080076 t.join();
77
78 BOOST_CHECK(s1 != nullptr);
79 BOOST_CHECK(s2 != nullptr);
80 BOOST_CHECK(s1 != s2);
81}
82
Davide Pesavento97210d52016-10-14 15:45:48 +020083BOOST_AUTO_TEST_SUITE_END() // TestScheduler
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080084
Junxiao Shid9ee45c2014-02-27 15:38:11 -070085} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080086} // namespace nfd