41bacdee2e09d1624788041e9f443be41971b17e
[senf.git] / senf / Scheduler / TimerEventProxy.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2010
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 //   Jens Moedeker <jmo@berlios.de>
27
28 /** \file
29     \brief TimerEventProxy.test non-inline non-template implementation */
30
31 //#include "TimerEventProxy.test.hh"
32 //#include "TimerEventProxy.test.ih"
33
34 // Custom includes
35 #include "TimerEventProxy.hh"
36 #include "Scheduler.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40 #include <boost/random.hpp>
41
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 using namespace senf;
45
46 namespace {
47
48     int mask = 0;
49
50     void handler(ClockService::clock_type time, int const &id)
51     {
52         mask = mask + id;
53     }
54
55     void run(ClockService::clock_type t) {
56         scheduler::TimerEvent timeout(
57                 "test-timeout", &scheduler::terminate, scheduler::now() + t);
58         scheduler::process();
59     }
60
61 }
62
63 SENF_AUTO_UNIT_TEST(timerEventProxy)
64 {
65 //    // abort on watchdog timeout
66 //    scheduler::watchdogAbort( true);
67 //    scheduler::watchdogTimeout(5000);
68
69     ClockService::clock_type now (ClockService::now());
70     {
71         scheduler::TimerEventProxy<int> timers ("unit-test");
72
73 //        timers.add( t + ClockService::milliseconds(10000), 0 , &handler);
74         timers.add( now + ClockService::milliseconds(800), 4, &handler);
75         timers.add( now + ClockService::milliseconds(200), 1, &handler);
76         BOOST_CHECK( timers.remove( 4));
77         BOOST_CHECK(! timers.remove( 4));
78         BOOST_CHECK_EQUAL( timers.timeout(4), 0);
79         timers.add( now + ClockService::milliseconds(700), 2, &handler);
80
81         BOOST_CHECK_EQUAL( timers.timeout(1), now + ClockService::milliseconds(200));
82         BOOST_CHECK_EQUAL( timers.timeout(2), now + ClockService::milliseconds(700));
83
84         timers.add( now + ClockService::milliseconds(800), 2, &handler);
85         BOOST_CHECK_EQUAL( timers.timeout(2), now + ClockService::milliseconds(800));
86         timers.add( now, 4, &handler);
87
88         run( ClockService::milliseconds( 2000));
89
90         BOOST_CHECK( mask == 7);
91     }
92 }
93
94
95 //-/////////////////////////////////////////////////////////////////////////////////////////////////
96 #undef prefix_
97
98
99 // Local Variables:
100 // mode: c++
101 // fill-column: 100
102 // comment-column: 40
103 // c-file-style: "senf"
104 // indent-tabs-mode: nil
105 // ispell-local-dictionary: "american"
106 // compile-command: "scons -u test"
107 // End: