COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-eventhandler.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for common::EventHandler"
9 
10 #include <boost/test/unit_test.hpp>
11 #include <boost/thread.hpp>
12 
13 #include <iostream>
14 
15 #include "common/Core.hpp"
16 #include "common/OptionT.hpp"
17 #include "common/OptionURI.hpp"
19 #include "common/EventHandler.hpp"
21 
22 using namespace std;
23 using namespace boost;
24 using namespace cf3;
25 using namespace cf3::common;
26 using namespace cf3::common::XML;
27 
28 //------------------------------------------------------------------------------------------
29 // Auxiliary classes
30 
31 //------------------------------------------------------------------------------------------
32 
37 
39 {
40  void run()
41  {
42  SignalOptions options;
43 
44  options.add("iteration", 0u);
45 
46  for( Uint iter = 0; iter < 10; ++iter )
47  {
48 
49  std::cout << " - iter [" << iter << "]" << std::endl;
50 
51  // wait a bit
52 
53  boost::this_thread::sleep(boost::posix_time::milliseconds(100));
54 
55  // alter the iteration on the options
56 
57  options["iteration"].change_value( iter );
58 
59  // raise the event
60 
61  SignalFrame f = options.create_frame();
62  Core::instance().event_handler().raise_event( "iteration_done", f );
63  }
64  }
65 
66  void finish()
67  {
68  // would writes a file here...
69 
70  std::cout << " - writting results file " << std::endl;
71 
72  // raise the event
73 
74  SignalOptions options;
75 
76  options.add("file", URI("file:///localhost/path/to/file.txt") );
77 
78  SignalFrame f = options.create_frame();
79  Core::instance().event_handler().raise_event( "file_written", f );
80  }
81 
82 };
83 
84 //------------------------------------------------------------------------------------------
85 
90 
91 struct FileUpload : public ConnectionManager {
92 
93  FileUpload() : triggered(0)
94  {
95 
96  // this is a simple event registration
97  // the event function may be private
98  Core::instance().event_handler().connect_to_event( "file_written",
99  this,
101  }
102 
106  {
107  SignalOptions options( args );
108 
109  URI filepath = options.value<URI>("file");
110 
111  std::cout << " + EVENT + uploading file [" << filepath.string() << "]" << std::endl;
112 
113  ++triggered;
114  }
115 
117 
118 };
119 
120 //------------------------------------------------------------------------------------------
121 
126 
128 
129  DisplayIter() : triggered(0) {}
130 
134  {
135  SignalOptions options( args );
136 
137  Uint current_iter = options.value<Uint>("iteration");
138 
139  display_iteration( current_iter );
140 
141  ++triggered;
142  }
143 
144  void display_iteration( Uint iter )
145  {
146  std::cout << " + EVENT + current iteration [" << iter << "]" << std::endl;
147  }
148 
150  {
151  Core::instance().event_handler().connect_to_event( "iteration_done",
152  this,
154  }
155 
157  {
158  connection("iteration_done")->disconnect();
159  }
160 
162 
163 };
164 
165 //------------------------------------------------------------------------------------------
166 // test fixtures
167 
169 {
171  {
172  Core::instance().initiate(boost::unit_test::framework::master_test_suite().argc,
173  boost::unit_test::framework::master_test_suite().argv);
174  }
175 
177  {
178  Core::instance().terminate();
179  }
180 };
181 
183 {
187 };
188 
189 //------------------------------------------------------------------------------------------
190 
192 
193 BOOST_AUTO_TEST_SUITE( event_handler_test_suite )
194 
195 //------------------------------------------------------------------------------------------
196 
197 #if 1
198 
200 {
201  std::cout << "raise_event" << std::endl;
202 
203  // run but dont react on events
204 
205  algo.run();
206 
207  BOOST_CHECK ( display.triggered == 0 );
208  BOOST_CHECK ( writer.triggered == 0 );
209 
210  // finish and write file
211 
212  algo.finish();
213 
214  BOOST_CHECK ( display.triggered == 0 );
215  BOOST_CHECK ( writer.triggered == 1 );
216 }
217 
218 #endif
219 
220 #if 1
221 
223 {
224  std::cout << "connect_to_event" << std::endl;
225 
226  // run but dont react on events
227 
228  algo.run();
229 
230  BOOST_CHECK ( display.triggered == 0 );
231  BOOST_CHECK ( writer.triggered == 0 );
232 
233  // start displaying on events
234 
235  display.start_displaying();
236 
237  algo.run();
238 
239  BOOST_CHECK ( display.triggered == 10 );
240  BOOST_CHECK ( writer.triggered == 0 );
241 
242  // finish and write file
243 
244  algo.finish();
245 
246  BOOST_CHECK ( display.triggered == 10 );
247  BOOST_CHECK ( writer.triggered == 1 );
248 }
249 
250 #endif
251 
252 #if 1
253 
254 BOOST_FIXTURE_TEST_CASE( disconnect_from_event, local_fixture )
255 {
256  std::cout << "disconnect_from_event" << std::endl;
257 
258  // start displaying on events
259 
260  display.start_displaying();
261 
262  algo.run();
263 
264  BOOST_CHECK ( display.triggered == 10 );
265  BOOST_CHECK ( writer.triggered == 0 );
266 
267  // stop displaying
268 
269  display.stop_displaying();
270 
271  algo.run();
272 
273  BOOST_CHECK ( display.triggered == 10 );
274  BOOST_CHECK ( writer.triggered == 0 );
275 }
276 
277 #endif
278 
279 //------------------------------------------------------------------------------------------
280 
281 BOOST_AUTO_TEST_SUITE_END()
282 
283 //------------------------------------------------------------------------------------------
Abstracts the use of XML when adding options to a signal frame.
void display_iteration(Uint iter)
external boost library namespace
Classes that implement the XML protocol for use in COOLFluiD.
Definition: Component.hpp:43
void on_file_written(SignalArgs &args)
void on_iteration_done(SignalArgs &args)
STL namespace.
IterativeAlgo algo
SignalFrame create_frame(const std::string &name=std::string(), const URI &sender=URI(), const URI &receiver=URI()) const
Creates a frame and puts the options in an "options" map.
Uint triggered
for unit test to check how often the event was triggered
Manages a set of maps.
Definition: SignalFrame.hpp:31
BOOST_FIXTURE_TEST_CASE(raise_event, local_fixture)
Uint triggered
for unit test to check how often the event was triggered
Top-level namespace for coolfluid.
Definition: Action.cpp:18
const TYPE value(const std::string &opt_name) const
Get the value of the option with given name.
Definition: OptionList.hpp:104
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
BOOST_GLOBAL_FIXTURE(GlobalFixture) BOOST_AUTO_TEST_CASE(MeshStats)
Accumulate some statistics about the cell volumes.
SelectOptionType< T >::type & add(const std::string &name, const T &default_value=T())
Definition: OptionList.hpp:45
Most basic kernel library.
Definition: Action.cpp:19
std::string string() const
Definition: URI.cpp:258
Send comments to:
COOLFluiD Web Admin