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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
//=============================================================================
/**
* @file Timer_Queue_Reference_Counting_Test.cpp
*
* $Id: Timer_Queue_Reference_Counting_Test.cpp 93638 2011-03-24 13:16:05Z johnnyw $
*
* This test is used to check reference counting of the Event
* Handler when it interacts with Timer Queues.
*
*
* @author Irfan Pyarali <irfan@oomworks.com>
*/
//=============================================================================
#include "test_config.h"
#include "ace/Get_Opt.h"
#include "ace/Timer_Queue.h"
#include "ace/Timer_Heap.h"
#include "ace/Timer_List.h"
#include "ace/Timer_Hash.h"
#include "ace/Timer_Wheel.h"
#include "ace/Reactor.h"
#include "ace/Recursive_Thread_Mutex.h"
#include "ace/Null_Mutex.h"
#include "ace/OS_NS_unistd.h"
static int debug = 0;
static const char *one_second_timeout = "one second timeout";
static const char *two_second_timeout = "two second timeout";
namespace
{
inline void WAIT_FOR_NEXT_EVENT (ACE_Timer_Queue &timer_queue)
{
ACE_Time_Value const earliest_time = timer_queue.earliest_time ();
ACE_Time_Value const time_of_day = timer_queue.gettimeofday ();
if (earliest_time > time_of_day)
{
ACE_OS::sleep (earliest_time - time_of_day);
}
}
}
class Reference_Counted_Event_Handler : public ACE_Event_Handler
{
public:
Reference_Counted_Event_Handler (int expected_number_of_handle_close_calls);
~Reference_Counted_Event_Handler (void);
int handle_timeout (const ACE_Time_Value &,
const void *);
int handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask masks);
int expected_number_of_handle_close_calls_;
int number_of_handle_close_calls_;
};
Reference_Counted_Event_Handler::Reference_Counted_Event_Handler (int expected_number_of_handle_close_calls)
: expected_number_of_handle_close_calls_ (expected_number_of_handle_close_calls),
number_of_handle_close_calls_ (0)
{
this->reference_counting_policy ().value
(ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
if (debug)
ACE_DEBUG ((LM_DEBUG,
"Reference count in Reference_Counted_Event_Handler() is %d\n",
this->reference_count_.value ()));
}
Reference_Counted_Event_Handler::~Reference_Counted_Event_Handler (void)
{
if (debug)
ACE_DEBUG ((LM_DEBUG,
"Reference count in ~Reference_Counted_Event_Handler() is %d\n",
this->reference_count_.value ()));
if (this->expected_number_of_handle_close_calls_ != -1)
ACE_TEST_ASSERT (this->number_of_handle_close_calls_ ==
this->expected_number_of_handle_close_calls_);
}
int
Reference_Counted_Event_Handler::handle_timeout (const ACE_Time_Value &,
const void *arg)
{
if (debug)
ACE_DEBUG ((LM_DEBUG,
"Reference count in Reference_Counted_Event_Handler::handle_timeout() for arg = %C is %d\n",
(const char *) arg,
this->reference_count_.value ()));
return 0;
}
int
Reference_Counted_Event_Handler::handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask masks)
{
if (debug)
ACE_DEBUG ((LM_DEBUG,
"Reference_Counted_Event_Handler::handle_close() called with handle = %d and masks = %d. "
"Reference count is %d\n",
handle,
masks,
this->reference_count_.value ()));
++this->number_of_handle_close_calls_;
return 0;
}
void
cancellation (ACE_Timer_Queue &timer_queue,
int repeat_timer,
int cancel_handler,
int second_timer,
int dont_call_handle_close)
{
int result = 0;
int expected_number_of_handle_close_calls = -1;
if (!dont_call_handle_close)
{
if (cancel_handler)
expected_number_of_handle_close_calls = 1;
else if (second_timer)
expected_number_of_handle_close_calls = 2;
else
expected_number_of_handle_close_calls = 1;
}
Reference_Counted_Event_Handler *handler =
new Reference_Counted_Event_Handler (expected_number_of_handle_close_calls);
ACE_Event_Handler_var safe_handler (handler);
long first_timer_id = -1;
long second_timer_id = -1;
if (repeat_timer)
{
first_timer_id =
timer_queue.schedule (handler,
one_second_timeout,
ACE_Time_Value (1) + timer_queue.gettimeofday (),
ACE_Time_Value (1));
ACE_TEST_ASSERT (first_timer_id != -1);
}
else
{
first_timer_id =
timer_queue.schedule (handler,
one_second_timeout,
ACE_Time_Value (1) + timer_queue.gettimeofday ());
ACE_TEST_ASSERT (first_timer_id != -1);
}
if (second_timer)
{
second_timer_id =
timer_queue.schedule (handler,
two_second_timeout,
ACE_Time_Value (2) + timer_queue.gettimeofday (),
ACE_Time_Value (2));
ACE_TEST_ASSERT (second_timer_id != -1);
}
if (cancel_handler)
{
result =
timer_queue.cancel (handler,
dont_call_handle_close);
if (second_timer)
ACE_TEST_ASSERT (result == 2);
else
ACE_TEST_ASSERT (result == 1);
}
else
{
result =
timer_queue.cancel (first_timer_id,
0,
dont_call_handle_close);
ACE_TEST_ASSERT (result == 1);
if (second_timer)
{
result =
timer_queue.cancel (second_timer_id,
0,
dont_call_handle_close);
ACE_TEST_ASSERT (result == 1);
}
}
}
template <class TIMER_QUEUE>
class cancellation_test
{
public:
cancellation_test (const char *);
};
template <class TIMER_QUEUE>
cancellation_test<TIMER_QUEUE>::cancellation_test (const char *timer_queue_type)
{
ACE_DEBUG ((LM_DEBUG,
"\nCancellation test for %C\n\n",
timer_queue_type));
int configs[][4] = {
{ 0, 0, 0, 0, },
{ 0, 0, 0, 1, },
{ 0, 0, 1, 0, },
{ 0, 0, 1, 1, },
{ 0, 1, 0, 0, },
{ 0, 1, 0, 1, },
{ 0, 1, 1, 0, },
{ 0, 1, 1, 1, },
{ 1, 0, 0, 0, },
{ 1, 0, 0, 1, },
{ 1, 0, 1, 0, },
{ 1, 0, 1, 1, },
{ 1, 1, 0, 0, },
{ 1, 1, 0, 1, },
{ 1, 1, 1, 0, },
{ 1, 1, 1, 1, },
};
for (int i = 0;
i < (int) (sizeof configs / (sizeof (int) * 5));
i++)
{
TIMER_QUEUE timer_queue;
cancellation (timer_queue,
configs[i][0],
configs[i][1],
configs[i][2],
configs[i][3]);
}
}
typedef int (*Expire_Function) (ACE_Timer_Queue &timer_queue);
int
invoke_expire (ACE_Timer_Queue &timer_queue)
{
return timer_queue.expire ();
}
int
invoke_one_upcall (ACE_Timer_Queue &timer_queue)
{
// Get the current time
ACE_Time_Value current_time (timer_queue.gettimeofday () +
timer_queue.timer_skew ());
// Look for a node in the timer queue whose timer <= the present
// time.
ACE_Timer_Node_Dispatch_Info dispatch_info;
if (timer_queue.dispatch_info (current_time,
dispatch_info))
{
const void *upcall_act = 0;
// Preinvoke.
timer_queue.preinvoke (dispatch_info,
current_time,
upcall_act);
// Call the functor
timer_queue.upcall (dispatch_info,
current_time);
// Postinvoke
timer_queue.postinvoke (dispatch_info,
current_time,
upcall_act);
// We have dispatched a timer
return 1;
}
return 0;
}
void
expire (ACE_Timer_Queue &timer_queue,
Expire_Function expire_function)
{
int events = 0;
int result = 0;
Reference_Counted_Event_Handler *handler =
new Reference_Counted_Event_Handler (1);
ACE_Event_Handler_var safe_handler (handler);
long timer_id =
timer_queue.schedule (handler,
one_second_timeout,
ACE_Time_Value (1) + timer_queue.gettimeofday (),
ACE_Time_Value (1));
ACE_TEST_ASSERT (timer_id != -1);
result =
timer_queue.schedule (handler,
two_second_timeout,
ACE_Time_Value (2) + timer_queue.gettimeofday ());
ACE_TEST_ASSERT (result != -1);
events += 4;
for (int i = 0; i < events;)
{
WAIT_FOR_NEXT_EVENT (timer_queue);
result =
expire_function (timer_queue);
ACE_TEST_ASSERT (result >= 0);
i += result;
}
timer_queue.cancel (timer_id, 0, 0);
}
template<class TIMER_QUEUE>
class expire_test
{
public:
expire_test (const char *);
};
template <class TIMER_QUEUE>
expire_test<TIMER_QUEUE>::expire_test (const char *timer_queue_type)
{
ACE_DEBUG ((LM_DEBUG,
"\nExpire test for %C\n\n",
timer_queue_type));
TIMER_QUEUE timer_queue;
expire (timer_queue,
invoke_expire);
}
template<class TIMER_QUEUE>
class upcall_test
{
public:
upcall_test (const char *);
};
template <class TIMER_QUEUE>
upcall_test<TIMER_QUEUE>::upcall_test (const char *timer_queue_type)
{
ACE_DEBUG ((LM_DEBUG,
"\nOne upcall test for %C\n\n",
timer_queue_type));
TIMER_QUEUE timer_queue;
expire (timer_queue,
invoke_one_upcall);
}
class Simple_Event_Handler : public ACE_Event_Handler
{
public:
Simple_Event_Handler (void);
~Simple_Event_Handler (void);
int handle_timeout (const ACE_Time_Value &,
const void *);
int handle_close (ACE_HANDLE,
ACE_Reactor_Mask);
};
Simple_Event_Handler::Simple_Event_Handler (void)
{
if (debug)
ACE_DEBUG ((LM_DEBUG,
"Simple_Event_Handler()\n"));
}
Simple_Event_Handler::~Simple_Event_Handler (void)
{
if (debug)
ACE_DEBUG ((LM_DEBUG,
"~Simple_Event_Handler()\n"));
}
int
Simple_Event_Handler::handle_timeout (const ACE_Time_Value &,
const void *arg)
{
if (debug)
ACE_DEBUG ((LM_DEBUG,
"Simple_Event_Handler::handle_timeout() for arg = %C\n",
(const char *) arg));
return 0;
}
int
Simple_Event_Handler::handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask masks)
{
if (debug)
ACE_DEBUG ((LM_DEBUG,
"Simple_Event_Handler::handle_close() called with handle = %d and masks = %d.\n",
handle,
masks));
delete this;
return 0;
}
void
simple (ACE_Timer_Queue &timer_queue)
{
int events = 0;
int result = 0;
long timer_id = -1;
{
Simple_Event_Handler *handler =
new Simple_Event_Handler;
timer_id =
timer_queue.schedule (handler,
one_second_timeout,
ACE_Time_Value (1) + timer_queue.gettimeofday (),
ACE_Time_Value (1));
ACE_TEST_ASSERT (timer_id != -1);
result =
timer_queue.cancel (timer_id,
0,
0);
ACE_TEST_ASSERT (result == 1);
}
{
Simple_Event_Handler *handler =
new Simple_Event_Handler;
timer_id =
timer_queue.schedule (handler,
one_second_timeout,
ACE_Time_Value (1) + timer_queue.gettimeofday (),
ACE_Time_Value (1));
ACE_TEST_ASSERT (timer_id != -1);
events += 3;
}
for (int i = 0; i < events;)
{
WAIT_FOR_NEXT_EVENT (timer_queue);
result =
timer_queue.expire ();
ACE_TEST_ASSERT (result >= 0);
i += result;
}
timer_queue.cancel (timer_id, 0, 0);
}
template <class TIMER_QUEUE>
class simple_test
{
public:
simple_test (const char *);
};
template <class TIMER_QUEUE>
simple_test<TIMER_QUEUE>::simple_test (const char *timer_queue_type)
{
ACE_DEBUG ((LM_DEBUG,
"\nSimple test for %C\n\n",
timer_queue_type));
TIMER_QUEUE timer_queue;
simple (timer_queue);
}
static int heap = 1;
static int list = 1;
static int hash = 1;
static int wheel = 1;
static int hashheap = 1;
static int test_cancellation = 1;
static int test_expire = 1;
static int test_one_upcall = 1;
static int test_simple = 1;
static int
parse_args (int argc, ACE_TCHAR *argv[])
{
ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("a:b:c:d:e:l:m:n:o:z:"));
int cc;
while ((cc = get_opt ()) != -1)
{
switch (cc)
{
case 'a':
heap = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'b':
list = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'c':
hash = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'd':
wheel = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'e':
hashheap = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'l':
test_cancellation = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'm':
test_expire = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'n':
test_one_upcall = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'o':
test_simple = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'z':
debug = ACE_OS::atoi (get_opt.opt_arg ());
break;
default:
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("\nusage: %s \n\n")
ACE_TEXT ("\t[-a heap] (defaults to %d)\n")
ACE_TEXT ("\t[-b list] (defaults to %d)\n")
ACE_TEXT ("\t[-c hash] (defaults to %d)\n")
ACE_TEXT ("\t[-d wheel] (defaults to %d)\n")
ACE_TEXT ("\t[-e hashheap] (defaults to %d)\n")
ACE_TEXT ("\t[-l test_cancellation] (defaults to %d)\n")
ACE_TEXT ("\t[-m test_expire] (defaults to %d)\n")
ACE_TEXT ("\t[-n test_one_upcall] (defaults to %d)\n")
ACE_TEXT ("\t[-o test_simple] (defaults to %d)\n")
ACE_TEXT ("\t[-z debug] (defaults to %d)\n")
ACE_TEXT ("\n"),
argv[0],
heap,
list,
hash,
wheel,
hashheap,
test_cancellation,
test_expire,
test_one_upcall,
test_simple,
debug));
return -1;
}
}
return 0;
}
int
run_main (int argc, ACE_TCHAR *argv[])
{
ACE_START_TEST (ACE_TEXT ("Timer_Queue_Reference_Counting_Test"));
// Validate options.
int result =
parse_args (argc, argv);
if (result != 0)
return result;
if (test_cancellation)
{
ACE_DEBUG ((LM_DEBUG,
"\nCancellation test...\n\n"));
if (heap) { cancellation_test<ACE_Timer_Heap> test ("ACE_Timer_Heap"); ACE_UNUSED_ARG (test); }
if (list) { cancellation_test<ACE_Timer_List> test ("ACE_Timer_List"); ACE_UNUSED_ARG (test); }
if (hash) { cancellation_test<ACE_Timer_Hash> test ("ACE_Timer_Hash"); ACE_UNUSED_ARG (test); }
if (wheel) { cancellation_test<ACE_Timer_Wheel> test ("ACE_Timer_Wheel"); ACE_UNUSED_ARG (test); }
if (hashheap) { cancellation_test<ACE_Timer_Hash_Heap> test ("ACE_Timer_Hash_Heap"); ACE_UNUSED_ARG (test); }
}
if (test_expire)
{
ACE_DEBUG ((LM_DEBUG,
"\nExpire test...\n\n"));
if (heap) { expire_test<ACE_Timer_Heap> test ("ACE_Timer_Heap"); ACE_UNUSED_ARG (test); }
if (list) { expire_test<ACE_Timer_List> test ("ACE_Timer_List"); ACE_UNUSED_ARG (test); }
if (hash) { expire_test<ACE_Timer_Hash> test ("ACE_Timer_Hash"); ACE_UNUSED_ARG (test); }
if (wheel) { expire_test<ACE_Timer_Wheel> test ("ACE_Timer_Wheel"); ACE_UNUSED_ARG (test); }
if (hashheap) { expire_test<ACE_Timer_Hash_Heap> test ("ACE_Timer_Hash_Heap"); ACE_UNUSED_ARG (test); }
}
if (test_one_upcall)
{
ACE_DEBUG ((LM_DEBUG,
"\nOne upcall at a time test...\n\n"));
if (heap) { upcall_test<ACE_Timer_Heap> test ("ACE_Timer_Heap"); ACE_UNUSED_ARG (test); }
if (list) { upcall_test<ACE_Timer_List> test ("ACE_Timer_List"); ACE_UNUSED_ARG (test); }
if (hash) { upcall_test<ACE_Timer_Hash> test ("ACE_Timer_Hash"); ACE_UNUSED_ARG (test); }
if (wheel) { upcall_test<ACE_Timer_Wheel> test ("ACE_Timer_Wheel"); ACE_UNUSED_ARG (test); }
if (hashheap) { upcall_test<ACE_Timer_Hash_Heap> test ("ACE_Timer_Hash_Heap"); ACE_UNUSED_ARG (test); }
}
if (test_simple)
{
ACE_DEBUG ((LM_DEBUG,
"\nSimple test...\n\n"));
if (heap) { simple_test<ACE_Timer_Heap> test ("ACE_Timer_Heap"); ACE_UNUSED_ARG (test); }
if (list) { simple_test<ACE_Timer_List> test ("ACE_Timer_List"); ACE_UNUSED_ARG (test); }
if (hash) { simple_test<ACE_Timer_Hash> test ("ACE_Timer_Hash"); ACE_UNUSED_ARG (test); }
if (wheel) { simple_test<ACE_Timer_Wheel> test ("ACE_Timer_Wheel"); ACE_UNUSED_ARG (test); }
if (hashheap) { simple_test<ACE_Timer_Hash_Heap> test ("ACE_Timer_Hash_Heap"); ACE_UNUSED_ARG (test); }
}
ACE_END_TEST;
return 0;
}
|