switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / MonitorModule.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 MonitorModule unit tests */
30
31 //#include "MonitorModule.test.hh"
32 //#include "MonitorModule.test.ih"
33
34 // Custom includes
35 #include "MonitorModule.hh"
36 #include "DebugModules.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace {
45
46     class PacketCounter : public senf::ppi::module::MonitorModule<>
47     {
48         SENF_PPI_MODULE(PacketCounter);
49     public:
50         PacketCounter() : count (0) {}
51         void v_handlePacket(senf::Packet const & p) { ++count; }
52         unsigned count;
53     };
54
55 }
56
57 SENF_AUTO_UNIT_TEST(monitorModulePassthrough)
58 {
59     senf::ppi::module::debug::ActiveSource source;
60     senf::ppi::module::debug::PassiveSink sink;
61     PacketCounter counter;
62
63     senf::ppi::connect(source, counter);
64     senf::ppi::connect(counter, sink);
65
66     senf::ppi::init();
67
68     senf::Packet p (senf::DataPacket::create());
69
70     BOOST_CHECK_EQUAL( counter.count, 0u );
71     source.submit(p);
72     BOOST_CHECK_EQUAL( counter.count, 1u );
73     BOOST_CHECK( sink.pop_front() == p );
74     BOOST_CHECK( source );
75     sink.throttle();
76     BOOST_CHECK( ! source );
77     sink.unthrottle();
78     BOOST_CHECK( source );
79 }
80
81 SENF_AUTO_UNIT_TEST(monitorModuleNoPassthrough)
82 {
83     senf::ppi::module::debug::ActiveSource source;
84     PacketCounter counter;
85
86     senf::ppi::connect(source, counter);
87
88     senf::ppi::init();
89
90     senf::Packet p (senf::DataPacket::create());
91
92     BOOST_CHECK_EQUAL( counter.count, 0u );
93     source.submit(p);
94     BOOST_CHECK_EQUAL( counter.count, 1u );
95     BOOST_CHECK( source );
96 }
97
98 SENF_AUTO_UNIT_TEST(monitorModuleDynamicConnect)
99 {
100     senf::ppi::module::debug::ActiveSource source;
101     PacketCounter counter;
102
103     senf::ppi::connect(source, counter);
104
105     senf::ppi::init();
106
107     senf::Packet p (senf::DataPacket::create());
108
109     BOOST_CHECK_EQUAL( counter.count, 0u );
110     source.submit(p);
111     BOOST_CHECK_EQUAL( counter.count, 1u );
112     BOOST_CHECK( source );
113
114     senf::ppi::module::debug::PassiveSink sink;
115     senf::ppi::connect(counter, sink);
116     senf::ppi::init(); // Needs to be called since I'm not running in the scheduler !!
117
118     BOOST_CHECK( source );
119     source.submit(p);
120     BOOST_CHECK_EQUAL( counter.count, 2u );
121     BOOST_CHECK( sink.pop_front() == p );
122     sink.throttle();
123     BOOST_CHECK( ! source );
124     sink.unthrottle();
125     BOOST_CHECK( source );
126
127     counter.output.disconnect();
128     senf::ppi::init(); // Needs to be called since I'm not running in the scheduler !!
129
130     BOOST_CHECK( source );
131     source.submit(p);
132     BOOST_CHECK_EQUAL( counter.count, 3u );
133
134 }
135
136 //-/////////////////////////////////////////////////////////////////////////////////////////////////
137 #undef prefix_
138
139 \f
140 // Local Variables:
141 // mode: c++
142 // fill-column: 100
143 // comment-column: 40
144 // c-file-style: "senf"
145 // indent-tabs-mode: nil
146 // ispell-local-dictionary: "american"
147 // compile-command: "scons -u test"
148 // End: