blob: 6d648b90df8906657f6a7ea5e6f8ba38ae210c33 [file] [log] [blame]
Alexander Afanasyev920af2f2014-01-25 22:56:11 -08001/* -*- 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 Shid9ee45c2014-02-27 15:38:11 -07009#include "tests/test-common.hpp"
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080010
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080011namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070012namespace tests {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080013
Junxiao Shid9ee45c2014-02-27 15:38:11 -070014BOOST_FIXTURE_TEST_SUITE(CoreScheduler, BaseFixture)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080015
Junxiao Shi98e29f42014-03-31 10:27:26 -070016class SchedulerFixture : protected BaseFixture
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080017{
Junxiao Shi98e29f42014-03-31 10:27:26 -070018public:
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080019 SchedulerFixture()
20 : count1(0)
21 , count2(0)
22 , count3(0)
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080023 {
24 }
Junxiao Shic041ca32014-02-25 20:01:15 -070025
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080026 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
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080046 int count1;
47 int count2;
48 int count3;
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080049};
50
51BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
52{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070053 scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080054
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070055 EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070056 scheduler::cancel(i);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080057
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070058 scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080059
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070060 i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070061 scheduler::cancel(i);
62
Junxiao Shid9ee45c2014-02-27 15:38:11 -070063 g_io.run();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080064
65 BOOST_CHECK_EQUAL(count1, 1);
66 BOOST_CHECK_EQUAL(count2, 0);
67 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080068}
69
Junxiao Shi234a5322014-01-30 22:40:48 -070070BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
71{
Junxiao Shi234a5322014-01-30 22:40:48 -070072 EventId i;
Junxiao Shic041ca32014-02-25 20:01:15 -070073 scheduler::cancel(i);
Junxiao Shi234a5322014-01-30 22:40:48 -070074}
75
Junxiao Shi98e29f42014-03-31 10:27:26 -070076class SelfCancelFixture : protected BaseFixture
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080077{
Junxiao Shi98e29f42014-03-31 10:27:26 -070078public:
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080079 void
80 cancelSelf()
81 {
Junxiao Shic041ca32014-02-25 20:01:15 -070082 scheduler::cancel(m_selfEventId);
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080083 }
Junxiao Shic041ca32014-02-25 20:01:15 -070084
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080085 EventId m_selfEventId;
86};
87
88BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
89{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070090 m_selfEventId = scheduler::schedule(time::milliseconds(100),
Junxiao Shic041ca32014-02-25 20:01:15 -070091 bind(&SelfCancelFixture::cancelSelf, this));
92
Junxiao Shid9ee45c2014-02-27 15:38:11 -070093 BOOST_REQUIRE_NO_THROW(g_io.run());
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080094}
95
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080096BOOST_AUTO_TEST_SUITE_END()
97
Junxiao Shid9ee45c2014-02-27 15:38:11 -070098} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080099} // namespace nfd