182755b21fb85c4b81df91288233071825130f2d
[senf.git] / 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.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 "../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                         unsigned long delta = senf::ClockService::milliseconds(100))
43     {
44         return (a<b ? b-a : a-b ) < delta;
45     }
46
47     bool is_close_pt(boost::posix_time::ptime a, boost::posix_time::ptime b,
48                      boost::posix_time::time_duration delta = boost::posix_time::milliseconds(100) )
49     {
50         return (a<b ? b-a : a-b ) < delta;
51     }
52
53     void delay(unsigned long milliseconds)
54     {
55         struct timespec ts;
56         ts.tv_sec = milliseconds / 1000;
57         ts.tv_nsec = (milliseconds % 1000) * 1000000;
58         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
59     }
60
61 }
62
63 BOOST_AUTO_UNIT_TEST(clockService)
64 {
65     senf::ClockService::restart(); // So we know, when the signal will be delivered
66     
67     senf::ClockService::clock_type t1 (senf::ClockService::now());
68     delay(200);
69     senf::ClockService::clock_type t2 (senf::ClockService::now());
70     BOOST_CHECK_PREDICATE( is_close_clock,
71                            (t1 + senf::ClockService::milliseconds(200)) 
72                            (t2) );
73
74     t1 = t2;
75
76     // Wait for SIGALRM and let the signal handler do the clock-skew detection
77     delay(1*1000);
78
79     t2 = senf::ClockService::now();
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     BOOST_CHECK_PREDICATE( is_close_clock,
89                            (t1 + senf::ClockService::milliseconds(200))
90                            (senf::ClockService::now()) );
91
92     // The next check validates that the clock service itimer/heartbeat_ is correctly reset after a
93     // clock-skew is detected
94
95     BOOST_CHECK_PREDICATE( is_close_clock,
96                            (t1 + senf::ClockService::milliseconds(200))
97                            (senf::ClockService::now()) );
98 }
99
100 ///////////////////////////////cc.e////////////////////////////////////////
101 #undef prefix_
102
103 \f
104 // Local Variables:
105 // mode: c++
106 // fill-column: 100
107 // comment-column: 40
108 // c-file-style: "senf"
109 // indent-tabs-mode: nil
110 // ispell-local-dictionary: "american"
111 // compile-command: "scons -u test"
112 // End: