PPI: Remove disconnected connectors from MultiConnectorMixin modules
[senf.git] / senf / PPI / Joins.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 Joins unit tests */
25
26 //#include "Joins.test.hh"
27 //#include "Joins.test.ih"
28
29 // Custom includes
30 #include "Joins.hh"
31 #include "DebugModules.hh"
32 #include "Setup.hh"
33 #include <senf/Packets/Packets.hh>
34
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 namespace ppi = senf::ppi;
42 namespace connector = ppi::connector;
43 namespace module = ppi::module;
44 namespace debug = module::debug;
45
46 namespace {
47
48     struct PassiveJoin : public module::PassiveJoin
49     {
50         using module::PassiveJoin::connectors;
51     };
52
53 }
54
55 BOOST_AUTO_UNIT_TEST(passiveJoin)
56 {
57     debug::ActiveSource source1;
58     debug::ActiveSource source2;
59     PassiveJoin join;
60     debug::PassiveSink sink;
61
62     ppi::connect(source1, join);
63     ppi::connect(source2, join);
64     ppi::connect(join, sink);
65     ppi::init();
66
67     senf::Packet p (senf::DataPacket::create());
68
69     source1.submit(p);
70     BOOST_CHECK_EQUAL( sink.size(), 1u );
71     source2.submit(p);
72     BOOST_CHECK_EQUAL( sink.size(), 2u );
73
74     sink.input.throttle();
75     BOOST_CHECK( ! source1 );
76     BOOST_CHECK( ! source2 );
77     
78     source1.submit(p);
79     source2.submit(p);
80     BOOST_CHECK_EQUAL( sink.size(), 2u );
81     sink.input.unthrottle();
82     BOOST_CHECK_EQUAL( sink.size(), 4u );
83
84     BOOST_CHECK_EQUAL( join.connectors().size(), 2u);
85     source1.output.disconnect();
86     BOOST_CHECK_EQUAL( join.connectors().size(), 1u);
87 }
88
89 BOOST_AUTO_UNIT_TEST(priorityJoin)
90 {
91     debug::PassiveSource source1;
92     debug::PassiveSource source2;
93     module::PriorityJoin join;
94     debug::ActiveSink sink;
95
96     ppi::connect(source1, join);
97     ppi::connect(source2, join);
98     ppi::connect(join,sink);
99     ppi::init();
100
101     BOOST_CHECK( ! sink );
102
103     senf::Packet p1 (senf::DataPacket::create());
104     senf::Packet p2 (senf::DataPacket::create());
105     
106     source1.submit(p1);
107     BOOST_CHECK( sink );
108     source2.submit(p2);
109     BOOST_CHECK( sink );
110     BOOST_CHECK( sink.request() == p1 );
111     BOOST_CHECK( sink );
112     BOOST_CHECK( sink.request() == p2 );
113     BOOST_CHECK( ! sink );
114
115     source1.submit(p1);
116     source2.submit(p2);
117     source1.submit(p1);
118     BOOST_CHECK( sink.request() == p1 );
119     BOOST_CHECK( sink.request() == p1 );
120     BOOST_CHECK( sink.request() == p2 );
121     BOOST_CHECK( ! sink );
122
123     debug::PassiveSource source3;
124     debug::PassiveSource source4;
125     ppi::connect(source3, join, 0);
126     ppi::connect(source4, join, -2);
127     // Ordering now: source3, source1, source4, source2
128  
129     senf::Packet p3 (senf::DataPacket::create());
130     senf::Packet p4 (senf::DataPacket::create());
131  
132     source4.submit(p4);
133     source3.submit(p3);
134     source2.submit(p2);
135     source1.submit(p1);
136     BOOST_CHECK( sink.request() == p3 );
137     BOOST_CHECK( sink.request() == p1 );
138     BOOST_CHECK( sink.request() == p4 );
139     BOOST_CHECK( sink.request() == p2 );
140     BOOST_CHECK( ! sink );
141 }
142
143 ///////////////////////////////cc.e////////////////////////////////////////
144 #undef prefix_
145
146 \f
147 // Local Variables:
148 // mode: c++
149 // fill-column: 100
150 // comment-column: 40
151 // c-file-style: "senf"
152 // indent-tabs-mode: nil
153 // ispell-local-dictionary: "american"
154 // compile-command: "scons -u test"
155 // End: