Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Refcounted_Event_Handler_Test_DevPoll.cpp
blobd0f233993e810e0220b40af12d10ae8370c4b3c5
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"
25 #if defined (ACE_HAS_DEV_POLL) || defined (ACE_HAS_EVENT_POLL)
27 static const char *message =
28 "Hello there! Hope you get this message";
30 class Handler : public ACE_Event_Handler
32 public:
33 Handler (ACE_Reactor &reactor);
35 ~Handler();
37 int handle_timeout (const ACE_Time_Value &tv,
38 const void *arg);
40 int handle_input (ACE_HANDLE fd);
42 int handle_output (ACE_HANDLE fd);
44 ACE_HANDLE get_handle (void) const;
46 // We need to add MSG_OOB data transfer to this test to check the
47 // order of when <handle_exception> gets called. I tried with
48 // Windows 2000 but only one byte of the message came across as
49 // urgent data. The rest of the data was treated as normal! There
50 // was some explanation of Microsoft's TCP/IP deals with out-of-band
51 // data in "Out-of-Band Data and Push Bit in TCP/IP" in the MSDN
52 // library. However, I did not comprehend that well enough. If
53 // someone can make this work, please check the order of
54 // <handle_exception> getting called.
55 // int handle_exception (ACE_HANDLE fd);
57 ACE_Pipe pipe_;
59 int dispatch_order_;
60 bool ok_; // Constructed and initialized ok
63 Handler::Handler (ACE_Reactor &reactor)
64 : ACE_Event_Handler (&reactor),
65 dispatch_order_ (1),
66 ok_ (false)
68 // Create the pipe.
69 if (0 != this->pipe_.open ())
70 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("pipe")));
71 else
73 // Register for all events.
74 if (0 != this->reactor ()->register_handler
75 (this->pipe_.read_handle (),
76 this,
77 ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK))
78 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("register")));
79 else
80 this->ok_ = true;
85 Handler::~Handler (void)
87 this->pipe_.close ();
91 ACE_HANDLE
92 Handler::get_handle (void) const
94 return this->pipe_.read_handle ();
97 int
98 Handler::handle_timeout (const ACE_Time_Value &,
99 const void *)
101 int me = this->dispatch_order_++;
102 if (me != 1)
103 ACE_ERROR ((LM_ERROR,
104 ACE_TEXT ("handle_timeout should be #1; it's %d\n"),
105 me));
106 else
107 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Handler::handle_timeout\n")));
109 return 0;
113 Handler::handle_output (ACE_HANDLE)
115 int me = this->dispatch_order_++;
116 if (me != 2)
117 ACE_ERROR ((LM_ERROR,
118 ACE_TEXT ("handle_output should be #2; it's %d\n"),
119 me));
120 else
121 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Handler::handle_output\n")));
123 // Don't want to continually see writeable; only verify its relative order.
124 this->reactor ()->mask_ops (this->pipe_.read_handle (),
125 ACE_Event_Handler::WRITE_MASK,
126 ACE_Reactor::CLR_MASK);
128 return 0;
132 Handler::handle_input (ACE_HANDLE fd)
134 int me = this->dispatch_order_++;
135 if (me != 3)
136 ACE_ERROR ((LM_ERROR,
137 ACE_TEXT ("handle_input should be #3; it's %d\n"),
138 me));
140 char buffer[BUFSIZ];
141 ssize_t result = ACE::recv (fd, buffer, sizeof buffer);
142 if (result != ssize_t (ACE_OS::strlen (message)))
143 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Handler recv'd %b bytes; expected %B\n"),
144 result, ACE_OS::strlen (message)));
145 buffer[result] = '\0';
147 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Handler::handle_input: %C\n"), buffer));
149 if (ACE_OS::strcmp (buffer, message) != 0)
150 ACE_ERROR ((LM_ERROR,
151 ACE_TEXT ("Handler text mismatch; received \"%C\"; ")
152 ACE_TEXT ("expected \"%C\"\n"),
153 buffer, message));
155 this->reactor ()->end_reactor_event_loop ();
157 return 0;
160 static bool
161 test_reactor_dispatch_order (ACE_Reactor &reactor)
163 Handler handler (reactor);
164 if (!handler.ok_)
166 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error initializing test; abort.\n")));
167 return false;
170 bool ok_to_go = true;
172 // This should trigger a call to <handle_input>.
173 ssize_t result =
174 ACE::send_n (handler.pipe_.write_handle (),
175 message,
176 ACE_OS::strlen (message));
177 if (result != ssize_t (ACE_OS::strlen (message)))
179 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Handler sent %b bytes; should be %B\n"),
180 result, ACE_OS::strlen (message)));
181 ok_to_go = false;
184 // This should trigger a call to <handle_timeout>.
185 if (-1 == reactor.schedule_timer (&handler,
187 ACE_Time_Value (0)))
189 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
190 ok_to_go = false;
193 // Suspend the handlers - only the timer should be dispatched
194 ACE_Time_Value tv (1);
195 reactor.suspend_handlers ();
196 reactor.run_reactor_event_loop (tv);
198 // only the timer should have fired
199 if (handler.dispatch_order_ != 2)
201 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Incorrect number fired %d\n"),
202 handler.dispatch_order_));
203 ok_to_go = false;
206 // Reset the dispatch_order_ count and schedule another timer
207 handler.dispatch_order_ = 1;
208 if (-1 == reactor.schedule_timer (&handler,
210 ACE_Time_Value (0)))
212 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
213 ok_to_go = false;
216 // Resume the handlers - things should work now
217 reactor.resume_handlers ();
219 if (ok_to_go)
221 reactor.run_reactor_event_loop (tv);
224 if (0 != reactor.remove_handler (handler.pipe_.read_handle (),
225 ACE_Event_Handler::ALL_EVENTS_MASK |
226 ACE_Event_Handler::DONT_CALL))
227 ACE_ERROR ((LM_ERROR,
228 ACE_TEXT ("%p\n"),
229 ACE_TEXT ("remover_handler pipe")));
231 if (handler.dispatch_order_ != 4)
233 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Incorrect number fired %d\n"),
234 handler.dispatch_order_));
235 ok_to_go = false;
238 return ok_to_go;
242 run_main (int, ACE_TCHAR *[])
244 ACE_START_TEST (ACE_TEXT ("Refcounted_Event_Handler_Test_DevPoll"));
245 int result = 0;
247 ACE_Dev_Poll_Reactor dev_poll_reactor_impl;
248 ACE_Reactor dev_poll_reactor (&dev_poll_reactor_impl);
249 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Testing Dev Poll Reactor\n")));
250 if (!test_reactor_dispatch_order (dev_poll_reactor))
251 ++result;
253 ACE_END_TEST;
254 return result;
256 #else
258 run_main (int, ACE_TCHAR *[])
260 ACE_START_TEST (ACE_TEXT ("Refcounted_Event_Handler_Test_DevPoll"));
261 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_Dev_Poll_Reactor is UNSUPPORTED on this platform\n")));
262 ACE_END_TEST;
263 return 0;
265 #endif /* ACE_HAS_DEV_POLL || ACE_HAS_EVENT_POLL */