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_EQUAL( target.input.queueSize(), 0u );
146 BOOST_CHECK( target.input.empty() );
149 SENF_AUTO_UNIT_TEST(outputConnector)
151 debug::ActiveSource source;
152 debug::PassiveSink target;
154 ppi::connect(source.output,target.input);
157 // operator() is implicitly tested within the Active/PassiveSource implementation which is
158 // tested in DebugModules.test.cc
160 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
165 class PassiveInputTest
166 : public ppi::module::Module
168 SENF_PPI_MODULE(PassiveInputTest);
171 ppi::connector::PassiveInput<> input;
173 PassiveInputTest() : counter() {
175 input.onRequest(&PassiveInputTest::request);
186 SENF_AUTO_UNIT_TEST(passiveInput)
188 debug::ActiveSource source;
189 PassiveInputTest target;
191 ppi::connect(source,target);
194 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
196 target.input.throttle();
197 senf::Packet p (senf::DataPacket::create());
200 BOOST_CHECK_EQUAL( target.counter, 0u );
201 BOOST_CHECK( target.input );
202 BOOST_CHECK_EQUAL( target.input.queueSize(), 1u );
203 target.input.unthrottle();
204 BOOST_CHECK( target.input );
205 BOOST_CHECK_EQUAL( target.counter, 1u );
207 BOOST_CHECK( target.input() == p );
208 BOOST_CHECK( ! target.input );
212 BOOST_CHECK_EQUAL( target.counter, 2u );
213 BOOST_CHECK( target.input.throttled() );
214 BOOST_CHECK( target.input() == p );
215 BOOST_CHECK( ! target.input.throttled() );
217 target.input.qdisc(ppi::ThresholdQueueing(2,0));
220 BOOST_CHECK ( ! target.input.throttled() );
222 BOOST_CHECK( target.input.throttled() );
224 BOOST_CHECK( target.input.throttled() );
226 BOOST_CHECK( ! target.input.throttled() );
229 SENF_AUTO_UNIT_TEST(passiveOutput)
231 debug::PassiveSource source;
232 debug::ActiveSink target;
234 ppi::connect(source,target);
237 senf::Packet p (senf::DataPacket::create());
240 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
242 BOOST_CHECK( source.output );
245 BOOST_CHECK( target.request() == p );
247 // connect() is tested indirectly via ppi::connect
250 SENF_AUTO_UNIT_TEST(activeInput)
252 debug::PassiveSource source;
253 debug::ActiveSink target;
255 ppi::connect(source,target);
258 BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
260 BOOST_CHECK ( ! target.input );
262 senf::Packet p (senf::DataPacket::create());
265 BOOST_CHECK( target.input );
266 BOOST_CHECK( target.request() == p );
269 target.input.request();
270 BOOST_CHECK_EQUAL( target.input.queueSize(), 1u );
271 BOOST_CHECK( target.input );
272 BOOST_CHECK( target.request() == p );
275 SENF_AUTO_UNIT_TEST(activeOutput)
277 debug::ActiveSource source;
278 debug::PassiveSink target;
280 ppi::connect(source,target);
283 BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
284 BOOST_CHECK( source.output );
285 target.input.throttle();
286 BOOST_CHECK( ! source.output );
288 // connect() is tested indirectly via ppi::connect
293 template <class PacketType = senf::DataPacket>
294 class TypedPassiveInput
295 : public ppi::module::Module
297 SENF_PPI_MODULE(TypedPassiveInput);
300 ppi::connector::PassiveInput<PacketType> input;
302 TypedPassiveInput() {
304 input.onRequest(&TypedPassiveInput::request);
308 senf::IGNORE( input() );
309 senf::IGNORE( input.read() );
313 template <class PacketType = senf::DataPacket>
314 class TypedActiveInput
315 : public ppi::module::Module
317 SENF_PPI_MODULE(TypedActiveInput);
320 ppi::connector::ActiveInput<PacketType> input;
327 template <class PacketType = senf::DataPacket>
328 class TypedPassiveOutput
329 : public ppi::module::Module
331 SENF_PPI_MODULE(TypedPassiveOutput);
334 ppi::connector::PassiveOutput<PacketType> output;
336 TypedPassiveOutput() {
338 output.onRequest(&TypedPassiveOutput::request);
342 senf::DataPacket pkg (senf::DataPacket::create());
348 template <class PacketType = senf::DataPacket>
349 class TypedActiveOutput
350 : public ppi::module::Module
352 SENF_PPI_MODULE(TypedActiveOutput);
355 ppi::connector::ActiveOutput<PacketType> output;
357 TypedActiveOutput() {
362 struct MyPacketType : public senf::PacketTypeBase
365 typedef senf::ConcretePacket<MyPacketType> MyPacket;
369 SENF_AUTO_UNIT_TEST(typedInput)
371 debug::ActiveSource source;
372 TypedPassiveInput<> target;
374 ppi::connect(source,target);
377 senf::Packet p (senf::DataPacket::create());
383 SENF_AUTO_UNIT_TEST(tyepdOutput)
385 TypedPassiveOutput<> source;
386 debug::ActiveSink target;
388 ppi::connect(source,target);
391 senf::IGNORE( target.request() );
396 SENF_AUTO_UNIT_TEST(connectorTest)
399 TypedPassiveInput<> input;
400 TypedActiveOutput<MyPacket> output;
401 BOOST_CHECK_THROW( ppi::connect(output, input),
402 ppi::connector::IncompatibleConnectorsException );
405 TypedPassiveInput<MyPacket> input;
406 TypedActiveOutput<> output;
407 BOOST_CHECK_THROW( ppi::connect(output, input),
408 ppi::connector::IncompatibleConnectorsException );
411 TypedPassiveInput<> input;
412 TypedActiveOutput<> output;
413 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
416 TypedPassiveInput<> input;
417 debug::ActiveSource output;
418 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
421 debug::ActiveSink input;
422 TypedPassiveOutput<> output;
423 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
426 debug::ActiveSink input;
427 debug::PassiveSource output;
428 SENF_CHECK_NO_THROW( ppi::connect(output, input) );
432 SENF_AUTO_UNIT_TEST(delayedConnect)
435 debug::PassiveSource source;
436 debug::ActiveSink target;
440 BOOST_CHECK( ! target.input );
441 BOOST_CHECK( ! target.request() );
443 ppi::connect(source, target);
446 BOOST_CHECK( ! target.input );
448 senf::Packet p (senf::DataPacket::create());
450 BOOST_CHECK( target.request() == p );
454 debug::PassiveSource source;
455 debug::ActiveSink target;
459 senf::Packet p (senf::DataPacket::create());
462 BOOST_CHECK( ! target.input );
463 BOOST_CHECK( ! target.request() );
465 ppi::connect(source, target);
468 BOOST_CHECK( target.input );
469 BOOST_CHECK( target.request() == p );
473 debug::ActiveSource source;
474 debug::PassiveSink target;
478 BOOST_CHECK( ! source.output );
479 SENF_CHECK_NO_THROW( source.output(senf::DataPacket::create()) );
481 ppi::connect(source, target);
484 BOOST_CHECK( source.output );
486 senf::Packet p (senf::DataPacket::create());
489 BOOST_CHECK( target.front() == p );
490 BOOST_CHECK_EQUAL( target.size(), 1u );
494 debug::ActiveSource source;
495 debug::PassiveSink target;
499 BOOST_CHECK( ! source.output );
500 SENF_CHECK_NO_THROW( source.output(senf::DataPacket::create()) );
503 ppi::connect(source, target);
506 BOOST_CHECK( ! source.output );
508 BOOST_CHECK( source.output );
512 SENF_AUTO_UNIT_TEST(disconnect)
515 debug::PassiveSource source;
516 debug::ActiveSink target;
518 ppi::connect(source, target);
521 BOOST_CHECK( ! target.input );
523 senf::Packet p (senf::DataPacket::create());
526 BOOST_CHECK( target.input );
528 target.input.disconnect();
531 BOOST_CHECK( ! target.input );
534 debug::ActiveSource source;
535 debug::PassiveSink target;
537 ppi::connect(source, target);
540 BOOST_CHECK( source.output );
542 source.output.disconnect();
545 BOOST_CHECK( ! source.output );
549 //-/////////////////////////////////////////////////////////////////////////////////////////////////
556 // comment-column: 40
557 // c-file-style: "senf"
558 // indent-tabs-mode: nil
559 // ispell-local-dictionary: "american"
560 // compile-command: "scons -u test"