blob: b63c3c0b42c1347ce64e82416af1863b4865fc6f [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
9#include <boost/test/unit_test.hpp>
10
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 }
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
83BOOST_AUTO_TEST_SUITE_END()
84
85} // namespace ndn