08495a520babafba41d20513ab8f06fde95c4245
[senf.git] / senf / PPI / Module.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 Module unit tests */
25
26 //#include "Module.test.hh"
27 //#include "Module.test.ih"
28
29 // Custom includes
30 #include <boost/scoped_ptr.hpp>
31 #include <senf/Utils/membind.hh>
32 #include "DebugEvent.hh"
33 #include "DebugModules.hh"
34 #include "Setup.hh"
35 #include "Module.hh"
36
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39 #include <boost/type_traits.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace ppi = senf::ppi;
45 namespace connector = ppi::connector;
46 namespace debug = ppi::module::debug;
47
48 namespace {
49     class TestModule : public ppi::module::Module
50     {
51         SENF_PPI_MODULE(TestModule);
52
53     public:
54         connector::ActiveOutput<> output;
55
56         ppi::DebugEvent event;
57
58         TestModule() {
59             noroute(output);
60             registerEvent(event, &TestModule::onEvent);
61         }
62
63         void onEvent() {
64             output(senf::DataPacket::create());
65         }
66
67         using ppi::module::Module::time;
68     };
69 }
70
71 SENF_AUTO_UNIT_TEST(module)
72 {
73     // route and registerEvent are tested in Route.test.cc
74
75     TestModule tester;
76     debug::PassiveSink sink;
77     ppi::connect(tester, sink);
78     ppi::init();
79
80     tester.event.trigger();
81     BOOST_CHECK_EQUAL( sink.size(), 1u );
82     BOOST_CHECK( senf::ClockService::now() - tester.time() < senf::ClockService::seconds(1) );
83 }
84
85 namespace {
86
87     void timeout() {
88         senf::scheduler::terminate();
89     }
90
91     class InitTest : public ppi::module::Module
92     {
93         SENF_PPI_MODULE(InitTest);
94     public:
95         InitTest() : init (false) {}
96         void v_init() { init = true; }
97
98         bool init;
99     };
100
101     struct MakeInit {
102         boost::scoped_ptr<InitTest> tester;
103         void make() {
104             tester.reset(new InitTest());
105         }
106         void test() {
107             BOOST_REQUIRE( tester );
108             BOOST_CHECK( tester->init );
109         }
110     };
111
112 }
113
114 SENF_AUTO_UNIT_TEST(delayedInit)
115 {
116     MakeInit maker;
117     senf::scheduler::TimerEvent timer (
118         "delayedInit timer",
119         senf::membind(&MakeInit::make, &maker),
120         senf::ClockService::now() + senf::ClockService::milliseconds(250) );
121     senf::scheduler::TimerEvent testTimer (
122         "delayedInit test",
123         senf::membind(&MakeInit::test, &maker),
124         senf::ClockService::now() + senf::ClockService::milliseconds(500) );
125     senf::scheduler::TimerEvent timeoutTimer (
126         "delayedInit timeout",
127         &timeout,
128         senf::ClockService::now() + senf::ClockService::milliseconds(750) );
129
130     senf::ppi::run();
131
132     BOOST_REQUIRE( maker.tester );
133     BOOST_CHECK( maker.tester->init );
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: