blob: 01fb18c7b93ea940cec4095cd5a24d8b53cd1cc3 [file] [log] [blame]
Alexander Afanasyevf6468892014-01-29 01:04:14 -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 "util/scheduler.hpp"
8
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -07009#include "boost-test.hpp"
Alexander Afanasyevf6468892014-01-29 01:04:14 -080010
11namespace ndn {
12
13BOOST_AUTO_TEST_SUITE(TestScheduler)
14
15struct SchedulerFixture
16{
17 SchedulerFixture()
18 : count1(0)
19 , count2(0)
20 , count3(0)
21 , count4(0)
22 {
23 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070024
Alexander Afanasyevf6468892014-01-29 01:04:14 -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 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050
Alexander Afanasyevf6468892014-01-29 01:04:14 -080051 int count1;
52 int count2;
53 int count3;
54 int count4;
55};
56
57BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
58{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070059 boost::asio::io_service io;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080060
61 Scheduler scheduler(io);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062 scheduler.scheduleEvent(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
63
64 EventId i = scheduler.scheduleEvent(time::seconds(1), bind(&SchedulerFixture::event2, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080065 scheduler.cancelEvent(i);
66
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067 scheduler.scheduleEvent(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 i = scheduler.scheduleEvent(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080070 scheduler.cancelEvent(i);
71
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072 i = scheduler.schedulePeriodicEvent(time::milliseconds(1500), time::milliseconds(500), bind(&SchedulerFixture::event4, this));
Alexander Afanasyev83cdc5b2014-03-03 16:17:47 +000073 scheduler.scheduleEvent(time::seconds(4), bind(&Scheduler::cancelEvent, &scheduler, i));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075 io.run();
76
77 BOOST_CHECK_EQUAL(count1, 1);
78 BOOST_CHECK_EQUAL(count2, 0);
79 BOOST_CHECK_EQUAL(count3, 1);
Alexander Afanasyev83cdc5b2014-03-03 16:17:47 +000080 BOOST_CHECK_GE(count4, 2);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080081}
82
Yingdi Yuf2a82092014-02-03 16:49:15 -080083BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
84{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070085 boost::asio::io_service io;
Yingdi Yuf2a82092014-02-03 16:49:15 -080086 Scheduler scheduler(io);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070087
Yingdi Yuf2a82092014-02-03 16:49:15 -080088 EventId i;
89 scheduler.cancelEvent(i);
90}
91
92struct SelfCancelFixture
93{
94 SelfCancelFixture()
95 : m_scheduler(m_io)
96 {
97 }
98
99 void
100 cancelSelf()
101 {
102 m_scheduler.cancelEvent(m_selfEventId);
103 }
104
105 boost::asio::io_service m_io;
106 Scheduler m_scheduler;
107 EventId m_selfEventId;
108};
109
110BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
111{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112 m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800113 bind(&SelfCancelFixture::cancelSelf, this));
114
115 BOOST_REQUIRE_NO_THROW(m_io.run());
116}
117
118struct SelfRescheduleFixture
119{
120 SelfRescheduleFixture()
121 : m_scheduler(m_io)
122 , m_count(0)
123 {
124 }
125
126 void
127 reschedule()
128 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700129 EventId eventId = m_scheduler.scheduleEvent(time::milliseconds(100),
130 bind(&SelfRescheduleFixture::reschedule, this));
Yingdi Yuf2a82092014-02-03 16:49:15 -0800131 m_scheduler.cancelEvent(m_selfEventId);
132 m_selfEventId = eventId;
133
134 if(m_count < 5)
135 m_count++;
136 else
137 m_scheduler.cancelEvent(m_selfEventId);
138 }
139
140 void
141 reschedule2()
142 {
143 m_scheduler.cancelEvent(m_selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144
145
Yingdi Yuf2a82092014-02-03 16:49:15 -0800146 if(m_count < 5)
147 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700148 m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800149 bind(&SelfRescheduleFixture::reschedule2, this));
150 m_count++;
151 }
152 }
153
154 void
155 doNothing()
156 {
157 m_count++;
158 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700159
Yingdi Yuf2a82092014-02-03 16:49:15 -0800160 void
161 reschedule3()
162 {
163 m_scheduler.cancelEvent(m_selfEventId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700164
165 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800166 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700167 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800168 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700169 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800170 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700171 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800172 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700173 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800174 bind(&SelfRescheduleFixture::doNothing, this));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700175 m_scheduler.scheduleEvent(time::milliseconds(100),
Yingdi Yuf2a82092014-02-03 16:49:15 -0800176 bind(&SelfRescheduleFixture::doNothing, this));
177 }
178
179 boost::asio::io_service m_io;
180 Scheduler m_scheduler;
181 EventId m_selfEventId;
182 int m_count;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700183
Yingdi Yuf2a82092014-02-03 16:49:15 -0800184};
185
186BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
187{
188 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
189 bind(&SelfRescheduleFixture::reschedule, this));
190
191 BOOST_REQUIRE_NO_THROW(m_io.run());
192
193 BOOST_CHECK_EQUAL(m_count, 5);
194}
195
196BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
197{
198 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
199 bind(&SelfRescheduleFixture::reschedule2, this));
200
201 BOOST_REQUIRE_NO_THROW(m_io.run());
202
203 BOOST_CHECK_EQUAL(m_count, 5);
204}
205
206BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
207{
208 m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
209 bind(&SelfRescheduleFixture::reschedule3, this));
210
211 BOOST_REQUIRE_NO_THROW(m_io.run());
212
213 BOOST_CHECK_EQUAL(m_count, 6);
214}
215
216
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800217BOOST_AUTO_TEST_SUITE_END()
218
219} // namespace ndn