Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / tests / Refcounted_Event_Handler_Test_DevPoll.cpp
blob591581b8951a69617100e192db911e78bb9b596b
2 //=============================================================================
3 /**
4 * @file Refcounted_Event_Handler_Test_DevPoll.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 //=============================================================================
14 #include "test_config.h"
15 #include "ace/OS_NS_string.h"
16 #include "ace/Reactor.h"
17 #include "ace/Select_Reactor.h"
18 #include "ace/WFMO_Reactor.h"
19 #include "ace/Dev_Poll_Reactor.h"
20 #include "ace/Pipe.h"
21 #include "ace/ACE.h"
24 #if defined (ACE_HAS_DEV_POLL) || defined (ACE_HAS_EVENT_POLL)
26 static const char *message =
27 "Hello there! Hope you get this message";
29 class Handler : public ACE_Event_Handler
31 public:
32 Handler (ACE_Reactor &reactor);
34 ~Handler() override;
36 int handle_timeout (const ACE_Time_Value &tv,
37 const void *arg) override;
39 int handle_input (ACE_HANDLE fd) override;
41 int handle_output (ACE_HANDLE fd) override;
43 ACE_HANDLE get_handle () const override;
45 // We need to add MSG_OOB data transfer to this test to check the
46 // order of when <handle_exception> gets called. I tried with
47 // Windows 2000 but only one byte of the message came across as
48 // urgent data. The rest of the data was treated as normal! There
49 // was some explanation of Microsoft's TCP/IP deals with out-of-band
50 // data in "Out-of-Band Data and Push Bit in TCP/IP" in the MSDN
51 // library. However, I did not comprehend that well enough. If
52 // someone can make this work, please check the order of
53 // <handle_exception> getting called.
54 // int handle_exception (ACE_HANDLE fd);
56 ACE_Pipe pipe_;
58 int dispatch_order_;
59 bool ok_; // Constructed and initialized ok
62 Handler::Handler (ACE_Reactor &reactor)
63 : ACE_Event_Handler (&reactor),
64 dispatch_order_ (1),
65 ok_ (false)
67 // Create the pipe.
68 if (0 != this->pipe_.open ())
69 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("pipe")));
70 else
72 // Register for all events.
73 if (0 != this->reactor ()->register_handler
74 (this->pipe_.read_handle (),
75 this,
76 ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK))
77 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("register")));
78 else
79 this->ok_ = true;
84 Handler::~Handler ()
86 this->pipe_.close ();
90 ACE_HANDLE
91 Handler::get_handle () const
93 return this->pipe_.read_handle ();
96 int
97 Handler::handle_timeout (const ACE_Time_Value &,
98 const void *)
100 int me = this->dispatch_order_++;
101 if (me != 1)
102 ACE_ERROR ((LM_ERROR,
103 ACE_TEXT ("handle_timeout should be #1; it's %d\n"),
104 me));
105 else
106 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Handler::handle_timeout\n")));
108 return 0;
112 Handler::handle_output (ACE_HANDLE)
114 int me = this->dispatch_order_++;
115 if (me != 2)
116 ACE_ERROR ((LM_ERROR,
117 ACE_TEXT ("handle_output should be #2; it's %d\n"),
118 me));
119 else
120 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Handler::handle_output\n")));
122 // Don't want to continually see writeable; only verify its relative order.
123 this->reactor ()->mask_ops (this->pipe_.read_handle (),
124 ACE_Event_Handler::WRITE_MASK,
125 ACE_Reactor::CLR_MASK);
127 return 0;
131 Handler::handle_input (ACE_HANDLE fd)
133 int me = this->dispatch_order_++;
134 if (me != 3)
135 ACE_ERROR ((LM_ERROR,
136 ACE_TEXT ("handle_input should be #3; it's %d\n"),
137 me));
139 char buffer[BUFSIZ];
140 ssize_t result = ACE::recv (fd, buffer, sizeof buffer);
141 if (result != ssize_t (ACE_OS::strlen (message)))
142 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Handler recv'd %b bytes; expected %B\n"),
143 result, ACE_OS::strlen (message)));
144 buffer[result] = '\0';
146 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Handler::handle_input: %C\n"), buffer));
148 if (ACE_OS::strcmp (buffer, message) != 0)
149 ACE_ERROR ((LM_ERROR,
150 ACE_TEXT ("Handler text mismatch; received \"%C\"; ")
151 ACE_TEXT ("expected \"%C\"\n"),
152 buffer, message));
154 this->reactor ()->end_reactor_event_loop ();
156 return 0;
159 static bool
160 test_reactor_dispatch_order (ACE_Reactor &reactor)
162 Handler handler (reactor);
163 if (!handler.ok_)
165 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error initializing test; abort.\n")));
166 return false;
169 bool ok_to_go = true;
171 // This should trigger a call to <handle_input>.
172 ssize_t result =
173 ACE::send_n (handler.pipe_.write_handle (),
174 message,
175 ACE_OS::strlen (message));
176 if (result != ssize_t (ACE_OS::strlen (message)))
178 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Handler sent %b bytes; should be %B\n"),
179 result, ACE_OS::strlen (message)));
180 ok_to_go = false;
183 // This should trigger a call to <handle_timeout>.
184 if (-1 == reactor.schedule_timer (&handler,
186 ACE_Time_Value (0)))
188 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
189 ok_to_go = false;
192 // Suspend the handlers - only the timer should be dispatched
193 ACE_Time_Value tv (1);
194 reactor.suspend_handlers ();
195 reactor.run_reactor_event_loop (tv);
197 // only the timer should have fired
198 if (handler.dispatch_order_ != 2)
200 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Incorrect number fired %d\n"),
201 handler.dispatch_order_));
202 ok_to_go = false;
205 // Reset the dispatch_order_ count and schedule another timer
206 handler.dispatch_order_ = 1;
207 if (-1 == reactor.schedule_timer (&handler,
209 ACE_Time_Value (0)))
211 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
212 ok_to_go = false;
215 // Resume the handlers - things should work now
216 reactor.resume_handlers ();
218 if (ok_to_go)
220 reactor.run_reactor_event_loop (tv);
223 if (0 != reactor.remove_handler (handler.pipe_.read_handle (),
224 ACE_Event_Handler::ALL_EVENTS_MASK |
225 ACE_Event_Handler::DONT_CALL))
226 ACE_ERROR ((LM_ERROR,
227 ACE_TEXT ("%p\n"),
228 ACE_TEXT ("remover_handler pipe")));
230 if (handler.dispatch_order_ != 4)
232 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Incorrect number fired %d\n"),
233 handler.dispatch_order_));
234 ok_to_go = false;
237 return ok_to_go;
241 run_main (int, ACE_TCHAR *[])
243 ACE_START_TEST (ACE_TEXT ("Refcounted_Event_Handler_Test_DevPoll"));
244 int result = 0;
246 ACE_Dev_Poll_Reactor dev_poll_reactor_impl;
247 ACE_Reactor dev_poll_reactor (&dev_poll_reactor_impl);
248 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Testing Dev Poll Reactor\n")));
249 if (!test_reactor_dispatch_order (dev_poll_reactor))
250 ++result;
252 ACE_END_TEST;
253 return result;
255 #else
257 run_main (int, ACE_TCHAR *[])
259 ACE_START_TEST (ACE_TEXT ("Refcounted_Event_Handler_Test_DevPoll"));
260 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_Dev_Poll_Reactor is UNSUPPORTED on this platform\n")));
261 ACE_END_TEST;
262 return 0;
264 #endif /* ACE_HAS_DEV_POLL || ACE_HAS_EVENT_POLL */