switch to new MPL based Fraunhofer FOKUS Public License
[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 timeout() {
48         senf::scheduler::terminate();
49     }
50 }
51
52 SENF_AUTO_UNIT_TEST(rateFilter)
53 {
54     module::RateFilter rateFilter ( senf::ClockService::milliseconds(100) );
55     debug::PassiveSource source;
56     debug::PassiveSink sink;
57
58     ppi::connect(source, rateFilter);
59     ppi::connect(rateFilter, sink);
60
61     std::string data ("TEST");
62     senf::Packet p (senf::DataPacket::create(data));
63     for (int i=0; i<10; i++)
64         source.submit(p);
65
66     senf::scheduler::TimerEvent timer (
67         "rateFilter test timer", &timeout,
68         senf::ClockService::now() + senf::ClockService::milliseconds(250));
69
70     senf::ppi::run();
71
72     BOOST_CHECK_EQUAL( rateFilter.interval(), senf::ClockService::milliseconds(100) );
73     BOOST_CHECK_EQUAL( sink.size(), 2u );
74 }
75
76 namespace {
77     // just a helper class for the test
78     struct RateFilter_IntervalChanger {
79         module::RateFilter & rateFilter_;
80         RateFilter_IntervalChanger( module::RateFilter & rateFilter)
81             : rateFilter_( rateFilter) {};
82         void changeInterval() {
83             rateFilter_.interval( senf::ClockService::milliseconds(200));
84         }
85     };
86 }
87
88 SENF_AUTO_UNIT_TEST(rateFilter_changeInterval)
89 {
90     char const * enabled (getenv("SENF_TIMING_CRITICAL_TESTS"));
91     if (! enabled) {
92         BOOST_WARN_MESSAGE(false, "Set SENF_TIMING_CRITICAL_TESTS to not skip timing critical tests");
93     }
94
95     module::RateFilter rateFilter ( senf::ClockService::milliseconds(100) );
96     debug::PassiveSource source;
97     debug::PassiveSink sink;
98
99     ppi::connect(source, rateFilter);
100     ppi::connect(rateFilter, sink);
101
102     std::string data ("TEST");
103     senf::Packet p (senf::DataPacket::create(data));
104     for (int i=0; i<10; i++)
105         source.submit(p);
106
107     senf::scheduler::TimerEvent timeoutTimer (
108         "rateFilter test timer", &timeout,
109         senf::ClockService::now() + senf::ClockService::milliseconds(675));
110
111     RateFilter_IntervalChanger intervalChanger (rateFilter);
112     senf::scheduler::TimerEvent timer ( "RateFilter_IntervalChanger timer",
113         senf::membind(&RateFilter_IntervalChanger::changeInterval, intervalChanger),
114         senf::ClockService::now() + senf::ClockService::milliseconds(250));
115
116     senf::ppi::run();
117
118     BOOST_CHECK_EQUAL( rateFilter.interval(), senf::ClockService::milliseconds(200) );
119     if (enabled)
120         BOOST_CHECK_EQUAL( sink.size(), 4);
121 }
122
123 //-/////////////////////////////////////////////////////////////////////////////////////////////////
124 #undef prefix_
125
126 \f
127 // Local Variables:
128 // mode: c++
129 // fill-column: 100
130 // comment-column: 40
131 // c-file-style: "senf"
132 // indent-tabs-mode: nil
133 // ispell-local-dictionary: "american"
134 // compile-command: "scons -u test"
135 // End: