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"
30 #include <boost/scoped_ptr.hpp>
32 #include "DebugEvent.hh"
33 #include "DebugModules.hh"
36 #include "CloneSource.hh"
38 #include "PassiveQueue.hh"
39 #include <senf/Utils/membind.hh>
40 #include <senf/Utils/senfassert.hh>
42 #include <senf/Utils/auto_unit_test.hh>
43 #include <boost/test/test_tools.hpp>
46 ///////////////////////////////cc.p////////////////////////////////////////
48 namespace ppi = senf::ppi;
49 namespace connector = ppi::connector;
50 namespace module = ppi::module;
51 namespace debug = module::debug;
54 class RouteTester : public module::Module
56 SENF_PPI_MODULE(RouteTester);
59 connector::ActiveInput<> activeIn;
60 connector::PassiveInput<> passiveIn;
62 connector::ActiveOutput<> activeOut;
63 connector::PassiveOutput<> passiveOut;
65 ppi::DebugEvent event;
67 ppi::ForwardingRoute * rt;
69 RouteTester() : events(0), throttles(0) {
70 route( activeIn, activeOut ); // non-forwarding
71 rt = & route( activeIn, passiveOut ); // forward throttling
72 route( passiveIn, activeOut ); // backward throttling
73 route( passiveIn, passiveOut ); // non-forwarding
74 route( event, activeOut ); // forward event throttling
75 route( activeIn, event ); // backward event throttling
77 passiveIn.onRequest(&RouteTester::inputRequest);
78 passiveOut.onRequest(&RouteTester::outputRequest);
79 registerEvent(event, &RouteTester::onEvent);
81 activeIn.onThrottle(&RouteTester::throttleRequest);
82 activeIn.onUnthrottle(&RouteTester::unthrottleRequest);
83 activeOut.onThrottle(&RouteTester::throttleRequest);
84 activeOut.onUnthrottle(&RouteTester::unthrottleRequest);
88 activeOut(passiveIn());
91 void outputRequest() {
92 passiveOut(activeIn());
99 void throttleRequest() {
103 void unthrottleRequest() {
112 SENF_AUTO_UNIT_TEST(route)
114 debug::PassiveSource passiveSource;
115 debug::ActiveSource activeSource;
116 debug::PassiveSink passiveSink;
117 debug::ActiveSink activeSink;
120 ppi::connect(passiveSource, tester.activeIn);
121 ppi::connect(activeSource, tester.passiveIn);
122 ppi::connect(tester.activeOut, passiveSink);
123 ppi::connect(tester.passiveOut, activeSink);
127 senf::Packet p1 (senf::DataPacket::create());
128 senf::Packet p2 (senf::DataPacket::create());
130 passiveSource.submit(p1);
131 activeSource.submit(p2);
133 BOOST_CHECK( p2 == passiveSink.front() );
135 // The passive source is not throttled at this point since it has packets in queue
137 passiveSink.input.throttle();
138 BOOST_CHECK( passiveSink.input.throttled() );
139 BOOST_CHECK( ! tester.activeOut );
140 BOOST_CHECK_EQUAL( tester.throttles, 1 );
141 BOOST_CHECK( tester.passiveIn.throttled() );
142 BOOST_CHECK( ! activeSource );
143 BOOST_CHECK( ! tester.event.enabled() );
145 passiveSink.input.unthrottle();
146 BOOST_CHECK( activeSource );
147 BOOST_CHECK( tester.event.enabled() );
149 // Now throttle the passive source by exhausting the queue
151 BOOST_CHECK( p1 == activeSink.request() );
152 BOOST_CHECK( passiveSource.output.throttled() );
153 BOOST_CHECK( ! tester.activeIn );
154 BOOST_CHECK_EQUAL( tester.throttles, 1 );
155 BOOST_CHECK( tester.passiveOut.throttled() );
156 BOOST_CHECK( ! activeSink );
157 BOOST_CHECK( ! tester.event.enabled() );
159 passiveSource.submit(p1);
160 BOOST_CHECK( activeSink );
161 BOOST_CHECK( tester.event.enabled() );
163 // Check correct combination of multiple throttling events
165 activeSink.request();
166 BOOST_CHECK( ! tester.event.enabled() );
167 passiveSink.input.throttle();
168 BOOST_CHECK( ! tester.event.enabled() );
169 passiveSource.submit(p1);
170 BOOST_CHECK( ! tester.event.enabled() );
171 passiveSink.input.unthrottle();
172 BOOST_CHECK( tester.event.enabled() );
174 tester.rt->autoThrottling(false);
176 BOOST_CHECK( p1 == activeSink.request() );
177 BOOST_CHECK( passiveSource.output.throttled() );
178 BOOST_CHECK( activeSink );
181 ///////////////////////////////////////////////////
182 // test connection new modules on runtime
186 senf::scheduler::terminate();
189 // just a helper class for the test
190 struct ModuleConnector {
191 module::PriorityJoin & join_;
192 ModuleConnector( module::PriorityJoin & join)
195 queue.reset(new module::PassiveQueue);
196 ppi::connect( *queue, join_, 0);
198 boost::scoped_ptr<module::PassiveQueue> queue;
201 class TestSink : public module::Module
203 SENF_PPI_MODULE(TestSink);
205 connector::PassiveInput<> input;
208 input.onRequest(&TestSink::request);
212 SENF_ASSERT(input(), "TestSink called without packet");
217 SENF_AUTO_UNIT_TEST(connect_runtime)
220 module::ActiveFeeder feeder;
221 module::PriorityJoin join;
222 module::CloneSource source1 (senf::DataPacket::create());
224 ppi::connect( source1, join);
225 ppi::connect( join, feeder);
226 ppi::connect( feeder, sink);
228 ModuleConnector moduleConnector ( join);
229 senf::scheduler::TimerEvent timer (
230 "connect_runtime timer",
231 senf::membind(&ModuleConnector::connect, &moduleConnector),
232 senf::ClockService::now() + senf::ClockService::milliseconds(250));
234 senf::scheduler::TimerEvent timeoutTimer (
235 "connect_runtime test timeoutTimer", &timeout,
236 senf::ClockService::now() + senf::ClockService::milliseconds(500));
243 ///////////////////////////////cc.e////////////////////////////////////////
250 // comment-column: 40
251 // c-file-style: "senf"
252 // indent-tabs-mode: nil
253 // ispell-local-dictionary: "american"
254 // compile-command: "scons -u test"