4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief TimerEvent unit tests */
31 //#include "TimerEvent.test.hh"
32 //#include "TimerEvent.test.ih"
35 #include "TimerEvent.hh"
36 #include "Scheduler.hh"
37 #include <boost/bind.hpp>
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
41 #include <boost/random.hpp>
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 bool is_close(senf::ClockService::clock_type a, senf::ClockService::clock_type b)
50 return (a<b ? b-a : a-b) < senf::ClockService::milliseconds(50);
61 SENF_AUTO_UNIT_TEST(timerDispatcher)
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");
66 senf::scheduler::detail::FdManager::instance().timeout(1000);
68 senf::ClockService::clock_type t (senf::ClockService::now());
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() );
80 BOOST_CHECK_PREDICATE( is_close, (t2-t)(senf::ClockService::milliseconds(500)) );
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() );
93 BOOST_CHECK_PREDICATE( is_close, (t) (senf::ClockService::now()) );
94 BOOST_CHECK( called );
100 senf::ClockService::clock_type randomDelay()
102 static boost::uniform_smallint<> random (100,300);
103 static boost::mt19937 generator;
104 return senf::scheduler::now() + senf::ClockService::milliseconds(random(generator));
108 senf::ClockService::int64_type delay (0);
111 void jitterCb(senf::scheduler::TimerEvent & tm)
113 //std::cerr << "diff:" << senf::ClockService::in_microseconds( senf::scheduler::now() - tm.timeout()) << '\n';
115 delay += senf::ClockService::in_microseconds( senf::scheduler::now() - tm.timeout());
117 tm.timeout(randomDelay());
128 // std::cerr << senf::scheduler::now() << '\n';
131 // void idleCb(senf::scheduler::TimerEvent & tm)
133 // tm.timeout( senf::scheduler::now());
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);
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());
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());
156 senf::scheduler::TimerEvent timeout("jitterTest::timeout", &senf::scheduler::terminate,
157 senf::scheduler::now() + senf::ClockService::seconds(5));
159 senf::scheduler::process();
161 std::cerr << "Average scheduling delay: " << delay/count << " microseconds\n";
166 SENF_AUTO_UNIT_TEST(timerJitter)
168 senf::scheduler::watchdogTimeout(0);
169 std::cerr << "Epoll timers\n";
170 senf::scheduler::loresTimers();
172 std::cerr << "Hires timers\n";
173 senf::scheduler::hiresTimers();
175 senf::scheduler::loresTimers();
176 senf::scheduler::watchdogTimeout(1000);
180 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"