blob: d6b51fa83917a191023375cd1d0633973a2c3903 [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
9#include <boost/test/unit_test.hpp>
10
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080011namespace nfd {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080012
13BOOST_AUTO_TEST_SUITE(CoreScheduler)
14
15struct SchedulerFixture
16{
17 SchedulerFixture()
18 : count1(0)
19 , count2(0)
20 , count3(0)
21 , count4(0)
22 {
23 }
Junxiao Shic041ca32014-02-25 20:01:15 -070024
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080025 void
26 event1()
27 {
28 BOOST_CHECK_EQUAL(count3, 1);
29 ++count1;
30 }
31
32 void
33 event2()
34 {
35 ++count2;
36 }
37
38 void
39 event3()
40 {
41 BOOST_CHECK_EQUAL(count1, 0);
42 ++count3;
43 }
44
45 void
46 event4()
47 {
48 ++count4;
49 }
Junxiao Shic041ca32014-02-25 20:01:15 -070050
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080051 int count1;
52 int count2;
53 int count3;
54 int count4;
55};
56
57BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
58{
Junxiao Shic041ca32014-02-25 20:01:15 -070059 resetGlobalIoService();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080060
Junxiao Shic041ca32014-02-25 20:01:15 -070061 scheduler::schedule(time::seconds(0.5), bind(&SchedulerFixture::event1, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080062
Junxiao Shic041ca32014-02-25 20:01:15 -070063 EventId i = scheduler::schedule(time::seconds(1.0), bind(&SchedulerFixture::event2, this));
64 scheduler::cancel(i);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080065
Junxiao Shic041ca32014-02-25 20:01:15 -070066 scheduler::schedule(time::seconds(0.25), bind(&SchedulerFixture::event3, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080067
Junxiao Shic041ca32014-02-25 20:01:15 -070068 i = scheduler::schedule(time::seconds(0.05), bind(&SchedulerFixture::event2, this));
69 scheduler::cancel(i);
70
71 // TODO deprecate periodic event
72 i = scheduler::getGlobalScheduler().schedulePeriodicEvent(time::seconds(0.3), time::seconds(0.1), bind(&SchedulerFixture::event4, this));
73 scheduler::schedule(time::seconds(1), bind(&scheduler::cancel, i));
74
75 getGlobalIoService().run();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080076
77 BOOST_CHECK_EQUAL(count1, 1);
78 BOOST_CHECK_EQUAL(count2, 0);
79 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -080080 BOOST_CHECK_GT(count4, 1);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080081}
82
Junxiao Shi234a5322014-01-30 22:40:48 -070083BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
84{
Junxiao Shi234a5322014-01-30 22:40:48 -070085 EventId i;
Junxiao Shic041ca32014-02-25 20:01:15 -070086 scheduler::cancel(i);
Junxiao Shi234a5322014-01-30 22:40:48 -070087}
88
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080089struct SelfCancelFixture
90{
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080091 void
92 cancelSelf()
93 {
Junxiao Shic041ca32014-02-25 20:01:15 -070094 scheduler::cancel(m_selfEventId);
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080095 }
Junxiao Shic041ca32014-02-25 20:01:15 -070096
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080097 EventId m_selfEventId;
98};
99
100BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
101{
Junxiao Shic041ca32014-02-25 20:01:15 -0700102 resetGlobalIoService();
103
104 m_selfEventId = scheduler::schedule(time::seconds(0.1),
105 bind(&SelfCancelFixture::cancelSelf, this));
106
107 BOOST_REQUIRE_NO_THROW(getGlobalIoService().run());
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800108}
109
Alexander Afanasyev920af2f2014-01-25 22:56:11 -0800110BOOST_AUTO_TEST_SUITE_END()
111
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800112} // namespace nfd