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 Connectors unit tests */
26 //#include "Connectors.test.hh"
27 //#include "Connectors.test.ih"
30 #include "Connectors.hh"
31 #include "DebugModules.hh"
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
38 ///////////////////////////////cc.p////////////////////////////////////////
40 namespace ppi = senf::ppi;
41 namespace debug = ppi::module::debug;
43 // For each type of connector we use the corresponding debug module. Additionally, we always need
44 // the corresponding connected module since otherwise the connectors cannot be connected anywhere
45 // and will be unusable.
47 SENF_AUTO_UNIT_TEST(connector)
49 // It doesn't matter, which type of connectors we use here since they are all based on
52 debug::ActiveSource source;
53 debug::PassiveSink target;
55 ppi::connect(source.output,target.input);
58 BOOST_CHECK_EQUAL( & source.output.module(), & source );
59 BOOST_CHECK_EQUAL( & target.input.module(), & target );
60 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
61 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
64 SENF_AUTO_UNIT_TEST(passiveConnector)
66 debug::ActiveSource source;
67 debug::PassiveSink target;
69 ppi::connect(source.output,target.input);
72 // onRequest is implicitly tested within the PassiveSink implementation which is tested
73 // in DebugModules.test.cc
75 target.input.throttle();
76 BOOST_CHECK( target.input.throttled() );
77 BOOST_CHECK( target.input.nativeThrottled() );
79 target.input.unthrottle();
80 BOOST_CHECK( ! target.input.throttled() );
81 BOOST_CHECK( ! target.input.nativeThrottled() );
83 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
90 void handler() { called = true; }
93 SENF_AUTO_UNIT_TEST(activeConnector)
95 debug::ActiveSource source;
96 debug::PassiveSink target;
98 ppi::connect(source.output,target.input);
101 source.output.onThrottle(handler);
102 BOOST_CHECK( ! called );
103 target.input.throttle();
104 BOOST_CHECK( called );
106 target.input.unthrottle();
107 BOOST_CHECK( ! called );
108 source.output.onThrottle();
109 source.output.onUnthrottle(handler);
110 BOOST_CHECK( ! called );
111 target.input.throttle();
112 BOOST_CHECK( ! called );
113 target.input.unthrottle();
114 BOOST_CHECK( called );
115 source.output.onUnthrottle();
117 BOOST_CHECK( ! called );
118 target.input.throttle();
119 target.input.unthrottle();
120 BOOST_CHECK( ! called );
122 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
125 SENF_AUTO_UNIT_TEST(inputConnector)
127 debug::ActiveSource source;
128 debug::PassiveSink target;
130 ppi::connect(source.output,target.input);
133 // operator() is implicitly tested within the Active/PassiveSink implementation which is
134 // tested in DebugModules.test.cc
136 // peek() is implicitly tested within the Active/PassiveSink implementation
138 BOOST_CHECK_EQUAL ( & target.input.peer(), & source.output );
140 BOOST_CHECK( target.input.begin() == target.input.end() );
141 BOOST_CHECK_EQUAL( target.input.queueSize(), 0u );
142 BOOST_CHECK( target.input.empty() );
145 SENF_AUTO_UNIT_TEST(outputConnector)
147 debug::ActiveSource source;
148 debug::PassiveSink target;
150 ppi::connect(source.output,target.input);
153 // operator() is implicitly tested within the Active/PassiveSource implementation which is
154 // tested in DebugModules.test.cc
156 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
161 class PassiveInputTest
162 : public ppi::module::Module
164 SENF_PPI_MODULE(PassiveInputTest);
167 ppi::connector::PassiveInput<> input;
169 PassiveInputTest() : counter() {
171 input.onRequest(&PassiveInputTest::request);
182 SENF_AUTO_UNIT_TEST(passiveInput)
184 debug::ActiveSource source;
185 PassiveInputTest target;
187 ppi::connect(source,target);
190 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
192 target.input.throttle();
193 senf::Packet p (senf::DataPacket::create());
196 BOOST_CHECK_EQUAL( target.counter, 0u );
197 BOOST_CHECK( target.input );
198 BOOST_CHECK_EQUAL( target.input.queueSize(), 1u );
199 target.input.unthrottle();
200 BOOST_CHECK( target.input );
201 BOOST_CHECK_EQUAL( target.counter, 1u );
203 BOOST_CHECK( target.input() == p );
204 BOOST_CHECK( ! target.input );
208 BOOST_CHECK_EQUAL( target.counter, 2u );
209 BOOST_CHECK( target.input.throttled() );
210 BOOST_CHECK( target.input() == p );
211 BOOST_CHECK( ! target.input.throttled() );
213 target.input.qdisc(ppi::ThresholdQueueing(2,0));
216 BOOST_CHECK ( ! target.input.throttled() );
218 BOOST_CHECK( target.input.throttled() );
220 BOOST_CHECK( target.input.throttled() );
222 BOOST_CHECK( ! target.input.throttled() );
225 SENF_AUTO_UNIT_TEST(passiveOutput)
227 debug::PassiveSource source;
228 debug::ActiveSink target;
230 ppi::connect(source,target);
233 senf::Packet p (senf::DataPacket::create());
236 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
238 BOOST_CHECK( source.output );
241 BOOST_CHECK( target.request() == p );
243 // connect() is tested indirectly via ppi::connect
246 SENF_AUTO_UNIT_TEST(activeInput)
248 debug::PassiveSource source;
249 debug::ActiveSink target;
251 ppi::connect(source,target);
254 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
256 BOOST_CHECK ( ! target.input );
258 senf::Packet p (senf::DataPacket::create());
261 BOOST_CHECK( target.input );
262 BOOST_CHECK( target.request() == p );
265 target.input.request();
266 BOOST_CHECK_EQUAL( target.input.queueSize(), 1u );
267 BOOST_CHECK( target.input );
268 BOOST_CHECK( target.request() == p );
271 SENF_AUTO_UNIT_TEST(activeOutput)
273 debug::ActiveSource source;
274 debug::PassiveSink target;
276 ppi::connect(source,target);
279 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
280 BOOST_CHECK( source.output );
281 target.input.throttle();
282 BOOST_CHECK( ! source.output );
284 // connect() is tested indirectly via ppi::connect
289 template <class PacketType = senf::DataPacket>
290 class TypedPassiveInput
291 : public ppi::module::Module
293 SENF_PPI_MODULE(TypedPassiveInput);
296 ppi::connector::PassiveInput<PacketType> input;
298 TypedPassiveInput() {
300 input.onRequest(&TypedPassiveInput::request);
304 senf::IGNORE( input() );
305 senf::IGNORE( input.read() );
309 template <class PacketType = senf::DataPacket>
310 class TypedActiveInput
311 : public ppi::module::Module
313 SENF_PPI_MODULE(TypedActiveInput);
316 ppi::connector::ActiveInput<PacketType> input;
323 template <class PacketType = senf::DataPacket>
324 class TypedPassiveOutput
325 : public ppi::module::Module
327 SENF_PPI_MODULE(TypedPassiveOutput);
330 ppi::connector::PassiveOutput<PacketType> output;
332 TypedPassiveOutput() {
334 output.onRequest(&TypedPassiveOutput::request);
338 senf::DataPacket pkg (senf::DataPacket::create());
344 template <class PacketType = senf::DataPacket>
345 class TypedActiveOutput
346 : public ppi::module::Module
348 SENF_PPI_MODULE(TypedActiveOutput);
351 ppi::connector::ActiveOutput<PacketType> output;
353 TypedActiveOutput() {
358 struct MyPacketType : public senf::PacketTypeBase
361 typedef senf::ConcretePacket<MyPacketType> MyPacket;
365 SENF_AUTO_UNIT_TEST(typedInput)
367 debug::ActiveSource source;
368 TypedPassiveInput<> target;
370 ppi::connect(source,target);
373 senf::Packet p (senf::DataPacket::create());
379 SENF_AUTO_UNIT_TEST(tyepdOutput)
381 TypedPassiveOutput<> source;
382 debug::ActiveSink target;
384 ppi::connect(source,target);
387 senf::IGNORE( target.request() );
392 SENF_AUTO_UNIT_TEST(connectorTest)
395 TypedPassiveInput<> input;
396 TypedActiveOutput<MyPacket> output;
397 BOOST_CHECK_THROW( ppi::connect(output, input),
398 ppi::connector::IncompatibleConnectorsException );
401 TypedPassiveInput<MyPacket> input;
402 TypedActiveOutput<> output;
403 BOOST_CHECK_THROW( ppi::connect(output, input),
404 ppi::connector::IncompatibleConnectorsException );
407 TypedPassiveInput<> input;
408 TypedActiveOutput<> output;
409 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
412 TypedPassiveInput<> input;
413 debug::ActiveSource output;
414 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
417 debug::ActiveSink input;
418 TypedPassiveOutput<> output;
419 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
422 debug::ActiveSink input;
423 debug::PassiveSource output;
424 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
428 SENF_AUTO_UNIT_TEST(delayedConnect)
431 debug::PassiveSource source;
432 debug::ActiveSink target;
436 BOOST_CHECK( ! target.input );
437 BOOST_CHECK( ! target.request() );
439 ppi::connect(source, target);
442 BOOST_CHECK( ! target.input );
444 senf::Packet p (senf::DataPacket::create());
446 BOOST_CHECK( target.request() == p );
450 debug::PassiveSource source;
451 debug::ActiveSink target;
455 senf::Packet p (senf::DataPacket::create());
458 BOOST_CHECK( ! target.input );
459 BOOST_CHECK( ! target.request() );
461 ppi::connect(source, target);
464 BOOST_CHECK( target.input );
465 BOOST_CHECK( target.request() == p );
469 debug::ActiveSource source;
470 debug::PassiveSink target;
474 BOOST_CHECK( ! source.output );
475 SENF_CHECK_NO_THROW( source.output(senf::DataPacket::create()) );
477 ppi::connect(source, target);
480 BOOST_CHECK( source.output );
482 senf::Packet p (senf::DataPacket::create());
485 BOOST_CHECK( target.front() == p );
486 BOOST_CHECK_EQUAL( target.size(), 1u );
490 debug::ActiveSource source;
491 debug::PassiveSink target;
495 BOOST_CHECK( ! source.output );
496 SENF_CHECK_NO_THROW( source.output(senf::DataPacket::create()) );
499 ppi::connect(source, target);
502 BOOST_CHECK( ! source.output );
504 BOOST_CHECK( source.output );
508 SENF_AUTO_UNIT_TEST(disconnect)
511 debug::PassiveSource source;
512 debug::ActiveSink target;
514 ppi::connect(source, target);
517 BOOST_CHECK( ! target.input );
519 senf::Packet p (senf::DataPacket::create());
522 BOOST_CHECK( target.input );
524 target.input.disconnect();
527 BOOST_CHECK( ! target.input );
530 debug::ActiveSource source;
531 debug::PassiveSink target;
533 ppi::connect(source, target);
536 BOOST_CHECK( source.output );
538 source.output.disconnect();
541 BOOST_CHECK( ! source.output );
545 ///////////////////////////////cc.e////////////////////////////////////////
552 // comment-column: 40
553 // c-file-style: "senf"
554 // indent-tabs-mode: nil
555 // ispell-local-dictionary: "american"
556 // compile-command: "scons -u test"