PPI: Clean up time interface
[senf.git] / Scheduler / ClockService.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief ClockService.test unit tests */
25
26 //#include "ClockService.test.hh"
27 //#include "ClockService.test.ih"
28
29 // Custom includes
30 #include "ClockService.hh"
31 #include <errno.h>
32
33 #include <boost/test/auto_unit_test.hpp>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 namespace senf {
40 namespace detail {
41
42     struct ClockServiceTest
43     {
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_; }
48     };
49
50 }}
51
52 namespace {
53
54     bool is_close_clock(senf::ClockService::clock_type a, senf::ClockService::clock_type b, 
55                         unsigned long delta = 10100000ul)
56     {
57         return (a<b ? b-a : a-b ) < delta;
58     }
59
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(10) )
62     {
63         return (a<b ? b-a : a-b ) < delta;
64     }
65
66     void delay(unsigned long milliseconds)
67     {
68         struct timespec ts;
69         ts.tv_sec = milliseconds / 1000;
70         ts.tv_nsec = (milliseconds % 1000) * 1000000;
71         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
72     }
73
74 }
75
76 BOOST_AUTO_UNIT_TEST(clockService)
77 {
78     senf::ClockService::restart(); // So we know, when the signal will be delivered
79     
80     senf::ClockService::clock_type t (senf::ClockService::now());
81     delay(100);
82     BOOST_CHECK_PREDICATE( is_close_clock,
83                            (t + senf::ClockService::milliseconds(100)) 
84                            (senf::ClockService::now()) );
85
86     // We shift both heartbeat() and base() back 1 minute. This is the same as
87     // moving the current time forward 1 minute.
88     boost::posix_time::ptime b (senf::detail::ClockServiceTest::base());
89     boost::posix_time::ptime h (senf::detail::ClockServiceTest::heartbeat());
90     senf::detail::ClockServiceTest::heartbeat() -= boost::posix_time::minutes(1);
91     senf::detail::ClockServiceTest::base() -= boost::posix_time::minutes(1);
92
93     // Wait for SIGALRM and let the signal handler do the clock-skew detection
94     delay(senf::ClockService::CheckInterval*1000);
95
96     BOOST_CHECK_PREDICATE( is_close_pt,
97                            (b)
98                            (senf::detail::ClockServiceTest::base()) );
99     BOOST_CHECK_PREDICATE( is_close_pt,
100                            (h+boost::posix_time::seconds(senf::ClockService::CheckInterval))
101                            (senf::detail::ClockServiceTest::heartbeat()) );
102
103     BOOST_CHECK_PREDICATE( is_close_clock,
104                            (t + senf::ClockService::milliseconds(1100))
105                            (senf::ClockService::now()) );
106
107     senf::detail::ClockServiceTest::heartbeat() -= boost::posix_time::minutes(1);
108     senf::detail::ClockServiceTest::base() -= boost::posix_time::minutes(1);
109
110     // Let now() do the clock skew detection using getitimer() ...
111     delay(100);
112     BOOST_CHECK_PREDICATE( is_close_clock,
113                            (t + senf::ClockService::milliseconds(1200))
114                            (senf::ClockService::now()) );
115     
116 }
117
118 ///////////////////////////////cc.e////////////////////////////////////////
119 #undef prefix_
120
121 \f
122 // Local Variables:
123 // mode: c++
124 // fill-column: 100
125 // comment-column: 40
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // End: