4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief ClockService.test unit tests */
26 //#include "ClockService.test.hh"
27 //#include "ClockService.test.ih"
30 #include "ClockService.hh"
33 #include "../Utils/auto_unit_test.hh"
34 #include <boost/test/test_tools.hpp>
37 ///////////////////////////////cc.p////////////////////////////////////////
42 struct ClockServiceTest
44 static boost::posix_time::ptime & base()
45 { return senf::ClockService::instance().base_; }
46 static boost::posix_time::ptime & heartbeat()
47 { return senf::ClockService::instance().heartbeat_; }
54 bool is_close_clock(senf::ClockService::clock_type a, senf::ClockService::clock_type b,
55 unsigned long delta = senf::ClockService::milliseconds(100))
57 return (a<b ? b-a : a-b ) < delta;
60 bool is_close_pt(boost::posix_time::ptime a, boost::posix_time::ptime b,
61 boost::posix_time::time_duration delta = boost::posix_time::milliseconds(100) )
63 return (a<b ? b-a : a-b ) < delta;
66 void delay(unsigned long milliseconds)
69 ts.tv_sec = milliseconds / 1000;
70 ts.tv_nsec = (milliseconds % 1000) * 1000000;
71 while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
76 BOOST_AUTO_UNIT_TEST(clockService)
78 senf::ClockService::restart(); // So we know, when the signal will be delivered
80 senf::ClockService::clock_type t1 (senf::ClockService::now());
82 senf::ClockService::clock_type t2 (senf::ClockService::now());
83 BOOST_CHECK_PREDICATE( is_close_clock,
84 (t1 + senf::ClockService::milliseconds(200))
88 // We shift both heartbeat() and base() back 1 minute. This is the same as
89 // moving the current time forward 1 minute.
90 boost::posix_time::ptime b (senf::detail::ClockServiceTest::base());
91 boost::posix_time::ptime h (senf::detail::ClockServiceTest::heartbeat());
92 senf::detail::ClockServiceTest::heartbeat() -= boost::posix_time::minutes(1);
93 senf::detail::ClockServiceTest::base() -= boost::posix_time::minutes(1);
95 // Wait for SIGALRM and let the signal handler do the clock-skew detection
96 delay(senf::ClockService::CheckInterval*1000);
98 BOOST_CHECK_PREDICATE( is_close_pt,
100 (senf::detail::ClockServiceTest::base()) );
101 BOOST_CHECK_PREDICATE( is_close_pt,
102 (h+boost::posix_time::seconds(senf::ClockService::CheckInterval))
103 (senf::detail::ClockServiceTest::heartbeat()) );
105 t2 = senf::ClockService::now();
106 BOOST_CHECK_PREDICATE( is_close_clock,
107 (t1 + senf::ClockService::seconds(senf::ClockService::CheckInterval))
109 (senf::ClockService::milliseconds(500)) );
113 senf::detail::ClockServiceTest::heartbeat() -= boost::posix_time::minutes(1);
114 senf::detail::ClockServiceTest::base() -= boost::posix_time::minutes(1);
116 // Let now() do the clock skew detection using getitimer() ...
118 BOOST_CHECK_PREDICATE( is_close_clock,
119 (t1 + senf::ClockService::milliseconds(200))
120 (senf::ClockService::now()) );
122 // The next check validates that the clock service itimer/heartbeat_ is correctly reset after a
123 // clock-skew is detected
125 BOOST_CHECK_PREDICATE( is_close_clock,
126 (t1 + senf::ClockService::milliseconds(200))
127 (senf::ClockService::now()) );
130 ///////////////////////////////cc.e////////////////////////////////////////
137 // comment-column: 40
138 // c-file-style: "senf"
139 // indent-tabs-mode: nil
140 // ispell-local-dictionary: "american"
141 // compile-command: "scons -u test"