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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
//=============================================================================
/**
* @file Reactor_Notify_Test.cpp
*
* $Id: Reactor_Notify_Test.cpp 93638 2011-03-24 13:16:05Z johnnyw $
*
* This is a test that illustrates how the <ACE_Reactor>'s
* <notify> method works under various <max_notify_iterations>
* settings. It also tests that the <disable_notify_pipe> option
* works correctly. Moreover, if the $ACE_ROOT/ace/config.h file
* has the ACE_HAS_REACTOR_NOTIFICATION_QUEUE option enabled this
* test will also exercise this feature.
*
*
* @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
*/
//=============================================================================
#include "test_config.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Synch_Traits.h"
#include "ace/Task.h"
#include "ace/Pipe.h"
#include "ace/Auto_Ptr.h"
#include "ace/Reactor.h"
#include "ace/Select_Reactor.h"
#include "ace/Thread_Semaphore.h"
#if defined (ACE_HAS_THREADS)
static const time_t LONG_TIMEOUT = 10;
static const time_t SHORT_TIMEOUT = 2;
// A class to run a quiet event loop in one thread, and a plain notify()
// from the main thread to make sure a simple notify will wake up a quiet
// reactor.
class Quiet_Notify_Tester : public ACE_Task<ACE_NULL_SYNCH>
{
public:
Quiet_Notify_Tester (void) : result_ (0) {}
~Quiet_Notify_Tester (void) { this->wait (); }
//FUZZ: disable check_for_lack_ACE_OS
/// Start the reactor event thread.
virtual int open (void * = 0);
// Run the reactor event loop.
virtual int svc (void);
// Return the test result, 0 ok, -1 fail
int result (void) const { return this->result_; }
private:
ACE_Reactor r_;
int result_;
};
int
Quiet_Notify_Tester::open (void *)
{
this->reactor (&this->r_);
return this->activate ();
}
int
Quiet_Notify_Tester::svc (void)
{
// Count on the main thread doing a notify in less than LONG_TIMEOUT
// seconds. If we don't get it, report a failure.
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Starting quiet event loop\n")));
this->r_.owner (ACE_Thread::self ());
ACE_Time_Value tmo (LONG_TIMEOUT);
int status = this->r_.handle_events (&tmo);
time_t remain = tmo.sec ();
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) event loop status %d, %: secs remain\n"),
status, remain));
if (remain == 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) Notify did not wake quiet event loop\n")));
this->result_ = -1;
}
else
{
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Notify woke quiet event loop\n")));
}
return 0;
}
static int
run_quiet_notify_test (void)
{
ACE_DEBUG ((LM_DEBUG, "(%t) Starting quiet notify test\n"));
Quiet_Notify_Tester t;
if (t.open () == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("Quiet notify activate")),
-1);
// Now sleep a bit, then do a simple, reactor wake-up
ACE_OS::sleep (SHORT_TIMEOUT);
t.reactor ()->notify ();
t.wait ();
ACE_DEBUG ((LM_DEBUG, "(%t) Quiet notify test done\n"));
return t.result ();
}
class Supplier_Task : public ACE_Task<ACE_MT_SYNCH>
{
public:
/// Constructor.
Supplier_Task (int disable_notify_pipe,
const ACE_Time_Value &tv);
/// Destructor.
~Supplier_Task (void);
//FUZZ: disable check_for_lack_ACE_OS
/// Make this an Active Object.
virtual int open (void * = 0);
/// Close down the supplier.
///FUZZ: enable check_for_lack_ACE_OS
virtual int close (u_long);
/// Generates events and sends them to the <Reactor>'s <notify>
/// method.
virtual int svc (void);
/// Releases the <waiter_> semaphore when called by the <Reactor>'s
/// notify handler.
virtual int handle_exception (ACE_HANDLE);
/**
* Called every time through the main <ACE_Reactor> event loop to
* illustrate the difference between "limited" and "unlimited"
* notification.
*/
virtual int handle_output (ACE_HANDLE);
/// Release the <waiter_>.
void release (void);
private:
/// Perform the notifications.
int perform_notifications (int notifications);
/// Used to hand-shake between the <Supplier_Task> and the
/// <Reactor>'s notify mechanism.
ACE_Thread_Semaphore waiter_;
/**
* We use this pipe just to get a handle that is always "active,"
* i.e., the <ACE_Reactor> will always dispatch its <handle_output>
* method.
*/
ACE_Pipe pipe_;
/// Keeps track of whether the notification pipe in the <ACE_Reactor>
/// has been diabled or not.
int disable_notify_pipe_;
/**
* Keeps track of whether we're running with a <LONG_TIMEOUT>, which
* is used for the ACE_HAS_REACTOR_NOTIFICATION_QUEUE portion of
* this test.
*/
int long_timeout_;
};
void
Supplier_Task::release (void)
{
this->waiter_.release ();
}
Supplier_Task::Supplier_Task (int disable_notify_pipe,
const ACE_Time_Value &tv)
: waiter_ ((unsigned int) 0), // Make semaphore "locked" by default.
disable_notify_pipe_ (disable_notify_pipe),
long_timeout_ (tv.sec () == LONG_TIMEOUT)
{
}
int
Supplier_Task::open (void *)
{
// Create the pipe.
int result;
result = this->pipe_.open ();
ACE_TEST_ASSERT (result != -1);
// Register the pipe's write handle with the <Reactor> for writing.
// This should mean that it's always "active."
if (long_timeout_ == 0)
{
result = ACE_Reactor::instance ()->register_handler
(this->pipe_.write_handle (),
this,
ACE_Event_Handler::WRITE_MASK);
ACE_TEST_ASSERT (result != -1);
}
// Make this an Active Object.
result = this->activate (THR_BOUND | THR_DETACHED);
ACE_TEST_ASSERT (result != -1);
return 0;
}
int
Supplier_Task::close (u_long)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Supplier_Task::close\n")));
int result;
if (long_timeout_ == 0)
{
result = ACE_Reactor::instance ()->remove_handler
(this->pipe_.write_handle (),
ACE_Event_Handler::WRITE_MASK);
ACE_TEST_ASSERT (result != -1);
}
else
{
// Wait to be told to shutdown by the main thread.
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) waiting to be shutdown by main thread\n")));
result = this->waiter_.acquire ();
ACE_TEST_ASSERT (result != -1);
}
return 0;
}
Supplier_Task::~Supplier_Task (void)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) ~Supplier_Task\n")));
this->pipe_.close ();
}
int
Supplier_Task::perform_notifications (int notifications)
{
ACE_Reactor::instance ()->max_notify_iterations (notifications);
size_t iterations = ACE_MAX_ITERATIONS;
if (this->long_timeout_)
{
iterations *= (iterations * iterations * 2);
#if defined (ACE_VXWORKS)
// scale down otherwise the test won't finish in time
iterations /= 4;
#endif
}
for (size_t i = 0; i < iterations; i++)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) notifying reactor on iteration %d\n"),
i));
int result;
// Notify the Reactor, which will call <handle_exception>.
result = ACE_Reactor::instance ()->notify (this);
if (result == -1)
{
if (errno == ETIME)
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("notify")));
else
ACE_TEST_ASSERT (result != -1);
}
// Wait for our <handle_exception> method to release the
// semaphore.
if (this->long_timeout_ == 0
&& this->disable_notify_pipe_ == 0)
{
result = this->waiter_.acquire ();
ACE_TEST_ASSERT (result != -1);
}
}
return 0;
}
int
Supplier_Task::svc (void)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) **** starting unlimited notifications test\n")));
// Allow an unlimited number of iterations per
// <ACE_Reactor::notify>.
this->perform_notifications (-1);
if (this->long_timeout_ == 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) **** starting limited notifications test\n")));
// Only allow 1 iteration per <ACE_Reactor::notify>
this->perform_notifications (1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) **** exiting thread test\n")));
}
return 0;
}
int
Supplier_Task::handle_exception (ACE_HANDLE handle)
{
ACE_TEST_ASSERT (handle == ACE_INVALID_HANDLE);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) handle_exception\n")));
this->waiter_.release ();
return 0;
}
int
Supplier_Task::handle_output (ACE_HANDLE handle)
{
ACE_TEST_ASSERT (handle == this->pipe_.write_handle ());
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) handle_output\n")));
// This function is called by the main thread, believe it or not :-)
// That's because the pipe's write handle is always active. Thus,
// we can give the <Supplier_Task> a chance to run in its own
// thread.
ACE_OS::thr_yield ();
return 0;
}
static int
run_test (int disable_notify_pipe,
const ACE_Time_Value &tv)
{
// Create special reactors with the appropriate flags enabled.
ACE_Select_Reactor *reactor_impl = 0;
if (disable_notify_pipe)
ACE_NEW_RETURN (reactor_impl,
ACE_Select_Reactor (0, 0, 1),
-1);
else
ACE_NEW_RETURN (reactor_impl,
ACE_Select_Reactor,
-1);
ACE_Reactor *reactor;
ACE_NEW_RETURN (reactor,
ACE_Reactor (reactor_impl, 1), // Delete implementation
-1);
// Make sure this stuff gets cleaned up when this function exits.
auto_ptr<ACE_Reactor> r (reactor);
// Set the Singleton Reactor.
ACE_Reactor *orig_reactor = ACE_Reactor::instance (reactor);
ACE_TEST_ASSERT (ACE_LOG_MSG->op_status () != -1);
ACE_TEST_ASSERT (ACE_Reactor::instance () == reactor);
Supplier_Task task (disable_notify_pipe,
tv);
ACE_TEST_ASSERT (ACE_LOG_MSG->op_status () != -1);
int result;
result = task.open ();
ACE_TEST_ASSERT (result != -1);
if (tv.sec () == LONG_TIMEOUT)
// Sleep for a while so that the <ACE_Reactor>'s notification
// buffers will fill up!
ACE_OS::sleep (tv);
int shutdown = 0;
// Run the event loop that handles the <handle_output> and
// <handle_exception> notifications.
for (int iteration = 1;
shutdown == 0;
iteration++)
{
ACE_Time_Value timeout (tv);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) starting handle_events() on iteration %d")
ACE_TEXT (" with time-out = %d seconds\n"),
iteration,
timeout.sec ()));
// Use a timeout to inform the Reactor when to shutdown.
switch (ACE_Reactor::instance ()->handle_events (timeout))
{
case -1:
if (! disable_notify_pipe)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("reactor")));
shutdown = 1;
break;
/* NOTREACHED */
case 0:
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) handle_events timed out\n")));
shutdown = 1;
break;
/* NOTREACHED */
default:
break;
/* NOTREACHED */
}
}
if (tv.sec () == LONG_TIMEOUT)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) releasing supplier task thread\n")));
task.release ();
}
ACE_Reactor::instance (orig_reactor);
return 0;
}
#endif /* ACE_HAS_THREADS */
/**
* @class Purged_Notify
*
* @brief <run_notify_purge_test> tests the reactor's
* purge_pending_notifications function. It does 2 notifications,
* and explicitly cancels one, and deletes the other's event
* handler, which should cause it to be cancelled as well.
*/
class Purged_Notify : public ACE_Event_Handler
{
virtual int handle_exception (ACE_HANDLE = ACE_INVALID_HANDLE)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Got a notify that should have been purged!\n")),
0);
}
};
static int
run_notify_purge_test (void)
{
int status;
ACE_Reactor *r = ACE_Reactor::instance ();
{
Purged_Notify n1;
Purged_Notify *n2;
ACE_NEW_RETURN (n2, Purged_Notify, -1);
auto_ptr<Purged_Notify> ap (n2);
// First test:
// Notify EXCEPT, and purge ALL
r->notify (&n1); // the mask is EXCEPT_MASK
status = r->purge_pending_notifications (&n1);
if (status == -1 && errno == ENOTSUP)
return 0; // Select Reactor w/o ACE_HAS_REACTOR_NOTIFICATION_QUEUE
if (status != 1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Purged %d notifies; expected 1\n"),
status));
// Second test:
// Notify READ twice, and WRITE once, and purge READ and WRITE - should purge 3 times.
r->notify (&n1, ACE_Event_Handler::READ_MASK);
r->notify (&n1, ACE_Event_Handler::READ_MASK);
r->notify (&n1, ACE_Event_Handler::WRITE_MASK);
status = r->purge_pending_notifications
(&n1, ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK);
if (status != 3)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Purged %d notifies; expected 3\n"),
status));
// Third test:
// Notify READ on 2 handlers, and purge READ|WRITE on all handlers. Should purge 2
r->notify (&n1, ACE_Event_Handler::READ_MASK);
r->notify (n2, ACE_Event_Handler::READ_MASK);
status = r->purge_pending_notifications
(0, ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK);
if (status != 2)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Purged %d notifies; expected 2\n"),
status));
// Forth test:
// Notify EXCEPT and WRITE, purge READ. Should not purge
r->notify (&n1); // the mask is EXCEPT_MASK
r->notify (&n1, ACE_Event_Handler::WRITE_MASK);
status = r->purge_pending_notifications
(&n1, ACE_Event_Handler::READ_MASK);
if (status != 0)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Purged %d notifies; expected 0\n"),
status));
// Fifth test:
r->notify (n2);
// The destructor of the event handler no longer removes the
// notifications. It is the application's responsability to do
// so.
r->purge_pending_notifications(n2,
ACE_Event_Handler::ALL_EVENTS_MASK);
r->purge_pending_notifications(&n1,
ACE_Event_Handler::ALL_EVENTS_MASK);
}
ACE_Time_Value t (1);
status = r->handle_events (t); // Should be nothing to do, and time out
return status < 0 ? 1 : 0; // Return 0 for all ok, else error
}
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Reactor_Notify_Test"));
int test_result = 0; // Innocent until proven guilty
if (0 == run_notify_purge_test ())
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("purge_pending_notifications test OK\n")));
}
else
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("purge_pending_notifications test FAIL\n")));
test_result = 1;
}
#if defined (ACE_HAS_THREADS)
if (0 != run_quiet_notify_test ())
test_result = 1;
ACE_Time_Value timeout (SHORT_TIMEOUT);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) running tests with notify pipe enabled")
ACE_TEXT (" and time-out = %d seconds\n"),
timeout.sec ()));
run_test (0, timeout);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) running tests with notify pipe disabled")
ACE_TEXT (" and time-out = %d seconds\n"),
timeout.sec ()));
run_test (1, timeout);
timeout.set (LONG_TIMEOUT, 0);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) running tests with reactor notification ")
ACE_TEXT ("pipe enabled\n")
ACE_TEXT (" and time-out = %d seconds\n"),
timeout.sec ()));
run_test (0, timeout);
#else
ACE_ERROR ((LM_INFO,
ACE_TEXT ("threads not supported on this platform\n")));
#endif /* ACE_HAS_THREADS */
ACE_END_TEST;
return test_result;
}
|