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