switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / Route.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Route unit tests */
30
31 //#include "Route.test.hh"
32 //#include "Route.test.ih"
33
34 // Custom includes
35 #include <boost/scoped_ptr.hpp>
36 #include "Route.hh"
37 #include "DebugEvent.hh"
38 #include "DebugModules.hh"
39 #include "Module.hh"
40 #include "Setup.hh"
41 #include "CloneSource.hh"
42 #include "Joins.hh"
43 #include "PassiveQueue.hh"
44 #include <senf/Utils/membind.hh>
45 #include <senf/Utils/senfassert.hh>
46
47 #include <senf/Utils/auto_unit_test.hh>
48 #include <boost/test/test_tools.hpp>
49
50 #define prefix_
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52
53 namespace ppi = senf::ppi;
54 namespace connector = ppi::connector;
55 namespace module = ppi::module;
56 namespace debug = module::debug;
57
58 namespace {
59     class RouteTester : public module::Module
60     {
61         SENF_PPI_MODULE(RouteTester);
62
63     public:
64         connector::ActiveInput<> activeIn;
65         connector::PassiveInput<> passiveIn;
66
67         connector::ActiveOutput<> activeOut;
68         connector::PassiveOutput<> passiveOut;
69
70         ppi::DebugEvent event;
71
72         ppi::ForwardingRoute * rt;
73
74         RouteTester() : events(0), throttles(0) {
75                    route( activeIn,  activeOut  );  // non-forwarding
76             rt = & route( activeIn,  passiveOut );  // forward throttling
77                    route( passiveIn, activeOut  );  // backward throttling
78                    route( passiveIn, passiveOut );  // non-forwarding
79                    route( event,     activeOut  );  // forward event throttling
80                    route( activeIn,  event      );  // backward event throttling
81
82             passiveIn.onRequest(&RouteTester::inputRequest);
83             passiveOut.onRequest(&RouteTester::outputRequest);
84             registerEvent(event, &RouteTester::onEvent);
85
86             activeIn.onThrottle(&RouteTester::throttleRequest);
87             activeIn.onUnthrottle(&RouteTester::unthrottleRequest);
88             activeOut.onThrottle(&RouteTester::throttleRequest);
89             activeOut.onUnthrottle(&RouteTester::unthrottleRequest);
90         }
91
92         void inputRequest() {
93             activeOut(passiveIn());
94         }
95
96         void outputRequest() {
97             passiveOut(activeIn());
98         }
99
100         void onEvent() {
101             ++ events;
102         }
103
104         void throttleRequest() {
105             ++ throttles;
106         }
107
108         void unthrottleRequest() {
109             -- throttles;
110         }
111
112         unsigned events;
113         int throttles;
114     };
115 }
116
117 SENF_AUTO_UNIT_TEST(route)
118 {
119     debug::PassiveSource passiveSource;
120     debug::ActiveSource activeSource;
121     debug::PassiveSink passiveSink;
122     debug::ActiveSink activeSink;
123     RouteTester tester;
124
125     ppi::connect(passiveSource, tester.activeIn);
126     ppi::connect(activeSource, tester.passiveIn);
127     ppi::connect(tester.activeOut, passiveSink);
128     ppi::connect(tester.passiveOut, activeSink);
129
130     ppi::init();
131
132     senf::Packet p1 (senf::DataPacket::create());
133     senf::Packet p2 (senf::DataPacket::create());
134
135     passiveSource.submit(p1);
136     activeSource.submit(p2);
137
138     BOOST_CHECK( p2 == passiveSink.front() );
139
140     // The passive source is not throttled at this point since it has packets in queue
141
142     passiveSink.input.throttle();
143     BOOST_CHECK( passiveSink.input.throttled() );
144     BOOST_CHECK( ! tester.activeOut );
145     BOOST_CHECK_EQUAL( tester.throttles, 1 );
146     BOOST_CHECK( tester.passiveIn.throttled() );
147     BOOST_CHECK( ! activeSource );
148     BOOST_CHECK( ! tester.event.enabled() );
149
150     passiveSink.input.unthrottle();
151     BOOST_CHECK( activeSource );
152     BOOST_CHECK( tester.event.enabled() );
153
154     // Now throttle the passive source by exhausting the queue
155
156     BOOST_CHECK( p1 == activeSink.request() );
157     BOOST_CHECK( passiveSource.output.throttled() );
158     BOOST_CHECK( ! tester.activeIn );
159     BOOST_CHECK_EQUAL( tester.throttles, 1 );
160     BOOST_CHECK( tester.passiveOut.throttled() );
161     BOOST_CHECK( ! activeSink );
162     BOOST_CHECK( ! tester.event.enabled() );
163
164     passiveSource.submit(p1);
165     BOOST_CHECK( activeSink );
166     BOOST_CHECK( tester.event.enabled() );
167
168     // Check correct combination of multiple throttling events
169
170     activeSink.request();
171     BOOST_CHECK( ! tester.event.enabled() );
172     passiveSink.input.throttle();
173     BOOST_CHECK( ! tester.event.enabled() );
174     passiveSource.submit(p1);
175     BOOST_CHECK( ! tester.event.enabled() );
176     passiveSink.input.unthrottle();
177     BOOST_CHECK( tester.event.enabled() );
178
179     tester.rt->autoThrottling(false);
180
181     BOOST_CHECK( p1 == activeSink.request() );
182     BOOST_CHECK( passiveSource.output.throttled() );
183     BOOST_CHECK( activeSink );
184 }
185
186 //-/////////////////////////////////////////////////////////////////////////////////////////////////
187 // test connection new modules on runtime
188
189 namespace {
190     void timeout() {
191         senf::scheduler::terminate();
192     }
193
194     // just a helper class for the test
195     struct ModuleConnector {
196         module::PriorityJoin & join_;
197         ModuleConnector( module::PriorityJoin & join)
198             : join_( join) {};
199         void connect() {
200             queue.reset(new module::PassiveQueue);
201             ppi::connect( *queue, join_, 0);
202         }
203         boost::scoped_ptr<module::PassiveQueue> queue;
204     };
205
206     class TestSink : public module::Module
207     {
208         SENF_PPI_MODULE(TestSink);
209     public:
210         connector::PassiveInput<> input;
211         TestSink() {
212             noroute(input);
213             input.onRequest(&TestSink::request);
214         }
215     private:
216         void request() {
217             SENF_ASSERT(input(), "TestSink called without packet");
218         }
219     };
220 }
221
222 SENF_AUTO_UNIT_TEST(connect_runtime)
223 {
224     TestSink sink;
225     module::ActiveFeeder feeder;
226     module::PriorityJoin join;
227     module::CloneSource source1 (senf::DataPacket::create());
228
229     ppi::connect( source1, join);
230     ppi::connect( join, feeder);
231     ppi::connect( feeder, sink);
232
233     ModuleConnector moduleConnector ( join);
234     senf::scheduler::TimerEvent timer (
235         "connect_runtime timer",
236         senf::membind(&ModuleConnector::connect, &moduleConnector),
237         senf::ClockService::now() + senf::ClockService::milliseconds(250));
238
239     senf::scheduler::TimerEvent timeoutTimer (
240         "connect_runtime test timeoutTimer", &timeout,
241         senf::ClockService::now() + senf::ClockService::milliseconds(500));
242
243     senf::ppi::run();
244
245     BOOST_CHECK( true );
246 }
247
248 //-/////////////////////////////////////////////////////////////////////////////////////////////////
249 #undef prefix_
250
251 \f
252 // Local Variables:
253 // mode: c++
254 // fill-column: 100
255 // comment-column: 40
256 // c-file-style: "senf"
257 // indent-tabs-mode: nil
258 // ispell-local-dictionary: "american"
259 // compile-command: "scons -u test"
260 // End: