blob: 593744b89a86167c8c2a751d27f22ba4846b9d84 [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 }
24
25 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 }
50
51 int count1;
52 int count2;
53 int count3;
54 int count4;
55};
56
57BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
58{
59 boost::asio::io_service io;
60
61 Scheduler scheduler(io);
62 scheduler.scheduleEvent(time::seconds(0.1), bind(&SchedulerFixture::event1, this));
63
64 EventId i = scheduler.scheduleEvent(time::seconds(0.2), bind(&SchedulerFixture::event2, this));
65 scheduler.cancelEvent(i);
66
67 scheduler.scheduleEvent(time::seconds(0.05), bind(&SchedulerFixture::event3, this));
68
69 i = scheduler.scheduleEvent(time::seconds(0.01), bind(&SchedulerFixture::event2, this));
70 scheduler.cancelEvent(i);
71
72 i = scheduler.schedulePeriodicEvent(time::seconds(0.3), time::seconds(0.1), bind(&SchedulerFixture::event4, this));
73 scheduler.scheduleEvent(time::seconds(0.69), bind(&Scheduler::cancelEvent, &scheduler, i));
74
75 io.run();
76
77 BOOST_CHECK_EQUAL(count1, 1);
78 BOOST_CHECK_EQUAL(count2, 0);
79 BOOST_CHECK_EQUAL(count3, 1);
80 BOOST_CHECK_EQUAL(count4, 4);
81}
82
Junxiao Shi234a5322014-01-30 22:40:48 -070083BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
84{
85 boost::asio::io_service io;
86 Scheduler scheduler(io);
87
88 EventId i;
89 scheduler.cancelEvent(i);
90}
91
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080092BOOST_AUTO_TEST_SUITE_END()
93
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080094} // namespace nfd