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