switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Scheduler / ClockService.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
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 ClockService unit tests */
30
31 //#include "ClockService.test.hh"
32 //#include "ClockService.test.ih"
33
34 // Custom includes
35 #include "ClockService.hh"
36 #include <errno.h>
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace {
45
46     bool is_close_clock(senf::ClockService::clock_type a, senf::ClockService::clock_type b,
47                         senf::ClockService::clock_type delta)
48     {
49         return (a<b ? b-a : a-b ) < delta;
50     }
51
52     void delay(unsigned long milliseconds)
53     {
54         struct timespec ts;
55         ts.tv_sec = milliseconds / 1000;
56         ts.tv_nsec = (milliseconds % 1000) * 1000000;
57         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
58     }
59
60 }
61
62 SENF_AUTO_UNIT_TEST(clockService)
63 {
64     BOOST_CHECK( senf::ClockService::abstime(0).is_not_a_date_time());
65
66     char const * enabled (getenv("SENF_TIMING_CRITICAL_TESTS"));
67     BOOST_WARN_MESSAGE(enabled, "Set SENF_TIMING_CRITICAL_TESTS to not skip timing critical tests");
68     BOOST_CHECK( true );
69
70     senf::ClockService::restart(); // So we know, when the signal will be delivered
71
72     senf::ClockService::clock_type t1 (senf::ClockService::now());
73     delay(200);
74     senf::ClockService::clock_type t2 (senf::ClockService::now());
75     if (enabled)
76         BOOST_CHECK_PREDICATE( is_close_clock,
77                                (t1 + senf::ClockService::milliseconds(200))
78                                (t2)
79                                (senf::ClockService::milliseconds(100)) );
80
81     t1 = t2;
82
83     // Wait for SIGALRM and let the signal handler do the clock-skew detection
84     delay(1*1000);
85
86     t2 = senf::ClockService::now();
87     if (enabled)
88         BOOST_CHECK_PREDICATE( is_close_clock,
89                                (t1 + senf::ClockService::seconds(1))
90                                (t2)
91                                (senf::ClockService::milliseconds(500)) );
92
93     t1 = t2;
94
95     delay(200);
96     if (enabled)
97         BOOST_CHECK_PREDICATE( is_close_clock,
98                                (t1 + senf::ClockService::milliseconds(200))
99                                (senf::ClockService::now())
100                                (senf::ClockService::milliseconds(100)) );
101
102     // The next check validates that the clock service itimer/heartbeat_ is correctly reset after a
103     // clock-skew is detected
104
105     if (enabled)
106         BOOST_CHECK_PREDICATE( is_close_clock,
107                                (t1 + senf::ClockService::milliseconds(200))
108                                (senf::ClockService::now())
109                                (senf::ClockService::milliseconds(100)) );
110 }
111
112 //-/////////////////////////////////////////////////////////////////////////////////////////////////
113 #undef prefix_
114
115 \f
116 // Local Variables:
117 // mode: c++
118 // fill-column: 100
119 // comment-column: 40
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // End: