4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief Route unit tests */
31 //#include "Route.test.hh"
32 //#include "Route.test.ih"
35 #include <boost/scoped_ptr.hpp>
37 #include "DebugEvent.hh"
38 #include "DebugModules.hh"
41 #include "CloneSource.hh"
43 #include "PassiveQueue.hh"
44 #include <senf/Utils/membind.hh>
45 #include <senf/Utils/senfassert.hh>
47 #include <senf/Utils/auto_unit_test.hh>
48 #include <boost/test/test_tools.hpp>
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
53 namespace ppi = senf::ppi;
54 namespace connector = ppi::connector;
55 namespace module = ppi::module;
56 namespace debug = module::debug;
59 class RouteTester : public module::Module
61 SENF_PPI_MODULE(RouteTester);
64 connector::ActiveInput<> activeIn;
65 connector::PassiveInput<> passiveIn;
67 connector::ActiveOutput<> activeOut;
68 connector::PassiveOutput<> passiveOut;
70 ppi::DebugEvent event;
72 ppi::ForwardingRoute * rt;
74 RouteTester() : events(0), throttles(0) {
75 route( activeIn, activeOut ); // non-forwarding
76 rt = & route( activeIn, passiveOut ); // forward throttling
77 route( passiveIn, activeOut ); // backward throttling
78 route( passiveIn, passiveOut ); // non-forwarding
79 route( event, activeOut ); // forward event throttling
80 route( activeIn, event ); // backward event throttling
82 passiveIn.onRequest(&RouteTester::inputRequest);
83 passiveOut.onRequest(&RouteTester::outputRequest);
84 registerEvent(event, &RouteTester::onEvent);
86 activeIn.onThrottle(&RouteTester::throttleRequest);
87 activeIn.onUnthrottle(&RouteTester::unthrottleRequest);
88 activeOut.onThrottle(&RouteTester::throttleRequest);
89 activeOut.onUnthrottle(&RouteTester::unthrottleRequest);
93 activeOut(passiveIn());
96 void outputRequest() {
97 passiveOut(activeIn());
104 void throttleRequest() {
108 void unthrottleRequest() {
117 SENF_AUTO_UNIT_TEST(route)
119 debug::PassiveSource passiveSource;
120 debug::ActiveSource activeSource;
121 debug::PassiveSink passiveSink;
122 debug::ActiveSink activeSink;
125 ppi::connect(passiveSource, tester.activeIn);
126 ppi::connect(activeSource, tester.passiveIn);
127 ppi::connect(tester.activeOut, passiveSink);
128 ppi::connect(tester.passiveOut, activeSink);
132 senf::Packet p1 (senf::DataPacket::create());
133 senf::Packet p2 (senf::DataPacket::create());
135 passiveSource.submit(p1);
136 activeSource.submit(p2);
138 BOOST_CHECK( p2 == passiveSink.front() );
140 // The passive source is not throttled at this point since it has packets in queue
142 passiveSink.input.throttle();
143 BOOST_CHECK( passiveSink.input.throttled() );
144 BOOST_CHECK( ! tester.activeOut );
145 BOOST_CHECK_EQUAL( tester.throttles, 1 );
146 BOOST_CHECK( tester.passiveIn.throttled() );
147 BOOST_CHECK( ! activeSource );
148 BOOST_CHECK( ! tester.event.enabled() );
150 passiveSink.input.unthrottle();
151 BOOST_CHECK( activeSource );
152 BOOST_CHECK( tester.event.enabled() );
154 // Now throttle the passive source by exhausting the queue
156 BOOST_CHECK( p1 == activeSink.request() );
157 BOOST_CHECK( passiveSource.output.throttled() );
158 BOOST_CHECK( ! tester.activeIn );
159 BOOST_CHECK_EQUAL( tester.throttles, 1 );
160 BOOST_CHECK( tester.passiveOut.throttled() );
161 BOOST_CHECK( ! activeSink );
162 BOOST_CHECK( ! tester.event.enabled() );
164 passiveSource.submit(p1);
165 BOOST_CHECK( activeSink );
166 BOOST_CHECK( tester.event.enabled() );
168 // Check correct combination of multiple throttling events
170 activeSink.request();
171 BOOST_CHECK( ! tester.event.enabled() );
172 passiveSink.input.throttle();
173 BOOST_CHECK( ! tester.event.enabled() );
174 passiveSource.submit(p1);
175 BOOST_CHECK( ! tester.event.enabled() );
176 passiveSink.input.unthrottle();
177 BOOST_CHECK( tester.event.enabled() );
179 tester.rt->autoThrottling(false);
181 BOOST_CHECK( p1 == activeSink.request() );
182 BOOST_CHECK( passiveSource.output.throttled() );
183 BOOST_CHECK( activeSink );
186 //-/////////////////////////////////////////////////////////////////////////////////////////////////
187 // test connection new modules on runtime
191 senf::scheduler::terminate();
194 // just a helper class for the test
195 struct ModuleConnector {
196 module::PriorityJoin & join_;
197 ModuleConnector( module::PriorityJoin & join)
200 queue.reset(new module::PassiveQueue);
201 ppi::connect( *queue, join_, 0);
203 boost::scoped_ptr<module::PassiveQueue> queue;
206 class TestSink : public module::Module
208 SENF_PPI_MODULE(TestSink);
210 connector::PassiveInput<> input;
213 input.onRequest(&TestSink::request);
217 SENF_ASSERT(input(), "TestSink called without packet");
222 SENF_AUTO_UNIT_TEST(connect_runtime)
225 module::ActiveFeeder feeder;
226 module::PriorityJoin join;
227 module::CloneSource source1 (senf::DataPacket::create());
229 ppi::connect( source1, join);
230 ppi::connect( join, feeder);
231 ppi::connect( feeder, sink);
233 ModuleConnector moduleConnector ( join);
234 senf::scheduler::TimerEvent timer (
235 "connect_runtime timer",
236 senf::membind(&ModuleConnector::connect, &moduleConnector),
237 senf::ClockService::now() + senf::ClockService::milliseconds(250));
239 senf::scheduler::TimerEvent timeoutTimer (
240 "connect_runtime test timeoutTimer", &timeout,
241 senf::ClockService::now() + senf::ClockService::milliseconds(500));
248 //-/////////////////////////////////////////////////////////////////////////////////////////////////
255 // comment-column: 40
256 // c-file-style: "senf"
257 // indent-tabs-mode: nil
258 // ispell-local-dictionary: "american"
259 // compile-command: "scons -u test"