PPI/Ratefilter: added interval setter and unit tests
[senf.git] / 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 "../Utils/membind.hh"
31
32 #include "../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
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 BOOST_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( sink.size(), 2);
68 }
69
70 namespace {
71     // just a helper class for the test
72     struct RateFilter_IntervalChanger {
73         module::RateFilter & rateFilter_;
74         RateFilter_IntervalChanger( module::RateFilter & rateFilter)
75             : rateFilter_( rateFilter) {};
76         void changeInterval() {
77             rateFilter_.interval( senf::ClockService::milliseconds(200));
78         }
79     };
80 }
81
82 BOOST_AUTO_UNIT_TEST(rateFilter_changeInterval)
83 {
84     module::RateFilter rateFilter ( senf::ClockService::milliseconds(100) );
85     debug::PassiveSource source;
86     debug::PassiveSink sink;
87     
88     ppi::connect(source, rateFilter);
89     ppi::connect(rateFilter, sink);
90     
91     std::string data ("TEST");
92     senf::Packet p (senf::DataPacket::create(data));
93     for (int i=0; i<10; i++)
94         source.submit(p); 
95     
96     senf::scheduler::TimerEvent timeoutTimer (
97         "rateFilter test timer", &timeout,
98         senf::ClockService::now() + senf::ClockService::milliseconds(675));
99     
100     RateFilter_IntervalChanger intervalChanger (rateFilter);
101     senf::scheduler::TimerEvent timer ( "RateFilter_IntervalChanger timer", 
102         senf::membind(&RateFilter_IntervalChanger::changeInterval, intervalChanger),
103         senf::ClockService::now() + senf::ClockService::milliseconds(250));
104     
105     senf::ppi::run();
106
107     BOOST_CHECK_EQUAL( sink.size(), 4);
108 }
109
110 ///////////////////////////////cc.e////////////////////////////////////////
111 #undef prefix_
112
113 \f
114 // Local Variables:
115 // mode: c++
116 // fill-column: 100
117 // comment-column: 40
118 // c-file-style: "senf"
119 // indent-tabs-mode: nil
120 // ispell-local-dictionary: "american"
121 // compile-command: "scons -u test"
122 // End: