2 //=============================================================================
4 * @file Reactor_Dispatch_Order_Test.cpp
6 * This is a simple test that checks the order of dispatching of
7 * ACE Reactors. Order should be: timeout, output, and then input.
9 * @author Irfan Pyarali <irfan@cs.wustl.edu>
11 //=============================================================================
13 #include "test_config.h"
14 #include "ace/OS_NS_string.h"
15 #include "ace/Reactor.h"
16 #include "ace/Select_Reactor.h"
17 #include "ace/WFMO_Reactor.h"
18 #include "ace/Dev_Poll_Reactor.h"
22 static const char *message
= "Hello there! Hope you get this message";
24 class Handler
: public ACE_Event_Handler
27 Handler (ACE_Reactor
&reactor
);
31 int handle_timeout (const ACE_Time_Value
&tv
,
32 const void *arg
) override
;
34 int handle_input (ACE_HANDLE fd
) override
;
36 int handle_output (ACE_HANDLE fd
) override
;
38 ACE_HANDLE
get_handle () const override
;
40 // We need to add MSG_OOB data transfer to this test to check the
41 // order of when <handle_exception> gets called. I tried with
42 // Windows 2000 but only one byte of the message came across as
43 // urgent data. The rest of the data was treated as normal! There
44 // was some explanation of Microsoft's TCP/IP deals with out-of-band
45 // data in "Out-of-Band Data and Push Bit in TCP/IP" in the MSDN
46 // library. However, I did not comprehend that well enough. If
47 // someone can make this work, please check the order of
48 // <handle_exception> getting called.
49 // int handle_exception (ACE_HANDLE fd);
54 bool ok_
; // Constructed and initialized ok
57 Handler::Handler (ACE_Reactor
&reactor
)
58 : ACE_Event_Handler (&reactor
),
63 if (0 != this->pipe_
.open ())
64 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("pipe")));
67 // Register for all events.
68 if (0 != this->reactor ()->register_handler
69 (this->pipe_
.read_handle (),
71 ACE_Event_Handler::READ_MASK
| ACE_Event_Handler::WRITE_MASK
))
72 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("register")));
86 Handler::get_handle () const
88 return this->pipe_
.read_handle ();
92 Handler::handle_timeout (const ACE_Time_Value
&,
95 int const me
= this->dispatch_order_
++;
98 ACE_TEXT ("handle_timeout should be #1; it's %d\n"),
101 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Handler::handle_timeout\n")));
107 Handler::handle_output (ACE_HANDLE
)
109 int const me
= this->dispatch_order_
++;
111 ACE_ERROR ((LM_ERROR
,
112 ACE_TEXT ("handle_output should be #2; it's %d\n"),
115 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Handler::handle_output\n")));
117 #if defined (__OpenBSD__) || defined (ACE_VXWORKS)
118 // All that we need written has been written, so don't
119 // call handle_output again.
120 this->reactor ()->mask_ops (this->pipe_
.read_handle (),
121 ACE_Event_Handler::WRITE_MASK
,
122 ACE_Reactor::CLR_MASK
);
123 #endif /* __OpenBSD__ || ACE_VXWORKS */
129 Handler::handle_input (ACE_HANDLE fd
)
131 int const me
= this->dispatch_order_
++;
133 ACE_ERROR ((LM_ERROR
,
134 ACE_TEXT ("handle_input should be #3; it's %d\n"),
138 ssize_t result
= ACE::recv (fd
, buffer
, sizeof buffer
);
139 if (result
!= ssize_t (ACE_OS::strlen (message
)))
140 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Handler recv'd %b bytes; expected %B\n"),
141 result
, ACE_OS::strlen (message
)));
142 buffer
[result
] = '\0';
144 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Handler::handle_input: %C\n"), buffer
));
146 if (ACE_OS::strcmp (buffer
, message
) != 0)
147 ACE_ERROR ((LM_ERROR
,
148 ACE_TEXT ("Handler text mismatch; received \"%C\"; ")
149 ACE_TEXT ("expected \"%C\"\n"),
152 this->reactor ()->end_reactor_event_loop ();
158 test_reactor_dispatch_order (ACE_Reactor
&reactor
)
160 Handler
handler (reactor
);
163 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Error initializing test; abort.\n")));
167 bool ok_to_go
= true;
169 // This should trigger a call to <handle_input>.
171 ACE::send_n (handler
.pipe_
.write_handle (),
173 ACE_OS::strlen (message
));
174 if (result
!= ssize_t (ACE_OS::strlen (message
)))
176 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Handler sent %b bytes; should be %B\n"),
177 result
, ACE_OS::strlen (message
)));
181 // This should trigger a call to <handle_timeout>.
182 if (-1 == reactor
.schedule_timer (&handler
,
186 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
190 // Suspend the handlers - only the timer should be dispatched
191 ACE_Time_Value
tv (1);
192 reactor
.suspend_handlers ();
193 if (0 != reactor
.run_reactor_event_loop (tv
))
195 ACE_ERROR ((LM_ERROR
,
197 ACE_TEXT ("run_reactor_event_loop")));
201 // only the timer should have fired
202 if (handler
.dispatch_order_
!= 2)
204 ACE_ERROR ((LM_ERROR
,
205 ACE_TEXT ("Incorrect number fired %d; expected 2\n"),
206 handler
.dispatch_order_
));
210 // Reset the dispatch_order_ count and schedule another timer
211 handler
.dispatch_order_
= 1;
212 if (-1 == reactor
.schedule_timer (&handler
,
216 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
220 // Resume the handlers - things should work now
221 reactor
.resume_handlers ();
225 if (0 != reactor
.run_reactor_event_loop (tv
))
227 ACE_ERROR ((LM_ERROR
,
229 ACE_TEXT ("run_reactor_event_loop 2")));
234 if (0 != reactor
.remove_handler (handler
.pipe_
.read_handle (),
235 ACE_Event_Handler::ALL_EVENTS_MASK
|
236 ACE_Event_Handler::DONT_CALL
))
237 ACE_ERROR ((LM_ERROR
,
239 ACE_TEXT ("remover_handler pipe")));
241 if (handler
.dispatch_order_
!= 4)
243 ACE_ERROR ((LM_ERROR
,
244 ACE_TEXT ("Incorrect number fired %d; expected 4\n"),
245 handler
.dispatch_order_
));
249 int nr_cancelled
= reactor
.cancel_timer (&handler
);
250 if (nr_cancelled
> 0)
251 ACE_ERROR ((LM_ERROR
,
252 ACE_TEXT ("Finishing test with %d timers still scheduled\n"),
258 run_main (int, ACE_TCHAR
*[])
260 ACE_START_TEST (ACE_TEXT ("Reactor_Dispatch_Order_Test"));
263 ACE_Select_Reactor select_reactor_impl
;
264 ACE_Reactor
select_reactor (&select_reactor_impl
);
265 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Testing ACE_Select_Reactor\n")));
266 if (!test_reactor_dispatch_order (select_reactor
))
269 // Winsock 2 things are needed for WFMO_Reactor.
270 #if defined (ACE_WIN32) && \
271 (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0))
273 ACE_WFMO_Reactor wfmo_reactor_impl
;
274 ACE_Reactor
wfmo_reactor (&wfmo_reactor_impl
);
275 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Testing ACE_WFMO_Reactor\n")));
276 if (!test_reactor_dispatch_order (wfmo_reactor
))
279 #endif /* ACE_WIN32 && ACE_HAS_WINSOCK2 */