blob: 87c75cf5186aeead7c5e523a41fc367660a4a418 [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{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070060 scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080061
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070062 EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070063 scheduler::cancel(i);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080064
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070065 scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080066
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070067 i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070068 scheduler::cancel(i);
69
70 // TODO deprecate periodic event
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070071 i = scheduler::getGlobalScheduler().schedulePeriodicEvent(time::milliseconds(300),
72 time::milliseconds(100),
73 bind(&SchedulerFixture::event4, this));
Junxiao Shic041ca32014-02-25 20:01:15 -070074 scheduler::schedule(time::seconds(1), bind(&scheduler::cancel, i));
75
Junxiao Shid9ee45c2014-02-27 15:38:11 -070076 g_io.run();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080077
78 BOOST_CHECK_EQUAL(count1, 1);
79 BOOST_CHECK_EQUAL(count2, 0);
80 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyevc1e2ee02014-02-25 17:02:07 -080081 BOOST_CHECK_GT(count4, 1);
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080082}
83
Junxiao Shi234a5322014-01-30 22:40:48 -070084BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
85{
Junxiao Shi234a5322014-01-30 22:40:48 -070086 EventId i;
Junxiao Shic041ca32014-02-25 20:01:15 -070087 scheduler::cancel(i);
Junxiao Shi234a5322014-01-30 22:40:48 -070088}
89
Junxiao Shid9ee45c2014-02-27 15:38:11 -070090struct SelfCancelFixture : protected BaseFixture
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080091{
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080092 void
93 cancelSelf()
94 {
Junxiao Shic041ca32014-02-25 20:01:15 -070095 scheduler::cancel(m_selfEventId);
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080096 }
Junxiao Shic041ca32014-02-25 20:01:15 -070097
Alexander Afanasyev94ceb122014-02-03 14:47:57 -080098 EventId m_selfEventId;
99};
100
101BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
102{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700103 m_selfEventId = scheduler::schedule(time::milliseconds(100),
Junxiao Shic041ca32014-02-25 20:01:15 -0700104 bind(&SelfCancelFixture::cancelSelf, this));
105
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700106 BOOST_REQUIRE_NO_THROW(g_io.run());
Alexander Afanasyev94ceb122014-02-03 14:47:57 -0800107}
108
Alexander Afanasyev920af2f2014-01-25 22:56:11 -0800109BOOST_AUTO_TEST_SUITE_END()
110
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700111} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800112} // namespace nfd