4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Route unit tests */
26 //#include "Route.test.hh"
27 //#include "Route.test.ih"
31 #include "DebugEvent.hh"
32 #include "DebugModules.hh"
35 #include "CloneSource.hh"
37 #include "PassiveQueue.hh"
38 #include "../Utils/membind.hh"
40 #include "../Utils/auto_unit_test.hh"
41 #include <boost/test/test_tools.hpp>
44 ///////////////////////////////cc.p////////////////////////////////////////
46 namespace ppi = senf::ppi;
47 namespace connector = ppi::connector;
48 namespace module = ppi::module;
49 namespace debug = module::debug;
52 class RouteTester : public module::Module
54 SENF_PPI_MODULE(RouteTester);
57 connector::ActiveInput<> activeIn;
58 connector::PassiveInput<> passiveIn;
60 connector::ActiveOutput<> activeOut;
61 connector::PassiveOutput<> passiveOut;
63 ppi::DebugEvent event;
65 ppi::ForwardingRoute * rt;
67 RouteTester() : events(0), throttles(0) {
68 route( activeIn, activeOut ); // non-forwarding
69 rt = & route( activeIn, passiveOut ); // forward throttling
70 route( passiveIn, activeOut ); // backward throttling
71 route( passiveIn, passiveOut ); // non-forwarding
72 route( event, activeOut ); // forward event throttling
73 route( activeIn, event ); // backward event throttling
75 passiveIn.onRequest(&RouteTester::inputRequest);
76 passiveOut.onRequest(&RouteTester::outputRequest);
77 registerEvent(event, &RouteTester::onEvent);
79 activeIn.onThrottle(&RouteTester::throttleRequest);
80 activeIn.onUnthrottle(&RouteTester::unthrottleRequest);
81 activeOut.onThrottle(&RouteTester::throttleRequest);
82 activeOut.onUnthrottle(&RouteTester::unthrottleRequest);
86 activeOut(passiveIn());
89 void outputRequest() {
90 passiveOut(activeIn());
97 void throttleRequest() {
101 void unthrottleRequest() {
110 BOOST_AUTO_UNIT_TEST(route)
112 debug::PassiveSource passiveSource;
113 debug::ActiveSource activeSource;
114 debug::PassiveSink passiveSink;
115 debug::ActiveSink activeSink;
118 ppi::connect(passiveSource, tester.activeIn);
119 ppi::connect(activeSource, tester.passiveIn);
120 ppi::connect(tester.activeOut, passiveSink);
121 ppi::connect(tester.passiveOut, activeSink);
125 senf::Packet p1 (senf::DataPacket::create());
126 senf::Packet p2 (senf::DataPacket::create());
128 passiveSource.submit(p1);
129 activeSource.submit(p2);
131 BOOST_CHECK( p2 == passiveSink.front() );
133 // The passive source is not throttled at this point since it has packets in queue
135 passiveSink.input.throttle();
136 BOOST_CHECK( passiveSink.input.throttled() );
137 BOOST_CHECK( ! tester.activeOut );
138 BOOST_CHECK_EQUAL( tester.throttles, 1 );
139 BOOST_CHECK( tester.passiveIn.throttled() );
140 BOOST_CHECK( ! activeSource );
141 BOOST_CHECK( ! tester.event.enabled() );
143 passiveSink.input.unthrottle();
144 BOOST_CHECK( activeSource );
145 BOOST_CHECK( tester.event.enabled() );
147 // Now throttle the passive source by exhausting the queue
149 BOOST_CHECK( p1 == activeSink.request() );
150 BOOST_CHECK( passiveSource.output.throttled() );
151 BOOST_CHECK( ! tester.activeIn );
152 BOOST_CHECK_EQUAL( tester.throttles, 1 );
153 BOOST_CHECK( tester.passiveOut.throttled() );
154 BOOST_CHECK( ! activeSink );
155 BOOST_CHECK( ! tester.event.enabled() );
157 passiveSource.submit(p1);
158 BOOST_CHECK( activeSink );
159 BOOST_CHECK( tester.event.enabled() );
161 // Check correct combination of multiple throttling events
163 activeSink.request();
164 BOOST_CHECK( ! tester.event.enabled() );
165 passiveSink.input.throttle();
166 BOOST_CHECK( ! tester.event.enabled() );
167 passiveSource.submit(p1);
168 BOOST_CHECK( ! tester.event.enabled() );
169 passiveSink.input.unthrottle();
170 BOOST_CHECK( tester.event.enabled() );
172 tester.rt->autoThrottling(false);
174 BOOST_CHECK( p1 == activeSink.request() );
175 BOOST_CHECK( passiveSource.output.throttled() );
176 BOOST_CHECK( activeSink );
179 ///////////////////////////////////////////////////
180 // test connection new modules on runtime
184 senf::scheduler::terminate();
187 // just a helper class for the test
188 struct ModuleConnector {
189 module::PriorityJoin & join_;
190 ModuleConnector( module::PriorityJoin & join)
193 module::PassiveQueue * queue = new module::PassiveQueue;
194 ppi::connect( *queue, join_, 0);
198 class TestSink : public module::Module
200 SENF_PPI_MODULE(TestSink);
202 connector::PassiveInput<> input;
205 input.onRequest(&TestSink::request);
209 (void) input.read().data();
214 BOOST_AUTO_UNIT_TEST(connect_runtime)
217 module::ActiveFeeder feeder;
218 module::PriorityJoin join;
219 module::CloneSource source1 (senf::DataPacket::create());
220 module::PassiveQueue queue;
222 ppi::connect( feeder, sink);
223 ppi::connect( join, feeder);
224 ppi::connect( source1, join);
226 ModuleConnector moduleConnector ( join);
227 senf::scheduler::TimerEvent timer ( "connect_runtime timer",
228 senf::membind(&ModuleConnector::connect, moduleConnector),
229 senf::ClockService::now() + senf::ClockService::milliseconds(25));
231 senf::scheduler::TimerEvent timeoutTimer (
232 "connect_runtime test timeoutTimer", &timeout,
233 senf::ClockService::now() + senf::ClockService::milliseconds(50));
240 ///////////////////////////////cc.e////////////////////////////////////////
247 // comment-column: 40
248 // c-file-style: "senf"
249 // indent-tabs-mode: nil
250 // ispell-local-dictionary: "american"
251 // compile-command: "scons -u test"