1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/**
* @file Bug_2540_Regression_Test.cpp
*
* $Id: Bug_2540_Regression_Test.cpp 91673 2010-09-08 18:49:47Z johnnyw $
*
* Reproduces the problems reported in bug 2540
* http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=2540
*
* @author Carlos O'Ryan <coryan@atdesk.com>
* Based on Bug_1890_Regression_Test
*/
#include "test_config.h"
#include "ace/Pipe.h"
#include "ace/Event_Handler.h"
#include "ace/Reactor.h"
#include "ace/Select_Reactor.h"
#include "ace/Auto_Ptr.h"
int const nhandlers = 3;
/**
* This class is used to create real I/O in the test. To keep the I/O under
* control and keep the test to a single process we use ACE_Pipe. This class
* is known to work with the Reactor, in fact, that is its main function.
*
* Handler counts how many calls to handle_input() has the reactor performed.
* When bug 2540 is triggered the Reactor continues to call the timers, but it
* stops calling select() and the handle_input() functions.
*/
class Handler : public ACE_Event_Handler
{
public:
Handler();
//FUZZ: disable check_for_lack_ACE_OS
/// Initialize the pipe and register with the reactor
int open(ACE_Reactor * reactor);
//FUZZ: enable check_for_lack_ACE_OS
/// Return the current count
size_t handle_input_count() const;
/// Write some data
void send_dummy_data();
/// Removes itself from the reactor on the next call to handle_input()
void simulate_socket_closure();
/// Reactor callback
virtual ACE_HANDLE get_handle() const;
virtual int handle_input(ACE_HANDLE);
private:
bool auto_remove_flag_;
size_t handle_input_count_;
ACE_Pipe the_pipe_;
ACE_HANDLE handles_[2];
};
/**
* This is the main driver for the test. This timer is called by the reactor
* in a repeating interval. On the first @c initial_iterations the Timer
* writes data through all of its handlers. On iteration @c initial_iteration
* it triggers bug 2540 by removing two handlers from the reactor.
*
*/
class Timer : public ACE_Event_Handler
{
public:
Timer();
//FUZZ: disable check_for_lack_ACE_OS
int open(ACE_Reactor * reactor);
void close();
//FUZZ: enable check_for_lack_ACE_OS
bool check_expected_results() const;
virtual int handle_timeout(ACE_Time_Value const &, void const*);
private:
void send_data_through_handlers();
void remove_some_handlers();
Handler & special_handler();
Handler const & special_handler() const;
private:
Handler handler_[nhandlers];
int iteration_;
size_t recorded_count_;
};
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Bug_2540_Regression_Test"));
// Bug 2540 is all about ACE_Select_Reactor, so run it on that reactor
// regardless of platform. In particular, this test relies on a handler
// that doesn't consume ready-to-read data being called back - this won't
// happen with ACE_WFMO_Reactor.
ACE_Select_Reactor *impl_ptr = 0;
ACE_NEW_RETURN (impl_ptr, ACE_Select_Reactor, -1);
auto_ptr<ACE_Select_Reactor> auto_impl (impl_ptr);
ACE_Reactor reactor (impl_ptr);
// Create the timer, this is the main driver for the test
Timer * timer = new Timer;
// Initialize the timer and register with the reactor
if (-1 == timer->open (&reactor))
{
ACE_ERROR_RETURN ((LM_ERROR, "Cannot initialize timer\n"), -1);
}
reactor.run_reactor_event_loop ();
// Verify that the results are what we expect
if (!timer->check_expected_results ())
{
ACE_ERROR_RETURN ((LM_ERROR, "Test failed\n"), -1);
}
// Cleanup
timer->close ();
delete timer;
ACE_END_TEST;
return 0;
}
Handler::Handler()
: auto_remove_flag_(false)
, handle_input_count_(0)
, the_pipe_()
{
}
int Handler::open(ACE_Reactor * r)
{
if(-1 == the_pipe_.open(handles_))
{
return -1;
}
if(-1 == r->register_handler(this, ACE_Event_Handler::READ_MASK))
{
return -1;
}
return 0;
}
size_t Handler::handle_input_count() const
{
return handle_input_count_;
}
void Handler::send_dummy_data()
{
char buf[] = "dummy";
(void) the_pipe_.send(buf, sizeof(buf));
}
void Handler::simulate_socket_closure()
{
auto_remove_flag_ = true;
}
ACE_HANDLE Handler::get_handle() const
{
return the_pipe_.read_handle();
}
int Handler::handle_input(ACE_HANDLE /* h */)
{
++handle_input_count_;
// ACE_DEBUG((LM_DEBUG, "Handler::handle_input called for %d\n", h));
if(auto_remove_flag_)
{
auto_remove_flag_ = false;
return -1;
}
return 0;
}
int const initial_iterations = 5;
int const total_iterations = 10;
int const special_handler_index = nhandlers - 1;
Timer::Timer()
: iteration_(0)
, recorded_count_(0)
{
}
int Timer::open(ACE_Reactor * r)
{
this->reactor(r);
// Initialize both handles and register them with the reactor for reading.
for(int i = 0; i != nhandlers; ++i)
{
if (-1 == handler_[i].open(r))
{
ACE_ERROR_RETURN ((LM_ERROR, "Could not open dummy handler %d\n", i), -1);
}
}
ACE_Time_Value const interval(0, ACE_ONE_SECOND_IN_USECS / 10);
ACE_Time_Value const startup (0, ACE_ONE_SECOND_IN_USECS / 20);
if ( -1 == r->schedule_timer(this, 0, startup, interval))
{
ACE_ERROR_RETURN((LM_ERROR, "Could not schedule timer\n"), -1);
}
return 0;
}
void Timer::close()
{
for(int i = 0; i != nhandlers; ++i)
{
reactor()->remove_handler(&handler_[i], ACE_Event_Handler::ALL_EVENTS_MASK);
}
reactor()->cancel_timer(this);
}
bool Timer::check_expected_results() const
{
// We expect at least one more call after the other handlers are removed.
if(recorded_count_ + 1 < special_handler().handle_input_count() )
{
return true;
}
return false;
}
int Timer::handle_timeout(ACE_Time_Value const &, void const *)
{
if (iteration_ == 0)
{
// Sending data on the first iteration makes the handles always
// "ready" for reading because the Handler::handle_input() function
// never consumes the data.
send_data_through_handlers();
}
++iteration_;
if (iteration_ < initial_iterations)
{
// The first iterations are there just to prime things.
return 0;
}
if (iteration_ == initial_iterations)
{
// We expect the special_handler() to work normally after this
// iteration, i.e., more calls to handle_input() should be delivered
// to it.
recorded_count_ = special_handler().handle_input_count();
// Remove the handlers the next time the loop runs
remove_some_handlers();
// Run the event loop, this causes the handlers to be removed from the
// reactor, except for special_handler()
ACE_Time_Value interval(0, ACE_ONE_SECOND_IN_USECS / 50);
reactor()->handle_events(&interval);
return 0;
}
if (iteration_ < total_iterations)
{
// Run a while more to make sure the special_handler() is used.
return 0;
}
reactor()->end_reactor_event_loop();
return 0;
}
void Timer::send_data_through_handlers()
{
for(int i = 0; i != nhandlers; ++i)
{
handler_[i].send_dummy_data();
}
}
void Timer::remove_some_handlers()
{
for(int i = 0; i != nhandlers - 1; ++i)
{
handler_[i].simulate_socket_closure();
}
}
Handler & Timer::special_handler()
{
return handler_[special_handler_index];
}
Handler const & Timer::special_handler() const
{
return handler_[special_handler_index];
}
|