switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / Module.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 Module unit tests */
30
31 //#include "Module.test.hh"
32 //#include "Module.test.ih"
33
34 // Custom includes
35 #include <boost/scoped_ptr.hpp>
36 #include <senf/Utils/membind.hh>
37 #include "DebugEvent.hh"
38 #include "DebugModules.hh"
39 #include "Setup.hh"
40 #include "Module.hh"
41
42 #include <senf/Utils/auto_unit_test.hh>
43 #include <boost/test/test_tools.hpp>
44 #include <boost/type_traits.hpp>
45
46 #define prefix_
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48
49 namespace ppi = senf::ppi;
50 namespace connector = ppi::connector;
51 namespace debug = ppi::module::debug;
52
53 namespace {
54     class TestModule : public ppi::module::Module
55     {
56         SENF_PPI_MODULE(TestModule);
57
58     public:
59         connector::ActiveOutput<> output;
60
61         ppi::DebugEvent event;
62
63         TestModule() {
64             noroute(output);
65             registerEvent(event, &TestModule::onEvent);
66         }
67
68         void onEvent() {
69             output(senf::DataPacket::create());
70         }
71
72         using ppi::module::Module::time;
73     };
74 }
75
76 SENF_AUTO_UNIT_TEST(module)
77 {
78     // route and registerEvent are tested in Route.test.cc
79
80     TestModule tester;
81     debug::PassiveSink sink;
82     ppi::connect(tester, sink);
83     ppi::init();
84
85     tester.event.trigger();
86     BOOST_CHECK_EQUAL( sink.size(), 1u );
87     BOOST_CHECK( senf::ClockService::now() - tester.time() < senf::ClockService::seconds(1) );
88 }
89
90 namespace {
91
92     void timeout() {
93         senf::scheduler::terminate();
94     }
95
96     class InitTest : public ppi::module::Module
97     {
98         SENF_PPI_MODULE(InitTest);
99     public:
100         InitTest() : init (false) {}
101         void v_init() { init = true; }
102
103         bool init;
104     };
105
106     struct MakeInit {
107         boost::scoped_ptr<InitTest> tester;
108         void make() {
109             tester.reset(new InitTest());
110         }
111         void test() {
112             BOOST_REQUIRE( tester );
113             BOOST_CHECK( tester->init );
114         }
115     };
116
117 }
118
119 SENF_AUTO_UNIT_TEST(delayedInit)
120 {
121     MakeInit maker;
122     senf::scheduler::TimerEvent timer (
123         "delayedInit timer",
124         senf::membind(&MakeInit::make, &maker),
125         senf::ClockService::now() + senf::ClockService::milliseconds(250) );
126     senf::scheduler::TimerEvent testTimer (
127         "delayedInit test",
128         senf::membind(&MakeInit::test, &maker),
129         senf::ClockService::now() + senf::ClockService::milliseconds(500) );
130     senf::scheduler::TimerEvent timeoutTimer (
131         "delayedInit timeout",
132         &timeout,
133         senf::ClockService::now() + senf::ClockService::milliseconds(750) );
134
135     senf::ppi::run();
136
137     BOOST_REQUIRE( maker.tester );
138     BOOST_CHECK( maker.tester->init );
139 }
140
141 //-/////////////////////////////////////////////////////////////////////////////////////////////////
142 #undef prefix_
143
144 \f
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // comment-column: 40
149 // c-file-style: "senf"
150 // indent-tabs-mode: nil
151 // ispell-local-dictionary: "american"
152 // compile-command: "scons -u test"
153 // End: