PPI: Add optional template arg for packet type to connectors
[senf.git] / PPI / Route.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 Route.test unit tests */
25
26 //#include "Route.test.hh"
27 //#include "Route.test.ih"
28
29 // Custom includes
30 #include "Route.hh"
31 #include "DebugEvent.hh"
32 #include "DebugModules.hh"
33 #include "Module.hh"
34 #include "Setup.hh"
35
36 #include "../Utils/auto_unit_test.hh"
37 #include <boost/test/test_tools.hpp>
38
39 #define prefix_
40 ///////////////////////////////cc.p////////////////////////////////////////
41
42 namespace ppi = senf::ppi;
43 namespace connector = ppi::connector;
44 namespace module = ppi::module;
45 namespace debug = module::debug;
46
47 namespace {
48     class RouteTester : public module::Module
49     {
50         SENF_PPI_MODULE(RouteTester);
51
52     public:
53         connector::ActiveInput<> activeIn;
54         connector::PassiveInput<> passiveIn;
55
56         connector::ActiveOutput<> activeOut;
57         connector::PassiveOutput<> passiveOut;
58
59         ppi::DebugEvent event;
60
61         ppi::ForwardingRoute * rt;
62         
63         RouteTester() : events(0), throttles(0) {
64                    route( activeIn,  activeOut  );  // non-forwarding
65             rt = & route( activeIn,  passiveOut );  // forward throttling
66                    route( passiveIn, activeOut  );  // backward throttling
67                    route( passiveIn, passiveOut );  // non-forwarding
68                    route( event,     activeOut  );  // forward event throttling
69                    route( activeIn,  event      );  // backward event throttling
70
71             passiveIn.onRequest(&RouteTester::inputRequest);
72             passiveOut.onRequest(&RouteTester::outputRequest);
73             registerEvent(event, &RouteTester::onEvent);
74
75             activeIn.onThrottle(&RouteTester::throttleRequest);
76             activeIn.onUnthrottle(&RouteTester::unthrottleRequest);
77             activeOut.onThrottle(&RouteTester::throttleRequest);
78             activeOut.onUnthrottle(&RouteTester::unthrottleRequest);
79         }
80
81         void inputRequest() {
82             activeOut(passiveIn());
83         }
84
85         void outputRequest() {
86             passiveOut(activeIn());
87         }
88
89         void onEvent() {
90             ++ events;
91         }
92
93         void throttleRequest() {
94             ++ throttles;
95         }
96         
97         void unthrottleRequest() {
98             -- throttles;
99         }
100
101         unsigned events;
102         int throttles;
103     };
104 }
105
106 BOOST_AUTO_UNIT_TEST(route)
107 {
108     debug::PassiveSource passiveSource;
109     debug::ActiveSource activeSource;
110     debug::PassiveSink passiveSink;
111     debug::ActiveSink activeSink;
112     RouteTester tester;
113
114     ppi::connect(passiveSource, tester.activeIn);
115     ppi::connect(activeSource, tester.passiveIn);
116     ppi::connect(tester.activeOut, passiveSink);
117     ppi::connect(tester.passiveOut, activeSink);
118
119     ppi::init();
120
121     senf::Packet p1 (senf::DataPacket::create());
122     senf::Packet p2 (senf::DataPacket::create());
123
124     passiveSource.submit(p1);
125     activeSource.submit(p2);
126
127     BOOST_CHECK( p2 == passiveSink.front() );
128
129     // The passive source is not throttled at this point since it has packets in queue
130
131     passiveSink.input.throttle();
132     BOOST_CHECK( passiveSink.input.throttled() );
133     BOOST_CHECK( ! tester.activeOut );
134     BOOST_CHECK_EQUAL( tester.throttles, 1 );
135     BOOST_CHECK( tester.passiveIn.throttled() );
136     BOOST_CHECK( ! activeSource );
137     BOOST_CHECK( ! tester.event.enabled() );
138
139     passiveSink.input.unthrottle();
140     BOOST_CHECK( activeSource );
141     BOOST_CHECK( tester.event.enabled() );
142     
143     // Now throttle the passive source by exhausting the queue
144     
145     BOOST_CHECK( p1 == activeSink.request() );
146     BOOST_CHECK( passiveSource.output.throttled() );
147     BOOST_CHECK( ! tester.activeIn );
148     BOOST_CHECK_EQUAL( tester.throttles, 1 );
149     BOOST_CHECK( tester.passiveOut.throttled() );
150     BOOST_CHECK( ! activeSink );
151     BOOST_CHECK( ! tester.event.enabled() );
152     
153     passiveSource.submit(p1);
154     BOOST_CHECK( activeSink );
155     BOOST_CHECK( tester.event.enabled() );
156
157     // Check correct combination of multiple throttling events
158
159     activeSink.request();
160     BOOST_CHECK( ! tester.event.enabled() );
161     passiveSink.input.throttle();
162     BOOST_CHECK( ! tester.event.enabled() );
163     passiveSource.submit(p1);
164     BOOST_CHECK( ! tester.event.enabled() );
165     passiveSink.input.unthrottle();
166     BOOST_CHECK( tester.event.enabled() );
167
168     tester.rt->autoThrottling(false);
169
170     BOOST_CHECK( p1 == activeSink.request() );
171     BOOST_CHECK( passiveSource.output.throttled() );
172     BOOST_CHECK( activeSink );
173 }
174
175 ///////////////////////////////cc.e////////////////////////////////////////
176 #undef prefix_
177
178 \f
179 // Local Variables:
180 // mode: c++
181 // fill-column: 100
182 // comment-column: 40
183 // c-file-style: "senf"
184 // indent-tabs-mode: nil
185 // ispell-local-dictionary: "american"
186 // compile-command: "scons -u test"
187 // End: