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 Connectors unit tests */
31 //#include "Connectors.test.hh"
32 //#include "Connectors.test.ih"
35 #include "Connectors.hh"
36 #include "DebugModules.hh"
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 namespace ppi = senf::ppi;
46 namespace debug = ppi::module::debug;
48 // For each type of connector we use the corresponding debug module. Additionally, we always need
49 // the corresponding connected module since otherwise the connectors cannot be connected anywhere
50 // and will be unusable.
52 SENF_AUTO_UNIT_TEST(connector)
54 // It doesn't matter, which type of connectors we use here since they are all based on
57 debug::ActiveSource source;
58 debug::PassiveSink target;
60 ppi::connect(source.output,target.input);
63 BOOST_CHECK_EQUAL( & source.output.module(), & source );
64 BOOST_CHECK_EQUAL( & target.input.module(), & target );
65 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
66 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
69 SENF_AUTO_UNIT_TEST(passiveConnector)
71 debug::ActiveSource source;
72 debug::PassiveSink target;
74 ppi::connect(source.output,target.input);
77 // onRequest is implicitly tested within the PassiveSink implementation which is tested
78 // in DebugModules.test.cc
80 target.input.throttle();
81 BOOST_CHECK( target.input.throttled() );
82 BOOST_CHECK( target.input.nativeThrottled() );
84 target.input.unthrottle();
85 BOOST_CHECK( ! target.input.throttled() );
86 BOOST_CHECK( ! target.input.nativeThrottled() );
88 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
95 void handler() { called = true; }
98 SENF_AUTO_UNIT_TEST(activeConnector)
100 debug::ActiveSource source;
101 debug::PassiveSink target;
103 ppi::connect(source.output,target.input);
106 source.output.onThrottle(handler);
107 BOOST_CHECK( ! called );
108 target.input.throttle();
109 BOOST_CHECK( called );
111 target.input.unthrottle();
112 BOOST_CHECK( ! called );
113 source.output.onThrottle();
114 source.output.onUnthrottle(handler);
115 BOOST_CHECK( ! called );
116 target.input.throttle();
117 BOOST_CHECK( ! called );
118 target.input.unthrottle();
119 BOOST_CHECK( called );
120 source.output.onUnthrottle();
122 BOOST_CHECK( ! called );
123 target.input.throttle();
124 target.input.unthrottle();
125 BOOST_CHECK( ! called );
127 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
130 SENF_AUTO_UNIT_TEST(inputConnector)
132 debug::ActiveSource source;
133 debug::PassiveSink target;
135 ppi::connect(source.output,target.input);
138 // operator() is implicitly tested within the Active/PassiveSink implementation which is
139 // tested in DebugModules.test.cc
141 // peek() is implicitly tested within the Active/PassiveSink implementation
143 BOOST_CHECK_EQUAL ( & target.input.peer(), & source.output );
145 BOOST_CHECK( target.input.begin() == target.input.end() );
146 BOOST_CHECK_EQUAL( target.input.queueSize(), 0u );
147 BOOST_CHECK( target.input.empty() );
150 SENF_AUTO_UNIT_TEST(outputConnector)
152 debug::ActiveSource source;
153 debug::PassiveSink target;
155 ppi::connect(source.output,target.input);
158 // operator() is implicitly tested within the Active/PassiveSource implementation which is
159 // tested in DebugModules.test.cc
161 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
166 class PassiveInputTest
167 : public ppi::module::Module
169 SENF_PPI_MODULE(PassiveInputTest);
172 ppi::connector::PassiveInput<> input;
174 PassiveInputTest() : counter() {
176 input.onRequest(&PassiveInputTest::request);
187 SENF_AUTO_UNIT_TEST(passiveInput)
189 debug::ActiveSource source;
190 PassiveInputTest target;
192 ppi::connect(source,target);
195 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
197 target.input.throttle();
198 senf::Packet p (senf::DataPacket::create());
201 BOOST_CHECK_EQUAL( target.counter, 0u );
202 BOOST_CHECK( target.input );
203 BOOST_CHECK_EQUAL( target.input.queueSize(), 1u );
204 target.input.unthrottle();
205 BOOST_CHECK( target.input );
206 BOOST_CHECK_EQUAL( target.counter, 1u );
208 BOOST_CHECK( target.input() == p );
209 BOOST_CHECK( ! target.input );
213 BOOST_CHECK_EQUAL( target.counter, 2u );
214 BOOST_CHECK( target.input.throttled() );
215 BOOST_CHECK( target.input() == p );
216 BOOST_CHECK( ! target.input.throttled() );
218 target.input.qdisc(ppi::ThresholdQueueing(2,0));
221 BOOST_CHECK ( ! target.input.throttled() );
223 BOOST_CHECK( target.input.throttled() );
225 BOOST_CHECK( target.input.throttled() );
227 BOOST_CHECK( ! target.input.throttled() );
230 SENF_AUTO_UNIT_TEST(passiveOutput)
232 debug::PassiveSource source;
233 debug::ActiveSink target;
235 ppi::connect(source,target);
238 senf::Packet p (senf::DataPacket::create());
241 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
243 BOOST_CHECK( source.output );
246 BOOST_CHECK( target.request() == p );
248 // connect() is tested indirectly via ppi::connect
251 SENF_AUTO_UNIT_TEST(activeInput)
253 debug::PassiveSource source;
254 debug::ActiveSink target;
256 ppi::connect(source,target);
259 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
261 BOOST_CHECK ( ! target.input );
263 senf::Packet p (senf::DataPacket::create());
266 BOOST_CHECK( target.input );
267 BOOST_CHECK( target.request() == p );
270 target.input.request();
271 BOOST_CHECK_EQUAL( target.input.queueSize(), 1u );
272 BOOST_CHECK( target.input );
273 BOOST_CHECK( target.request() == p );
276 SENF_AUTO_UNIT_TEST(activeOutput)
278 debug::ActiveSource source;
279 debug::PassiveSink target;
281 ppi::connect(source,target);
284 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
285 BOOST_CHECK( source.output );
286 target.input.throttle();
287 BOOST_CHECK( ! source.output );
289 // connect() is tested indirectly via ppi::connect
294 template <class PacketType = senf::DataPacket>
295 class TypedPassiveInput
296 : public ppi::module::Module
298 SENF_PPI_MODULE(TypedPassiveInput);
301 ppi::connector::PassiveInput<PacketType> input;
303 TypedPassiveInput() {
305 input.onRequest(&TypedPassiveInput::request);
309 senf::IGNORE( input() );
310 senf::IGNORE( input.read() );
314 template <class PacketType = senf::DataPacket>
315 class TypedActiveInput
316 : public ppi::module::Module
318 SENF_PPI_MODULE(TypedActiveInput);
321 ppi::connector::ActiveInput<PacketType> input;
328 template <class PacketType = senf::DataPacket>
329 class TypedPassiveOutput
330 : public ppi::module::Module
332 SENF_PPI_MODULE(TypedPassiveOutput);
335 ppi::connector::PassiveOutput<PacketType> output;
337 TypedPassiveOutput() {
339 output.onRequest(&TypedPassiveOutput::request);
343 senf::DataPacket pkg (senf::DataPacket::create());
349 template <class PacketType = senf::DataPacket>
350 class TypedActiveOutput
351 : public ppi::module::Module
353 SENF_PPI_MODULE(TypedActiveOutput);
356 ppi::connector::ActiveOutput<PacketType> output;
358 TypedActiveOutput() {
363 struct MyPacketType : public senf::PacketTypeBase
366 typedef senf::ConcretePacket<MyPacketType> MyPacket;
370 SENF_AUTO_UNIT_TEST(typedInput)
372 debug::ActiveSource source;
373 TypedPassiveInput<> target;
375 ppi::connect(source,target);
378 senf::Packet p (senf::DataPacket::create());
384 SENF_AUTO_UNIT_TEST(tyepdOutput)
386 TypedPassiveOutput<> source;
387 debug::ActiveSink target;
389 ppi::connect(source,target);
392 senf::IGNORE( target.request() );
397 SENF_AUTO_UNIT_TEST(connectorTest)
400 TypedPassiveInput<> input;
401 TypedActiveOutput<MyPacket> output;
402 BOOST_CHECK_THROW( ppi::connect(output, input),
403 ppi::connector::IncompatibleConnectorsException );
406 TypedPassiveInput<MyPacket> input;
407 TypedActiveOutput<> output;
408 BOOST_CHECK_THROW( ppi::connect(output, input),
409 ppi::connector::IncompatibleConnectorsException );
412 TypedPassiveInput<> input;
413 TypedActiveOutput<> output;
414 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
417 TypedPassiveInput<> input;
418 debug::ActiveSource output;
419 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
422 debug::ActiveSink input;
423 TypedPassiveOutput<> output;
424 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
427 debug::ActiveSink input;
428 debug::PassiveSource output;
429 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
433 SENF_AUTO_UNIT_TEST(delayedConnect)
436 debug::PassiveSource source;
437 debug::ActiveSink target;
441 BOOST_CHECK( ! target.input );
442 BOOST_CHECK( ! target.request() );
444 ppi::connect(source, target);
447 BOOST_CHECK( ! target.input );
449 senf::Packet p (senf::DataPacket::create());
451 BOOST_CHECK( target.request() == p );
455 debug::PassiveSource source;
456 debug::ActiveSink target;
460 senf::Packet p (senf::DataPacket::create());
463 BOOST_CHECK( ! target.input );
464 BOOST_CHECK( ! target.request() );
466 ppi::connect(source, target);
469 BOOST_CHECK( target.input );
470 BOOST_CHECK( target.request() == p );
474 debug::ActiveSource source;
475 debug::PassiveSink target;
479 BOOST_CHECK( ! source.output );
480 SENF_CHECK_NO_THROW( source.output(senf::DataPacket::create()) );
482 ppi::connect(source, target);
485 BOOST_CHECK( source.output );
487 senf::Packet p (senf::DataPacket::create());
490 BOOST_CHECK( target.front() == p );
491 BOOST_CHECK_EQUAL( target.size(), 1u );
495 debug::ActiveSource source;
496 debug::PassiveSink target;
500 BOOST_CHECK( ! source.output );
501 SENF_CHECK_NO_THROW( source.output(senf::DataPacket::create()) );
504 ppi::connect(source, target);
507 BOOST_CHECK( ! source.output );
509 BOOST_CHECK( source.output );
513 SENF_AUTO_UNIT_TEST(disconnect)
516 debug::PassiveSource source;
517 debug::ActiveSink target;
519 ppi::connect(source, target);
522 BOOST_CHECK( ! target.input );
524 senf::Packet p (senf::DataPacket::create());
527 BOOST_CHECK( target.input );
529 target.input.disconnect();
532 BOOST_CHECK( ! target.input );
535 debug::ActiveSource source;
536 debug::PassiveSink target;
538 ppi::connect(source, target);
541 BOOST_CHECK( source.output );
543 source.output.disconnect();
546 BOOST_CHECK( ! source.output );
550 //-/////////////////////////////////////////////////////////////////////////////////////////////////
557 // comment-column: 40
558 // c-file-style: "senf"
559 // indent-tabs-mode: nil
560 // ispell-local-dictionary: "american"
561 // compile-command: "scons -u test"