fixes for ClockService::clock_type -> RestrictedInt change introduced by last commit
[senf.git] / senf / PPI / RateFilter.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief RateFilter unit tests */
30
31 // Custom includes
32 #include "RateFilter.hh"
33 #include "DebugModules.hh"
34 #include "Setup.hh"
35 #include <senf/Utils/membind.hh>
36
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 namespace ppi = senf::ppi;
43 namespace module = ppi::module;
44 namespace debug = module::debug;
45
46 namespace {
47     void runPPI(senf::ClockService::clock_type t) {
48         senf::scheduler::TimerEvent timeout(
49                 "emulatedDvbInterface test timer", &senf::scheduler::terminate, senf::scheduler::now() + t);
50         senf::ppi::run();
51     }
52 }
53
54 SENF_AUTO_UNIT_TEST(rateFilter)
55 {
56     module::RateFilter rateFilter ( senf::ClockService::milliseconds(200) );
57     debug::PassiveSource source;
58     debug::PassiveSink sink;
59
60     ppi::connect(source, rateFilter);
61     ppi::connect(rateFilter, sink);
62
63     std::string data ("TEST");
64     senf::Packet p (senf::DataPacket::create(data));
65     for (int i=0; i<10; i++)
66         source.submit(p);
67
68     runPPI( senf::ClockService::milliseconds(500));
69
70     BOOST_CHECK_EQUAL( rateFilter.interval(), senf::ClockService::milliseconds(200) );
71     BOOST_CHECK_EQUAL( sink.size(), 2u );
72 }
73
74 namespace {
75     // just a helper class for the test
76     struct RateFilter_IntervalChanger {
77         module::RateFilter & rateFilter_;
78         RateFilter_IntervalChanger( module::RateFilter & rateFilter)
79             : rateFilter_( rateFilter) {};
80         void changeInterval() {
81             rateFilter_.interval( senf::ClockService::milliseconds(200));
82         }
83     };
84 }
85
86 SENF_AUTO_UNIT_TEST(rateFilter_changeInterval)
87 {
88     char const * enabled (getenv("SENF_TIMING_CRITICAL_TESTS"));
89     if (! enabled) {
90         BOOST_WARN_MESSAGE(false, "Set SENF_TIMING_CRITICAL_TESTS to not skip timing critical tests");
91     }
92
93     module::RateFilter rateFilter ( senf::ClockService::milliseconds(100) );
94     debug::PassiveSource source;
95     debug::PassiveSink sink;
96
97     ppi::connect(source, rateFilter);
98     ppi::connect(rateFilter, sink);
99
100     std::string data ("TEST");
101     senf::Packet p (senf::DataPacket::create(data));
102     for (int i=0; i<10; i++)
103         source.submit(p);
104
105     RateFilter_IntervalChanger intervalChanger (rateFilter);
106     senf::scheduler::TimerEvent timer ( "RateFilter_IntervalChanger timer",
107         senf::membind(&RateFilter_IntervalChanger::changeInterval, intervalChanger),
108         senf::ClockService::now() + senf::ClockService::milliseconds(250));
109
110     runPPI( senf::ClockService::milliseconds(675));
111
112     BOOST_CHECK_EQUAL( rateFilter.interval(), senf::ClockService::milliseconds(200) );
113     if (enabled)
114         BOOST_CHECK_EQUAL( sink.size(), 4);
115 }
116
117 //-/////////////////////////////////////////////////////////////////////////////////////////////////
118 #undef prefix_
119
120 \f
121 // Local Variables:
122 // mode: c++
123 // fill-column: 100
124 // comment-column: 40
125 // c-file-style: "senf"
126 // indent-tabs-mode: nil
127 // ispell-local-dictionary: "american"
128 // compile-command: "scons -u test"
129 // End: