Make x.0.10 publicly available
[ACE_TAO.git] / ACE / tests / Reactor_Dispatch_Order_Test_Dev_Poll.cpp
blob0141e2394bafc9767bd423605667e90b3ea2595e
2 //=============================================================================
3 /**
4 * @file Reactor_Dispatch_Order_Test_Dev_Poll.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"
19 #include "ace/Pipe.h"
20 #include "ace/ACE.h"
22 #if defined (ACE_HAS_DEV_POLL) || defined (ACE_HAS_EVENT_POLL)
24 static const char *message = "Hello there! Hope you get this message";
26 class Handler : public ACE_Event_Handler
28 public:
29 Handler (ACE_Reactor &reactor);
31 ~Handler() override;
33 int handle_timeout (const ACE_Time_Value &tv,
34 const void *arg) override;
36 int handle_input (ACE_HANDLE fd) override;
38 int handle_output (ACE_HANDLE fd) override;
40 ACE_HANDLE get_handle () const override;
42 // We need to add MSG_OOB data transfer to this test to check the
43 // order of when <handle_exception> gets called. I tried with
44 // Windows 2000 but only one byte of the message came across as
45 // urgent data. The rest of the data was treated as normal! There
46 // was some explanation of Microsoft's TCP/IP deals with out-of-band
47 // data in "Out-of-Band Data and Push Bit in TCP/IP" in the MSDN
48 // library. However, I did not comprehend that well enough. If
49 // someone can make this work, please check the order of
50 // <handle_exception> getting called.
51 // int handle_exception (ACE_HANDLE fd);
53 ACE_Pipe pipe_;
55 int dispatch_order_;
56 bool ok_; // Constructed and initialized ok
59 Handler::Handler (ACE_Reactor &reactor)
60 : ACE_Event_Handler (&reactor),
61 dispatch_order_ (1),
62 ok_ (false)
64 // Create the pipe.
65 if (0 != this->pipe_.open ())
66 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("pipe")));
67 else
69 // Register for all events.
70 if (0 != this->reactor ()->register_handler
71 (this->pipe_.read_handle (),
72 this,
73 ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK))
74 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("register")));
75 else
76 this->ok_ = true;
81 Handler::~Handler ()
83 this->pipe_.close ();
87 ACE_HANDLE
88 Handler::get_handle () const
90 return this->pipe_.read_handle ();
93 int
94 Handler::handle_timeout (const ACE_Time_Value &,
95 const void *)
97 int me = this->dispatch_order_++;
98 if (me != 1)
99 ACE_ERROR ((LM_ERROR,
100 ACE_TEXT ("handle_timeout should be #1; it's %d\n"),
101 me));
102 else
103 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Handler::handle_timeout\n")));
105 return 0;
109 Handler::handle_output (ACE_HANDLE)
111 int me = this->dispatch_order_++;
112 if (me != 2)
113 ACE_ERROR ((LM_ERROR,
114 ACE_TEXT ("handle_output should be #2; it's %d\n"),
115 me));
116 else
117 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Handler::handle_output\n")));
119 // Don't want to continually see writeable; only verify its relative order.
120 this->reactor ()->mask_ops (this->pipe_.read_handle (),
121 ACE_Event_Handler::WRITE_MASK,
122 ACE_Reactor::CLR_MASK);
124 return 0;
128 Handler::handle_input (ACE_HANDLE fd)
130 int me = this->dispatch_order_++;
131 if (me != 3)
132 ACE_ERROR ((LM_ERROR,
133 ACE_TEXT ("handle_input should be #3; it's %d\n"),
134 me));
136 char buffer[BUFSIZ];
137 ssize_t result = ACE::recv (fd, buffer, sizeof buffer);
138 if (result != ssize_t (ACE_OS::strlen (message)))
139 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Handler recv'd %b bytes; expected %B\n"),
140 result, ACE_OS::strlen (message)));
141 buffer[result] = '\0';
143 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Handler::handle_input: %C\n"), buffer));
145 if (ACE_OS::strcmp (buffer, message) != 0)
146 ACE_ERROR ((LM_ERROR,
147 ACE_TEXT ("Handler text mismatch; received \"%C\"; ")
148 ACE_TEXT ("expected \"%C\"\n"),
149 buffer, message));
151 this->reactor ()->end_reactor_event_loop ();
153 return 0;
156 static bool
157 test_reactor_dispatch_order (ACE_Reactor &reactor)
159 Handler handler (reactor);
160 if (!handler.ok_)
162 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error initializing test; abort.\n")));
163 return false;
166 bool ok_to_go = true;
168 // This should trigger a call to <handle_input>.
169 ssize_t result =
170 ACE::send_n (handler.pipe_.write_handle (),
171 message,
172 ACE_OS::strlen (message));
173 if (result != ssize_t (ACE_OS::strlen (message)))
175 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Handler sent %b bytes; should be %B\n"),
176 result, ACE_OS::strlen (message)));
177 ok_to_go = false;
180 // This should trigger a call to <handle_timeout>.
181 if (-1 == reactor.schedule_timer (&handler,
183 ACE_Time_Value (0)))
185 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
186 ok_to_go = false;
189 // Suspend the handlers - only the timer should be dispatched
190 ACE_Time_Value tv (1);
191 reactor.suspend_handlers ();
192 reactor.run_reactor_event_loop (tv);
194 // only the timer should have fired
195 if (handler.dispatch_order_ != 2)
197 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Incorrect number fired %d\n"),
198 handler.dispatch_order_));
199 ok_to_go = false;
202 // Reset the dispatch_order_ count and schedule another timer
203 handler.dispatch_order_ = 1;
204 if (-1 == reactor.schedule_timer (&handler,
206 ACE_Time_Value (0)))
208 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
209 ok_to_go = false;
212 // Resume the handlers - things should work now
213 reactor.resume_handlers ();
215 if (ok_to_go)
217 reactor.run_reactor_event_loop (tv);
220 if (0 != reactor.remove_handler (handler.pipe_.read_handle (),
221 ACE_Event_Handler::ALL_EVENTS_MASK |
222 ACE_Event_Handler::DONT_CALL))
223 ACE_ERROR ((LM_ERROR,
224 ACE_TEXT ("%p\n"),
225 ACE_TEXT ("remover_handler pipe")));
227 if (handler.dispatch_order_ != 4)
229 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Incorrect number fired %d\n"),
230 handler.dispatch_order_));
231 ok_to_go = false;
234 return ok_to_go;
238 run_main (int, ACE_TCHAR *[])
240 ACE_START_TEST (ACE_TEXT ("Reactor_Dispatch_Order_Test_Dev_Poll"));
241 int result = 0;
243 ACE_Dev_Poll_Reactor dev_poll_reactor_impl;
244 ACE_Reactor dev_poll_reactor (&dev_poll_reactor_impl);
245 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Testing Dev Poll Reactor\n")));
246 if (!test_reactor_dispatch_order (dev_poll_reactor))
247 ++result;
249 ACE_END_TEST;
250 return result;
252 #else
254 run_main (int, ACE_TCHAR *[])
256 ACE_START_TEST (ACE_TEXT ("Reactor_Dispatch_Order_Test_Dev_Poll"));
257 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_Dev_Poll_Reactor is UNSUPPORTED on this platform\n")));
258 ACE_END_TEST;
259 return 0;
261 #endif /* ACE_HAS_DEV_POLL || ACE_HAS_EVENT_POLL */