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