Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "core/scheduler.hpp" |
| 8 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 9 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 11 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 12 | namespace tests { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 13 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 14 | BOOST_FIXTURE_TEST_SUITE(CoreScheduler, BaseFixture) |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 15 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 16 | struct SchedulerFixture : protected BaseFixture |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 17 | { |
| 18 | SchedulerFixture() |
| 19 | : count1(0) |
| 20 | , count2(0) |
| 21 | , count3(0) |
| 22 | , count4(0) |
| 23 | { |
| 24 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 26 | void |
| 27 | event1() |
| 28 | { |
| 29 | BOOST_CHECK_EQUAL(count3, 1); |
| 30 | ++count1; |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | event2() |
| 35 | { |
| 36 | ++count2; |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | event3() |
| 41 | { |
| 42 | BOOST_CHECK_EQUAL(count1, 0); |
| 43 | ++count3; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | event4() |
| 48 | { |
| 49 | ++count4; |
| 50 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 52 | int count1; |
| 53 | int count2; |
| 54 | int count3; |
| 55 | int count4; |
| 56 | }; |
| 57 | |
| 58 | BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture) |
| 59 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 60 | scheduler::schedule(time::seconds(0.5), bind(&SchedulerFixture::event1, this)); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 61 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 62 | EventId i = scheduler::schedule(time::seconds(1.0), bind(&SchedulerFixture::event2, this)); |
| 63 | scheduler::cancel(i); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 64 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 65 | scheduler::schedule(time::seconds(0.25), bind(&SchedulerFixture::event3, this)); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 66 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 67 | i = scheduler::schedule(time::seconds(0.05), bind(&SchedulerFixture::event2, this)); |
| 68 | scheduler::cancel(i); |
| 69 | |
| 70 | // TODO deprecate periodic event |
| 71 | i = scheduler::getGlobalScheduler().schedulePeriodicEvent(time::seconds(0.3), time::seconds(0.1), bind(&SchedulerFixture::event4, this)); |
| 72 | scheduler::schedule(time::seconds(1), bind(&scheduler::cancel, i)); |
| 73 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 74 | g_io.run(); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 75 | |
| 76 | BOOST_CHECK_EQUAL(count1, 1); |
| 77 | BOOST_CHECK_EQUAL(count2, 0); |
| 78 | BOOST_CHECK_EQUAL(count3, 1); |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 79 | BOOST_CHECK_GT(count4, 1); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 82 | BOOST_AUTO_TEST_CASE(CancelEmptyEvent) |
| 83 | { |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 84 | EventId i; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 85 | scheduler::cancel(i); |
Junxiao Shi | 234a532 | 2014-01-30 22:40:48 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 88 | struct SelfCancelFixture : protected BaseFixture |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 89 | { |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 90 | void |
| 91 | cancelSelf() |
| 92 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 93 | scheduler::cancel(m_selfEventId); |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 94 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 96 | EventId m_selfEventId; |
| 97 | }; |
| 98 | |
| 99 | BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture) |
| 100 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 101 | m_selfEventId = scheduler::schedule(time::seconds(0.1), |
| 102 | bind(&SelfCancelFixture::cancelSelf, this)); |
| 103 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 104 | BOOST_REQUIRE_NO_THROW(g_io.run()); |
Alexander Afanasyev | 94ceb12 | 2014-02-03 14:47:57 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 107 | BOOST_AUTO_TEST_SUITE_END() |
| 108 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 109 | } // namespace tests |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 110 | } // namespace nfd |