878b740e4475fa58157c712ae829f975ec78c914
[senf.git] / senf / Scheduler / TimerEvent.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief TimerEvent unit tests */
30
31 //#include "TimerEvent.test.hh"
32 //#include "TimerEvent.test.ih"
33
34 // Custom includes
35 #include "TimerEvent.hh"
36 #include "Scheduler.hh"
37 #include <boost/bind.hpp>
38
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
41 #include <boost/random.hpp>
42
43 #define prefix_
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45
46 namespace {
47
48     bool is_close(senf::ClockService::clock_type a, senf::ClockService::clock_type b)
49     {
50         return (a<b ? b-a : a-b) < senf::ClockService::milliseconds(50);
51     }
52
53     bool called = false;
54     void handler()
55     {
56         called = true;
57     }
58
59 }
60
61 SENF_AUTO_UNIT_TEST(timerDispatcher)
62 {
63     char const * enabled (getenv("SENF_TIMING_CRITICAL_TESTS"));
64     BOOST_WARN_MESSAGE(enabled, "Set SENF_TIMING_CRITICAL_TESTS to not skip timing critical tests");
65
66     senf::scheduler::detail::FdManager::instance().timeout(1000);
67
68     senf::ClockService::clock_type t (senf::ClockService::now());
69     {
70         senf::scheduler::TimerEvent timer ("testTimer", &handler,
71                                            t + senf::ClockService::milliseconds(500));
72         SENF_CHECK_NO_THROW( timer.disable() );
73         SENF_CHECK_NO_THROW( timer.enable() );
74         BOOST_CHECK( timer.enabled() );
75         SENF_CHECK_NO_THROW( senf::scheduler::process() );
76         senf::ClockService::clock_type t2 (senf::ClockService::now());
77         BOOST_CHECK( called );
78         BOOST_CHECK( ! timer.enabled() );
79         if (enabled)
80             BOOST_CHECK_PREDICATE( is_close, (t2-t)(senf::ClockService::milliseconds(500)) );
81
82         called=false;
83         t = senf::ClockService::now();
84         SENF_CHECK_NO_THROW( timer.timeout(t) );
85         BOOST_CHECK( timer.enabled() );
86         SENF_CHECK_NO_THROW( senf::scheduler::detail::TimerDispatcher::instance().enable() );
87         SENF_CHECK_NO_THROW( senf::scheduler::detail::TimerDispatcher::instance().reschedule() );
88         SENF_CHECK_NO_THROW( senf::scheduler::detail::FdManager::instance().processOnce() );
89         SENF_CHECK_NO_THROW( senf::scheduler::detail::TimerDispatcher::instance().prepareRun() );
90         SENF_CHECK_NO_THROW( senf::scheduler::detail::FIFORunner::instance().run() );
91         SENF_CHECK_NO_THROW( senf::scheduler::detail::TimerDispatcher::instance().disable() );
92         if (enabled)
93             BOOST_CHECK_PREDICATE( is_close, (t) (senf::ClockService::now()) );
94         BOOST_CHECK( called );
95     }
96 }
97
98 namespace {
99
100     senf::ClockService::clock_type randomDelay()
101     {
102         static boost::uniform_smallint<> random (100,300);
103         static boost::mt19937 generator;
104         return senf::scheduler::now() + senf::ClockService::milliseconds(random(generator));
105     }
106
107     unsigned count (0);
108     senf::ClockService::clock_type delay (0);
109     bool haveCb (false);
110
111     void jitterCb(senf::scheduler::TimerEvent & tm)
112     {
113         //std::cerr << "diff:" << senf::ClockService::in_microseconds( senf::scheduler::now() - tm.timeout()) << '\n';
114         count ++;
115         delay += senf::ClockService::in_microseconds( senf::scheduler::now() - tm.timeout());
116         haveCb = true;
117         tm.timeout(randomDelay());
118     }
119
120     void preCb()
121     {
122         haveCb = false;
123     }
124
125     void postCb()
126     {
127         if (! haveCb)
128             std::cerr << senf::scheduler::now() << '\n';
129     }
130
131     void idleCb(senf::scheduler::TimerEvent & tm)
132     {
133         tm.timeout( senf::scheduler::now());
134     }
135
136     void jitterTest()
137     {
138         count = 0;
139         delay = 0;
140 //        senf::scheduler::EventHook pre ("jitterTest::preCb", &preCb,
141 //                                        senf::scheduler::EventHook::PRE);
142 //        senf::scheduler::EventHook post ("jitterTest::postCb", &postCb,
143 //                                         senf::scheduler::EventHook::POST);
144
145         senf::scheduler::TimerEvent tm1 (
146                 "jitterTest::tm1", boost::bind(&jitterCb, boost::ref(tm1)), randomDelay());
147         senf::scheduler::TimerEvent tm2 (
148                 "jitterTest::tm2", boost::bind(&jitterCb, boost::ref(tm2)), randomDelay());
149         senf::scheduler::TimerEvent tm3 (
150                 "jitterTest::tm3", boost::bind(&jitterCb, boost::ref(tm3)), randomDelay());
151
152         // enabled "Idle"-Event will degrade scheduling delay
153         //senf::scheduler::TimerEvent idle (
154         //        "jitterTest::idle", boost::bind(&idleCb, boost::ref(idle)), senf::scheduler::now());
155
156         senf::scheduler::TimerEvent timeout("jitterTest::timeout", &senf::scheduler::terminate,
157                                             senf::scheduler::now() + senf::ClockService::seconds(5));
158
159         senf::scheduler::process();
160
161         std::cerr << "Average scheduling delay: " << delay/count << " microseconds\n";
162     }
163
164 }
165
166 SENF_AUTO_UNIT_TEST(timerJitter)
167 {
168     senf::scheduler::watchdogTimeout(0);
169     std::cerr << "Epoll timers\n";
170     senf::scheduler::loresTimers();
171     jitterTest();
172     std::cerr << "Hires timers\n";
173     senf::scheduler::hiresTimers();
174     jitterTest();
175     senf::scheduler::loresTimers();
176     senf::scheduler::watchdogTimeout(1000);
177     BOOST_CHECK( true );
178 }
179
180 //-/////////////////////////////////////////////////////////////////////////////////////////////////
181 #undef prefix_
182
183 \f
184 // Local Variables:
185 // mode: c++
186 // fill-column: 100
187 // comment-column: 40
188 // c-file-style: "senf"
189 // indent-tabs-mode: nil
190 // ispell-local-dictionary: "american"
191 // compile-command: "scons -u test"
192 // End: