Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 25 | |
| 26 | #include "core/scheduler.hpp" |
| 27 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 30 | #include <boost/thread.hpp> |
| 31 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 32 | namespace nfd { |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 33 | namespace scheduler { |
| 34 | // defined in scheduler.cpp |
| 35 | Scheduler& |
| 36 | getGlobalScheduler(); |
| 37 | } // namespace scheduler |
| 38 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 39 | namespace tests { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 40 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 41 | BOOST_FIXTURE_TEST_SUITE(TestScheduler, BaseFixture) |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 42 | |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 43 | BOOST_AUTO_TEST_CASE(ScheduleCancel) |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 44 | { |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 45 | int count1 = 0, count2 = 0, count3 = 0; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 46 | |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 47 | scheduler::schedule(500_ms, [&] { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(count3, 1); |
| 49 | ++count1; |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 50 | }); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 51 | |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 52 | scheduler::EventId eid = scheduler::schedule(1_s, [&] { ++count2; }); |
| 53 | scheduler::cancel(eid); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 54 | |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 55 | scheduler::schedule(250_ms, [&] { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(count1, 0); |
| 57 | ++count3; |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 58 | }); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 59 | |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 60 | { |
| 61 | scheduler::ScopedEventId se = scheduler::schedule(50_ms, [&] { ++count2; }); |
| 62 | } // se goes out of scope, canceling the event |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 63 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 64 | g_io.run(); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 65 | |
| 66 | BOOST_CHECK_EQUAL(count1, 1); |
| 67 | BOOST_CHECK_EQUAL(count2, 0); |
| 68 | BOOST_CHECK_EQUAL(count3, 1); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE(ThreadLocalScheduler) |
| 72 | { |
| 73 | scheduler::Scheduler* s1 = &scheduler::getGlobalScheduler(); |
| 74 | scheduler::Scheduler* s2 = nullptr; |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame^] | 75 | boost::thread t([&s2] { s2 = &scheduler::getGlobalScheduler(); }); |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 76 | t.join(); |
| 77 | |
| 78 | BOOST_CHECK(s1 != nullptr); |
| 79 | BOOST_CHECK(s2 != nullptr); |
| 80 | BOOST_CHECK(s1 != s2); |
| 81 | } |
| 82 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 83 | BOOST_AUTO_TEST_SUITE_END() // TestScheduler |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 84 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 85 | } // namespace tests |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 86 | } // namespace nfd |