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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
/**
* @file Reactor_Remove_Resume_Test.cpp
*
* $Id: Reactor_Remove_Resume_Test.cpp 84565 2009-02-23 08:20:39Z johnnyw $
*
* This test verifies that ACE reactors only remove or resume the event
* handler used during an upcall, not another with same handle value.
* There is are least one case where the event handler can change
* during an upcall. The event handler could be removed by another
* thread, and a new one is registered for a handle of the same value
* (e.g. closed and then reopened) during an upcall. "Misbehaved"
* event handlers could also cause this problem by closing and
* deregistering the event handler during an upcall.
*
* @author Ossama Othman
*/
#include "test_config.h"
#include "ace/Reactor.h"
#include "ace/TP_Reactor.h"
#include "ace/Pipe.h"
#include "ace/Auto_Ptr.h"
#include <algorithm>
#include <functional>
int overall_result = 0;
// ------------------------------------------------------------
class Bogus_Handler : public ACE_Event_Handler
{
public:
Bogus_Handler (ACE_Reactor * reactor,
ACE_HANDLE read_handle,
bool & okay_to_close);
protected:
virtual ~Bogus_Handler (void);
virtual ACE_HANDLE get_handle (void) const;
virtual int handle_input (ACE_HANDLE handle);
virtual int handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask close_mask);
virtual int resume_handler (void);
private:
ACE_HANDLE const read_handle_;
// If the reactor closes the event handler before it gets the okay,
// we will issue an error.
bool & okay_to_close_;
};
Bogus_Handler::Bogus_Handler (ACE_Reactor * reactor,
ACE_HANDLE read_handle,
bool & okay_to_close)
: ACE_Event_Handler (reactor)
, read_handle_ (read_handle)
, okay_to_close_ (okay_to_close)
{
this->reference_counting_policy ().value (
ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
}
Bogus_Handler::~Bogus_Handler (void)
{
}
ACE_HANDLE
Bogus_Handler::get_handle (void) const
{
return this->read_handle_;
}
int
Bogus_Handler::handle_input (ACE_HANDLE)
{
// This event handler should have been suspended, meaning it should
// not have received any events.
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Bogus_Handler received an ")
ACE_TEXT ("unexpected event.\n")),
-1);
}
int
Bogus_Handler::handle_close (ACE_HANDLE,
ACE_Reactor_Mask)
{
if (this->okay_to_close_)
return 0;
overall_result = -1;
// This event handler is being closed by the reactor unexpectedly.
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Bogus_Handler unexpectedly closed\n")),
-1);
}
int
Bogus_Handler::resume_handler (void)
{
// We don't want the reactor to resume this event handler.
return ACE_APPLICATION_RESUMES_HANDLER;
}
// ------------------------------------------------------------
class Bad_Handler : public ACE_Event_Handler
{
public:
Bad_Handler (ACE_Reactor * reactor,
ACE_HANDLE read_handle,
bool & okay_to_close,
bool suspension_test);
ACE_HANDLE write_handle (void) const;
protected:
virtual ~Bad_Handler (void);
private:
virtual ACE_HANDLE get_handle (void) const;
virtual int handle_input (ACE_HANDLE handle);
virtual int handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask close_mask);
int handle_input_result (void) const;
private:
ACE_HANDLE const read_handle_;
bool handle_close_called_;
// Passed on to the Bogus_Handler constructor. Not used by the
// Bad_Handler, otherwise.
bool & okay_to_close_;
// Are we running the event handler suspension or removal test?
bool suspension_test_;
};
Bad_Handler::Bad_Handler (ACE_Reactor * reactor,
ACE_HANDLE read_handle,
bool & okay_to_close,
bool suspension_test)
: ACE_Event_Handler (reactor)
, read_handle_ (read_handle)
, handle_close_called_ (false)
, okay_to_close_ (okay_to_close)
, suspension_test_ (suspension_test)
{
this->reference_counting_policy ().value (
ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
}
Bad_Handler::~Bad_Handler (void)
{
}
ACE_HANDLE
Bad_Handler::get_handle (void) const
{
return this->read_handle_;
}
int
Bad_Handler::handle_input (ACE_HANDLE handle)
{
// Remove ourselves from the reactor, and trigger a different one
// with the same handle to be registered.
if (this->reactor ()->remove_handler (handle,
ACE_Event_Handler::READ_MASK) != 0)
{
overall_result = -1;
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to remove Bad_Handler ")
ACE_TEXT ("from reactor\n")),
1);
}
ACE_Event_Handler_var bogus_handler (
new Bogus_Handler (this->reactor (),
handle,
this->okay_to_close_));
// Register and suspend a new handler.
if (this->reactor ()->register_handler (handle,
bogus_handler.handler (),
ACE_Event_Handler::READ_MASK) != 0
|| this->reactor ()->suspend_handler (handle) != 0)
{
overall_result = -1;
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to register or suspend ")
ACE_TEXT ("Bogus_Handler\n")),
1);
}
// Make sure a read event exists for dispatching during the next
// event loop iteration.
if (this->reactor ()->schedule_wakeup (handle,
ACE_Event_Handler::READ_MASK) == -1)
{
overall_result = -1;
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Wakeup scheduling failed\n")));
}
return this->handle_input_result ();
}
int
Bad_Handler::handle_close (ACE_HANDLE,
ACE_Reactor_Mask)
{
if (this->handle_close_called_)
{
overall_result = -1;
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Bad_Handler::handle_close() called ")
ACE_TEXT ("more than once\n")),
-1);
}
this->handle_close_called_ = true;
return 0;
}
int
Bad_Handler::handle_input_result (void) const
{
return
(this->suspension_test_
// Attempt to force reactor to resume the handle after the
// upcall completes.
? 0
// Attempt to force the new event handler to be removed from the
// reactor.
: -1);
}
// ------------------------------------------------------------
int
register_handler (ACE_Reactor * reactor,
ACE_HANDLE read_handle,
bool & okay_to_close,
bool suspension_test)
{
ACE_Event_Handler_var bad_handler (new Bad_Handler (reactor,
read_handle,
okay_to_close,
suspension_test));
// Register for read events.
if (reactor->register_handler (bad_handler.handler (),
ACE_Event_Handler::READ_MASK) != 0)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to register Bad_Handler with ")
ACE_TEXT ("reactor\n")),
-1);
}
return 0;
}
// ------------------------------------------------------------
int
send_data (ACE_HANDLE write_handle)
{
char const foo[] = "foo";
size_t const len = sizeof (foo); // We want the number of bytes, not
// the number of characters.
ACE_Time_Value const timeout (2);
// Trigger a read event on the pipe handle. This shouldn't timeout
// since the pipe should be able to hold such a small amount of
// data.
size_t bytes_transferred = 0;
ssize_t const result =
ACE::send_n (write_handle,
foo,
len,
&timeout, // timeout
&bytes_transferred);
if (result == -1
|| bytes_transferred != len)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("Unable to send data")),
-1);
}
return 0;
}
// ------------------------------------------------------------
int
handle_events (ACE_Reactor & reactor,
bool & okay_to_close)
{
ACE_Time_Value timeout (2);
// Only one event handler should have been dispatched.
if (reactor.handle_events (&timeout) != 1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Initial event dispatch failed\n")));
}
else
{
okay_to_close = true;
// Run the event loop again in an attempt to make the reactor
// dispatch the newly registered event handler. No events
// should be dispatched.
timeout.sec (2);
int const result = reactor.handle_events (&timeout);
if (result > 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Unexpectedly dispatched an event\n")));
}
else if (result < 0)
{
overall_result = -1;
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Event loop failed unexpectedly\n")));
}
else
return 0;
}
return -1;
}
// ------------------------------------------------------------
typedef auto_ptr<ACE_Reactor_Impl> (*reactor_factory_type) (void);
auto_ptr<ACE_Reactor_Impl>
tp_reactor_factory (void)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("Creating ACE_TP_Reactor.\n")));
return auto_ptr<ACE_Reactor_Impl> (new ACE_TP_Reactor);
}
// ------------------------------------------------------------
/**
* @struct Caller
*
* @brief Reactor test execution functor.
*
* Reactor test execution functor.
*/
struct Run_Test : public std::unary_function<reactor_factory_type, void>
{
/// Function call operator overload.
void operator() (reactor_factory_type factory)
{
bool const suspension_test[] =
{
true, // Run suspension test.
false // Run removal test.
};
bool const * const begin = suspension_test;
bool const * const end =
suspension_test
+ sizeof (suspension_test) / sizeof (suspension_test[0]);
for (bool const * i = begin; i != end; ++i)
{
bool const suspension_test = *i;
if (suspension_test)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("** Running suspension test **\n")));
}
else
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("** Running removal test **\n")));
}
auto_ptr<ACE_Reactor_Impl> the_factory (factory ());
ACE_Reactor reactor (the_factory.get ());
// In this test, it's only okay to close the Bogus_Handler
// when the reactor is destroyed.
bool okay_to_close = false;
ACE_Pipe the_pipe;
if (the_pipe.open () != 0
|| register_handler (&reactor,
the_pipe.read_handle (),
okay_to_close,
suspension_test) != 0
|| send_data (the_pipe.write_handle ()) != 0
|| handle_events (reactor, okay_to_close) != 0)
{
overall_result = -1;
}
okay_to_close = true;
ACE_DEBUG ((LM_INFO, ACE_TEXT ("\n"))); // For log aesthetics.
}
}
};
// ------------------------------------------------------------
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Reactor_Remove_Resume_Test"));
static reactor_factory_type const factories[] =
{
tp_reactor_factory
};
static size_t const factory_count = sizeof (factories) / sizeof (factories[0]);
std::for_each (factories, factories + factory_count, Run_Test ());
if (overall_result != 0)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Test failed.\n")));
else
ACE_ERROR ((LM_INFO,
ACE_TEXT ("Test passed.\n")));
ACE_END_TEST;
return overall_result;
}
|