0689e751ab6587299fd5143618eb838c7a00dd37
[senf.git] / senf / PPI / Connectors.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Connectors unit tests */
25
26 //#include "Connectors.test.hh"
27 //#include "Connectors.test.ih"
28
29 // Custom includes
30 #include "Connectors.hh"
31 #include "DebugModules.hh"
32 #include "Setup.hh"
33
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 namespace ppi = senf::ppi;
41 namespace debug = ppi::module::debug;
42
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.
46
47 SENF_AUTO_UNIT_TEST(connector)
48 {
49     // It doesn't matter, which type of connectors we use here since they are all based on
50     // Connector.
51
52     debug::ActiveSource source;
53     debug::PassiveSink target;
54
55     ppi::connect(source.output,target.input);
56     ppi::init();
57
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 );
62 }
63
64 SENF_AUTO_UNIT_TEST(passiveConnector)
65 {
66     debug::ActiveSource source;
67     debug::PassiveSink target;
68
69     ppi::connect(source.output,target.input);
70     ppi::init();
71
72     // onRequest is implicitly tested within the PassiveSink implementation which is tested
73     // in DebugModules.test.cc
74
75     target.input.throttle();
76     BOOST_CHECK( target.input.throttled() );
77     BOOST_CHECK( target.input.nativeThrottled() );
78
79     target.input.unthrottle();
80     BOOST_CHECK( ! target.input.throttled() );
81     BOOST_CHECK( ! target.input.nativeThrottled() );
82
83     BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
84 }
85
86 namespace {
87
88     bool called = false;
89
90     void handler() { called = true; }
91 }
92
93 SENF_AUTO_UNIT_TEST(activeConnector)
94 {
95     debug::ActiveSource source;
96     debug::PassiveSink target;
97
98     ppi::connect(source.output,target.input);
99     ppi::init();
100
101     source.output.onThrottle(handler);
102     BOOST_CHECK( ! called );
103     target.input.throttle();
104     BOOST_CHECK( called );
105     called = false;
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();
116     called = false;
117     BOOST_CHECK( ! called );
118     target.input.throttle();
119     target.input.unthrottle();
120     BOOST_CHECK( ! called );
121
122     BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
123 }
124
125 SENF_AUTO_UNIT_TEST(inputConnector)
126 {
127     debug::ActiveSource source;
128     debug::PassiveSink target;
129
130     ppi::connect(source.output,target.input);
131     ppi::init();
132
133     // operator() is implicitly tested within the Active/PassiveSink implementation which is
134     // tested in DebugModules.test.cc
135
136     // peek() is implicitly tested within the Active/PassiveSink implementation
137
138     BOOST_CHECK_EQUAL ( & target.input.peer(), & source.output );
139
140     BOOST_CHECK( target.input.begin() == target.input.end() );
141     BOOST_CHECK_EQUAL( target.input.queueSize(), 0u );
142     BOOST_CHECK( target.input.empty() );
143 }
144
145 SENF_AUTO_UNIT_TEST(outputConnector)
146 {
147     debug::ActiveSource source;
148     debug::PassiveSink target;
149
150     ppi::connect(source.output,target.input);
151     ppi::init();
152
153     // operator() is implicitly tested within the Active/PassiveSource implementation which is
154     // tested in DebugModules.test.cc
155
156     BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
157 }
158
159 namespace {
160
161     class PassiveInputTest
162         : public ppi::module::Module
163     {
164         SENF_PPI_MODULE(PassiveInputTest);
165
166     public:
167         ppi::connector::PassiveInput<> input;
168
169         PassiveInputTest() : counter() {
170             noroute(input);
171             input.onRequest(&PassiveInputTest::request);
172         }
173
174         void request() {
175             ++ counter;
176         }
177
178         unsigned counter;
179     };
180 }
181
182 SENF_AUTO_UNIT_TEST(passiveInput)
183 {
184     debug::ActiveSource source;
185     PassiveInputTest target;
186
187     ppi::connect(source,target);
188     ppi::init();
189
190     BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
191
192     target.input.throttle();
193     senf::Packet p (senf::DataPacket::create());
194     source.submit(p);
195
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 );
202
203     BOOST_CHECK( target.input() == p );
204     BOOST_CHECK( ! target.input );
205
206     source.submit(p);
207
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() );
212
213     target.input.qdisc(ppi::ThresholdQueueing(2,0));
214
215     source.submit(p);
216     BOOST_CHECK ( ! target.input.throttled() );
217     source.submit(p);
218     BOOST_CHECK( target.input.throttled() );
219     target.input();
220     BOOST_CHECK( target.input.throttled() );
221     target.input();
222     BOOST_CHECK( ! target.input.throttled() );
223 }
224
225 SENF_AUTO_UNIT_TEST(passiveOutput)
226 {
227     debug::PassiveSource source;
228     debug::ActiveSink target;
229
230     ppi::connect(source,target);
231     ppi::init();
232
233     senf::Packet p (senf::DataPacket::create());
234     source.submit(p);
235
236     BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
237
238     BOOST_CHECK( source.output );
239
240     source.submit(p);
241     BOOST_CHECK( target.request() == p );
242
243     // connect() is tested indirectly via ppi::connect
244 }
245
246 SENF_AUTO_UNIT_TEST(activeInput)
247 {
248     debug::PassiveSource source;
249     debug::ActiveSink target;
250
251     ppi::connect(source,target);
252     ppi::init();
253
254     BOOST_CHECK_EQUAL( & target.input.peer(), & source.output );
255
256     BOOST_CHECK ( ! target.input );
257
258     senf::Packet p (senf::DataPacket::create());
259     source.submit(p);
260
261     BOOST_CHECK( target.input );
262     BOOST_CHECK( target.request() == p );
263
264     source.submit(p);
265     target.input.request();
266     BOOST_CHECK_EQUAL( target.input.queueSize(), 1u );
267     BOOST_CHECK( target.input );
268     BOOST_CHECK( target.request() == p );
269 }
270
271 SENF_AUTO_UNIT_TEST(activeOutput)
272 {
273     debug::ActiveSource source;
274     debug::PassiveSink target;
275
276     ppi::connect(source,target);
277     ppi::init();
278
279     BOOST_CHECK_EQUAL( & source.output.peer(), & target.input );
280     BOOST_CHECK( source.output );
281     target.input.throttle();
282     BOOST_CHECK( ! source.output );
283
284     // connect() is tested indirectly via ppi::connect
285 }
286
287 namespace {
288
289     template <class PacketType = senf::DataPacket>
290     class TypedPassiveInput
291         : public ppi::module::Module
292     {
293         SENF_PPI_MODULE(TypedPassiveInput);
294
295     public:
296         ppi::connector::PassiveInput<PacketType> input;
297
298         TypedPassiveInput() {
299             noroute(input);
300             input.onRequest(&TypedPassiveInput::request);
301         }
302
303         void request() {
304             senf::IGNORE( input() );
305             senf::IGNORE( input.read() );
306         }
307     };
308
309     template <class PacketType = senf::DataPacket>
310     class TypedActiveInput
311         : public ppi::module::Module
312     {
313         SENF_PPI_MODULE(TypedActiveInput);
314
315     public:
316         ppi::connector::ActiveInput<PacketType> input;
317
318         TypedActiveInput() {
319             noroute(input);
320         }
321     };
322
323     template <class PacketType = senf::DataPacket>
324     class TypedPassiveOutput
325         : public ppi::module::Module
326     {
327         SENF_PPI_MODULE(TypedPassiveOutput);
328
329     public:
330         ppi::connector::PassiveOutput<PacketType> output;
331
332         TypedPassiveOutput() {
333             noroute(output);
334             output.onRequest(&TypedPassiveOutput::request);
335         }
336
337         void request() {
338             senf::DataPacket pkg (senf::DataPacket::create());
339             output(pkg);
340             output.write(pkg);
341         }
342     };
343
344     template <class PacketType = senf::DataPacket>
345     class TypedActiveOutput
346         : public ppi::module::Module
347     {
348         SENF_PPI_MODULE(TypedActiveOutput);
349
350     public:
351         ppi::connector::ActiveOutput<PacketType> output;
352
353         TypedActiveOutput() {
354             noroute(output);
355         }
356     };
357
358     struct MyPacketType : public senf::PacketTypeBase
359     {};
360
361     typedef senf::ConcretePacket<MyPacketType> MyPacket;
362
363 }
364
365 SENF_AUTO_UNIT_TEST(typedInput)
366 {
367     debug::ActiveSource source;
368     TypedPassiveInput<> target;
369
370     ppi::connect(source,target);
371     ppi::init();
372
373     senf::Packet p (senf::DataPacket::create());
374     source.submit(p);
375
376     BOOST_CHECK( true );
377 }
378
379 SENF_AUTO_UNIT_TEST(tyepdOutput)
380 {
381     TypedPassiveOutput<> source;
382     debug::ActiveSink target;
383
384     ppi::connect(source,target);
385     ppi::init();
386
387     senf::IGNORE( target.request() );
388
389     BOOST_CHECK( true );
390 }
391
392 SENF_AUTO_UNIT_TEST(connectorTest)
393 {
394     {
395         TypedPassiveInput<> input;
396         TypedActiveOutput<MyPacket> output;
397         BOOST_CHECK_THROW( ppi::connect(output, input),
398                            ppi::connector::IncompatibleConnectorsException );
399     }
400     {
401         TypedPassiveInput<MyPacket> input;
402         TypedActiveOutput<> output;
403         BOOST_CHECK_THROW( ppi::connect(output, input),
404                            ppi::connector::IncompatibleConnectorsException );
405     }
406     {
407         TypedPassiveInput<> input;
408         TypedActiveOutput<> output;
409         SENF_CHECK_NO_THROW( ppi::connect(output, input) );
410     }
411     {
412         TypedPassiveInput<> input;
413         debug::ActiveSource output;
414         SENF_CHECK_NO_THROW( ppi::connect(output, input) );
415     }
416     {
417         debug::ActiveSink input;
418         TypedPassiveOutput<> output;
419         SENF_CHECK_NO_THROW( ppi::connect(output, input) );
420     }
421     {
422         debug::ActiveSink input;
423         debug::PassiveSource output;
424         SENF_CHECK_NO_THROW( ppi::connect(output, input) );
425     }
426 }
427
428 SENF_AUTO_UNIT_TEST(delayedConnect)
429 {
430     {
431         debug::PassiveSource source;
432         debug::ActiveSink target;
433
434         ppi::init();
435
436         BOOST_CHECK( ! target.input );
437         BOOST_CHECK( ! target.request() );
438
439         ppi::connect(source, target);
440         ppi::init();
441
442         BOOST_CHECK( ! target.input );
443
444         senf::Packet p (senf::DataPacket::create());
445         source.submit(p);
446         BOOST_CHECK( target.request() == p );
447     }
448
449     {
450         debug::PassiveSource source;
451         debug::ActiveSink target;
452
453         ppi::init();
454
455         senf::Packet p (senf::DataPacket::create());
456         source.submit(p);
457
458         BOOST_CHECK( ! target.input );
459         BOOST_CHECK( ! target.request() );
460
461         ppi::connect(source, target);
462         ppi::init();
463
464         BOOST_CHECK( target.input );
465         BOOST_CHECK( target.request() == p );
466     }
467
468     {
469         debug::ActiveSource source;
470         debug::PassiveSink target;
471
472         ppi::init();
473
474         BOOST_CHECK( ! source.output );
475         SENF_CHECK_NO_THROW( source.output(senf::DataPacket::create()) );
476
477         ppi::connect(source, target);
478         ppi::init();
479
480         BOOST_CHECK( source.output );
481
482         senf::Packet p (senf::DataPacket::create());
483         source.submit(p);
484
485         BOOST_CHECK( target.front() == p );
486         BOOST_CHECK_EQUAL( target.size(), 1u );
487     }
488
489     {
490         debug::ActiveSource source;
491         debug::PassiveSink target;
492
493         ppi::init();
494
495         BOOST_CHECK( ! source.output );
496         SENF_CHECK_NO_THROW( source.output(senf::DataPacket::create()) );
497         target.throttle();
498
499         ppi::connect(source, target);
500         ppi::init();
501
502         BOOST_CHECK( ! source.output );
503         target.unthrottle();
504         BOOST_CHECK( source.output );
505     }
506 }
507
508 SENF_AUTO_UNIT_TEST(disconnect)
509 {
510     {
511         debug::PassiveSource source;
512         debug::ActiveSink target;
513
514         ppi::connect(source, target);
515         ppi::init();
516
517         BOOST_CHECK( ! target.input );
518
519         senf::Packet p (senf::DataPacket::create());
520         source.submit(p);
521
522         BOOST_CHECK( target.input );
523
524         target.input.disconnect();
525         ppi::init();
526
527         BOOST_CHECK( ! target.input );
528     }
529     {
530         debug::ActiveSource source;
531         debug::PassiveSink target;
532
533         ppi::connect(source, target);
534         ppi::init();
535
536         BOOST_CHECK( source.output );
537
538         source.output.disconnect();
539         ppi::init();
540
541         BOOST_CHECK( ! source.output );
542     }
543 }
544
545 //-/////////////////////////////////////////////////////////////////////////////////////////////////
546 #undef prefix_
547
548 \f
549 // Local Variables:
550 // mode: c++
551 // fill-column: 100
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"
557 // End: