6d81ba165ed2143e104c529ada6ed295362fa129
[senf.git] / senf / 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 unit tests */
25
26 //#include "Route.test.hh"
27 //#include "Route.test.ih"
28
29 // Custom includes
30 #include <boost/scoped_ptr.hpp>
31 #include "Route.hh"
32 #include "DebugEvent.hh"
33 #include "DebugModules.hh"
34 #include "Module.hh"
35 #include "Setup.hh"
36 #include "CloneSource.hh"
37 #include "Joins.hh"
38 #include "PassiveQueue.hh"
39 #include <senf/Utils/membind.hh>
40 #include <senf/Utils/senfassert.hh>
41
42 #include <senf/Utils/auto_unit_test.hh>
43 #include <boost/test/test_tools.hpp>
44
45 #define prefix_
46 ///////////////////////////////cc.p////////////////////////////////////////
47
48 namespace ppi = senf::ppi;
49 namespace connector = ppi::connector;
50 namespace module = ppi::module;
51 namespace debug = module::debug;
52
53 namespace {
54     class RouteTester : public module::Module
55     {
56         SENF_PPI_MODULE(RouteTester);
57
58     public:
59         connector::ActiveInput<> activeIn;
60         connector::PassiveInput<> passiveIn;
61
62         connector::ActiveOutput<> activeOut;
63         connector::PassiveOutput<> passiveOut;
64
65         ppi::DebugEvent event;
66
67         ppi::ForwardingRoute * rt;
68
69         RouteTester() : events(0), throttles(0) {
70                    route( activeIn,  activeOut  );  // non-forwarding
71             rt = & route( activeIn,  passiveOut );  // forward throttling
72                    route( passiveIn, activeOut  );  // backward throttling
73                    route( passiveIn, passiveOut );  // non-forwarding
74                    route( event,     activeOut  );  // forward event throttling
75                    route( activeIn,  event      );  // backward event throttling
76
77             passiveIn.onRequest(&RouteTester::inputRequest);
78             passiveOut.onRequest(&RouteTester::outputRequest);
79             registerEvent(event, &RouteTester::onEvent);
80
81             activeIn.onThrottle(&RouteTester::throttleRequest);
82             activeIn.onUnthrottle(&RouteTester::unthrottleRequest);
83             activeOut.onThrottle(&RouteTester::throttleRequest);
84             activeOut.onUnthrottle(&RouteTester::unthrottleRequest);
85         }
86
87         void inputRequest() {
88             activeOut(passiveIn());
89         }
90
91         void outputRequest() {
92             passiveOut(activeIn());
93         }
94
95         void onEvent() {
96             ++ events;
97         }
98
99         void throttleRequest() {
100             ++ throttles;
101         }
102
103         void unthrottleRequest() {
104             -- throttles;
105         }
106
107         unsigned events;
108         int throttles;
109     };
110 }
111
112 SENF_AUTO_UNIT_TEST(route)
113 {
114     debug::PassiveSource passiveSource;
115     debug::ActiveSource activeSource;
116     debug::PassiveSink passiveSink;
117     debug::ActiveSink activeSink;
118     RouteTester tester;
119
120     ppi::connect(passiveSource, tester.activeIn);
121     ppi::connect(activeSource, tester.passiveIn);
122     ppi::connect(tester.activeOut, passiveSink);
123     ppi::connect(tester.passiveOut, activeSink);
124
125     ppi::init();
126
127     senf::Packet p1 (senf::DataPacket::create());
128     senf::Packet p2 (senf::DataPacket::create());
129
130     passiveSource.submit(p1);
131     activeSource.submit(p2);
132
133     BOOST_CHECK( p2 == passiveSink.front() );
134
135     // The passive source is not throttled at this point since it has packets in queue
136
137     passiveSink.input.throttle();
138     BOOST_CHECK( passiveSink.input.throttled() );
139     BOOST_CHECK( ! tester.activeOut );
140     BOOST_CHECK_EQUAL( tester.throttles, 1 );
141     BOOST_CHECK( tester.passiveIn.throttled() );
142     BOOST_CHECK( ! activeSource );
143     BOOST_CHECK( ! tester.event.enabled() );
144
145     passiveSink.input.unthrottle();
146     BOOST_CHECK( activeSource );
147     BOOST_CHECK( tester.event.enabled() );
148
149     // Now throttle the passive source by exhausting the queue
150
151     BOOST_CHECK( p1 == activeSink.request() );
152     BOOST_CHECK( passiveSource.output.throttled() );
153     BOOST_CHECK( ! tester.activeIn );
154     BOOST_CHECK_EQUAL( tester.throttles, 1 );
155     BOOST_CHECK( tester.passiveOut.throttled() );
156     BOOST_CHECK( ! activeSink );
157     BOOST_CHECK( ! tester.event.enabled() );
158
159     passiveSource.submit(p1);
160     BOOST_CHECK( activeSink );
161     BOOST_CHECK( tester.event.enabled() );
162
163     // Check correct combination of multiple throttling events
164
165     activeSink.request();
166     BOOST_CHECK( ! tester.event.enabled() );
167     passiveSink.input.throttle();
168     BOOST_CHECK( ! tester.event.enabled() );
169     passiveSource.submit(p1);
170     BOOST_CHECK( ! tester.event.enabled() );
171     passiveSink.input.unthrottle();
172     BOOST_CHECK( tester.event.enabled() );
173
174     tester.rt->autoThrottling(false);
175
176     BOOST_CHECK( p1 == activeSink.request() );
177     BOOST_CHECK( passiveSource.output.throttled() );
178     BOOST_CHECK( activeSink );
179 }
180
181 ///////////////////////////////////////////////////
182 // test connection new modules on runtime
183
184 namespace {
185     void timeout() {
186         senf::scheduler::terminate();
187     }
188
189     // just a helper class for the test
190     struct ModuleConnector {
191         module::PriorityJoin & join_;
192         ModuleConnector( module::PriorityJoin & join)
193             : join_( join) {};
194         void connect() {
195             queue.reset(new module::PassiveQueue);
196             ppi::connect( *queue, join_, 0);
197         }
198         boost::scoped_ptr<module::PassiveQueue> queue;
199     };
200
201     class TestSink : public module::Module
202     {
203         SENF_PPI_MODULE(TestSink);
204     public:
205         connector::PassiveInput<> input;
206         TestSink() {
207             noroute(input);
208             input.onRequest(&TestSink::request);
209         }
210     private:
211         void request() {
212             SENF_ASSERT(input(), "TestSink called without packet");
213         }
214     };
215 }
216
217 SENF_AUTO_UNIT_TEST(connect_runtime)
218 {
219     TestSink sink;
220     module::ActiveFeeder feeder;
221     module::PriorityJoin join;
222     module::CloneSource source1 (senf::DataPacket::create());
223
224     ppi::connect( source1, join);
225     ppi::connect( join, feeder);
226     ppi::connect( feeder, sink);
227
228     ModuleConnector moduleConnector ( join);
229     senf::scheduler::TimerEvent timer (
230         "connect_runtime timer",
231         senf::membind(&ModuleConnector::connect, &moduleConnector),
232         senf::ClockService::now() + senf::ClockService::milliseconds(250));
233
234     senf::scheduler::TimerEvent timeoutTimer (
235         "connect_runtime test timeoutTimer", &timeout,
236         senf::ClockService::now() + senf::ClockService::milliseconds(500));
237
238     senf::ppi::run();
239
240     BOOST_CHECK( true );
241 }
242
243 ///////////////////////////////cc.e////////////////////////////////////////
244 #undef prefix_
245
246 \f
247 // Local Variables:
248 // mode: c++
249 // fill-column: 100
250 // comment-column: 40
251 // c-file-style: "senf"
252 // indent-tabs-mode: nil
253 // ispell-local-dictionary: "american"
254 // compile-command: "scons -u test"
255 // End: