blob: 806c982a8de0648aee0542dc1b3d119678d9fd61 [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 Shid9ee45c2014-02-27 15:38:11 -070016struct SchedulerFixture : protected BaseFixture
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080017{
18 SchedulerFixture()
19 : count1(0)
20 , count2(0)
21 , count3(0)
22 , count4(0)
23 {
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
46 void
47 event4()
48 {
49 ++count4;
50 }
Junxiao Shic041ca32014-02-25 20:01:15 -070051
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080052 int count1;
53 int count2;
54 int count3;
55 int count4;
56};
57
58BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
59{
Junxiao Shic041ca32014-02-25 20:01:15 -070060 scheduler::schedule(time::seconds(0.5), bind(&SchedulerFixture::event1, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080061
Junxiao Shic041ca32014-02-25 20:01:15 -070062 EventId i = scheduler::schedule(time::seconds(1.0), bind(&SchedulerFixture::event2, this));
63 scheduler::cancel(i);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080064
Junxiao Shic041ca32014-02-25 20:01:15 -070065 scheduler::schedule(time::seconds(0.25), bind(&SchedulerFixture::event3, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080066
Junxiao Shic041ca32014-02-25 20:01:15 -070067 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 Shid9ee45c2014-02-27 15:38:11 -070074 g_io.run();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080075
76 BOOST_CHECK_EQUAL(count1, 1);
77 BOOST_CHECK_EQUAL(count2, 0);
78 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -080079 BOOST_CHECK_GT(count4, 1);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080080}
81
Junxiao Shi234a5322014-01-30 22:40:48 -070082BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
83{
Junxiao Shi234a5322014-01-30 22:40:48 -070084 EventId i;
Junxiao Shic041ca32014-02-25 20:01:15 -070085 scheduler::cancel(i);
Junxiao Shi234a5322014-01-30 22:40:48 -070086}
87
Junxiao Shid9ee45c2014-02-27 15:38:11 -070088struct SelfCancelFixture : protected BaseFixture
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080089{
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080090 void
91 cancelSelf()
92 {
Junxiao Shic041ca32014-02-25 20:01:15 -070093 scheduler::cancel(m_selfEventId);
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080094 }
Junxiao Shic041ca32014-02-25 20:01:15 -070095
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080096 EventId m_selfEventId;
97};
98
99BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
100{
Junxiao Shic041ca32014-02-25 20:01:15 -0700101 m_selfEventId = scheduler::schedule(time::seconds(0.1),
102 bind(&SelfCancelFixture::cancelSelf, this));
103
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700104 BOOST_REQUIRE_NO_THROW(g_io.run());
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800105}
106
Alexander Afanasyev920af2f2014-01-25 22:56:11 -0800107BOOST_AUTO_TEST_SUITE_END()
108
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700109} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800110} // namespace nfd