671844862402581e145f6765a0d74475671ff2ea
[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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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 SENF_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     ppi::connect(source1, join);
88     ppi::init();
89     BOOST_CHECK_EQUAL( join.connectors().size(), 2u);
90 }
91
92 SENF_AUTO_UNIT_TEST(priorityJoin)
93 {
94     debug::PassiveSource source1;
95     debug::PassiveSource source2;
96     module::PriorityJoin join;
97     debug::ActiveSink sink;
98
99     ppi::connect(source1, join);
100     ppi::connect(source2, join);
101     ppi::connect(join,sink);
102     ppi::init();
103
104     BOOST_CHECK( ! sink );
105
106     senf::Packet p1 (senf::DataPacket::create());
107     senf::Packet p2 (senf::DataPacket::create());
108
109     source1.submit(p1);
110     BOOST_CHECK( sink );
111     source2.submit(p2);
112     BOOST_CHECK( sink );
113     BOOST_CHECK( sink.request() == p1 );
114     BOOST_CHECK( sink );
115     BOOST_CHECK( sink.request() == p2 );
116     BOOST_CHECK( ! sink );
117
118     source1.submit(p1);
119     source2.submit(p2);
120     source1.submit(p1);
121     BOOST_CHECK( sink.request() == p1 );
122     BOOST_CHECK( sink.request() == p1 );
123     BOOST_CHECK( sink.request() == p2 );
124     BOOST_CHECK( ! sink );
125
126     debug::PassiveSource source3;
127     debug::PassiveSource source4;
128     ppi::connect(source3, join, 0);
129     ppi::connect(source4, join, -2);
130     // Ordering now: source3, source1, source4, source2
131
132     senf::Packet p3 (senf::DataPacket::create());
133     senf::Packet p4 (senf::DataPacket::create());
134
135     source4.submit(p4);
136     source3.submit(p3);
137     source2.submit(p2);
138     source1.submit(p1);
139     BOOST_CHECK( sink.request() == p3 );
140     BOOST_CHECK( sink.request() == p1 );
141     BOOST_CHECK( sink.request() == p4 );
142     BOOST_CHECK( sink.request() == p2 );
143     BOOST_CHECK( ! sink );
144 }
145
146 namespace {
147     struct ActiveJackSource
148     {
149         senf::ppi::connector::ActiveOutputJack<> output;
150
151         debug::ActiveSource source1;
152         debug::ActiveSource source2;
153
154         ActiveJackSource()
155             : output (source1.output) {}
156
157         void flip() {
158             output.reset( source2.output);
159         }
160     };
161 }
162
163 //SENF_AUTO_UNIT_TEST(jack_passiveJoin)
164 //{
165 //    ActiveJackSource jackSource;
166 //    PassiveJoin join;
167 //    debug::PassiveSink sink;
168 //
169 //    ppi::connect(jackSource, join);
170 //    ppi::connect(join, sink);
171 //    ppi::init();
172 //
173 //    senf::Packet p1 (senf::DataPacket::create());
174 //    senf::Packet p2 (senf::DataPacket::create());
175 //
176 //    jackSource.source1.submit( p1);
177 //    BOOST_CHECK_EQUAL( sink.pop_front(), p1);
178 //
179 //    jackSource.flip();
180 //
181 //    jackSource.source2.submit( p2);
182 //    BOOST_CHECK_EQUAL( sink.pop_front(), p2);
183 //}
184
185 //-/////////////////////////////////////////////////////////////////////////////////////////////////
186 #undef prefix_
187
188 \f
189 // Local Variables:
190 // mode: c++
191 // fill-column: 100
192 // comment-column: 40
193 // c-file-style: "senf"
194 // indent-tabs-mode: nil
195 // ispell-local-dictionary: "american"
196 // compile-command: "scons -u test"
197 // End: