Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / tests / Reference_Counted_Event_Handler_Test.cpp
blob02a2272cbd5b0349f10f037c4c6ab90259560799
2 //=============================================================================
3 /**
4 * @file Reference_Counted_Event_Handler_Test.cpp
6 * This test is used to check reference counting of the Event
7 * Handler when it interacts with the Reactor.
9 * @author Irfan Pyarali <irfan@oomworks.com>
11 //=============================================================================
14 #include "test_config.h"
15 #include "ace/Reactor.h"
16 #include "ace/Select_Reactor.h"
17 #include "ace/TP_Reactor.h"
18 #include "ace/WFMO_Reactor.h"
19 #include "ace/Dev_Poll_Reactor.h"
20 #include "ace/Get_Opt.h"
21 #include "ace/ACE.h"
23 static const char message[] = "abcdefghijklmnopqrstuvwxyz";
24 static const int message_size = 26;
25 static int test_select_reactor = 1;
26 static int test_tp_reactor = 1;
27 static int test_wfmo_reactor = 1;
28 static int test_dev_poll_reactor = 1;
29 static int test_io = 1;
30 static int test_timers = 1;
31 static int test_find = 1;
32 static int test_simple_event_handler = 1;
33 static int test_reference_counted_event_handler_1 = 1;
34 static int test_reference_counted_event_handler_2 = 1;
35 static int test_closed_in_upcall_event_handler = 1;
36 static int debug = 1;
37 static const char *one_second_timeout = "one second timeout";
38 static const char *two_second_timeout = "two second timeout";
40 class Reference_Counted_Event_Handler : public ACE_Event_Handler
42 public:
43 Reference_Counted_Event_Handler (int &events);
45 ~Reference_Counted_Event_Handler () override;
47 int handle_input (ACE_HANDLE) override;
49 int handle_output (ACE_HANDLE) override;
51 int handle_timeout (const ACE_Time_Value &,
52 const void *) override;
54 int handle_signal (int, siginfo_t *, ucontext_t *) override;
56 int handle_close (ACE_HANDLE,
57 ACE_Reactor_Mask) override;
59 ACE_Event_Handler::Reference_Count add_reference () override;
61 ACE_Event_Handler::Reference_Count remove_reference () override;
63 ACE_Pipe pipe_;
65 int &events_;
68 Reference_Counted_Event_Handler::Reference_Counted_Event_Handler (int &events)
69 : events_ (events)
71 if (this->pipe_.open () != 0)
72 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ctor: pipe open")));
74 this->reference_counting_policy ().value
75 (ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
77 ACE_DEBUG ((LM_DEBUG,
78 "Reference count in Reference_Counted_Event_Handler() is %d\n",
79 this->reference_count_.load ()));
82 Reference_Counted_Event_Handler::~Reference_Counted_Event_Handler ()
84 ACE_DEBUG ((LM_DEBUG,
85 "Reference count in ~Reference_Counted_Event_Handler() is %d\n",
86 this->reference_count_.load ()));
88 this->pipe_.close ();
91 int
92 Reference_Counted_Event_Handler::handle_input (ACE_HANDLE)
94 ACE_DEBUG ((LM_DEBUG,
95 "Reference count in Reference_Counted_Event_Handler::handle_input() is %d\n",
96 this->reference_count_.load ()));
98 --this->events_;
100 char buf[message_size + 1];
102 ssize_t result =
103 ACE::recv_n (this->pipe_.read_handle (),
104 buf,
105 sizeof buf - 1);
107 if (result != message_size)
108 ACE_ERROR ((LM_ERROR,
109 ACE_TEXT ("recv_n expected %d bytes; got %b\n"),
110 message_size,
111 result));
113 buf[message_size] = '\0';
115 ACE_DEBUG ((LM_DEBUG,
116 "Message received: %C\n",
117 buf));
119 return 0;
123 Reference_Counted_Event_Handler::handle_output (ACE_HANDLE)
125 ACE_DEBUG ((LM_DEBUG,
126 "Reference count in Reference_Counted_Event_Handler::handle_output() is %d\n",
127 this->reference_count_.load ()));
129 --this->events_;
131 ssize_t result =
132 ACE::send_n (this->pipe_.write_handle (),
133 message,
134 message_size);
136 if (result != message_size)
137 ACE_ERROR ((LM_ERROR,
138 ACE_TEXT ("send_n sent %b bytes; should be %d\n"),
139 result,
140 message_size));
142 // No longer interested in output.
143 return -1;
147 Reference_Counted_Event_Handler::handle_timeout (const ACE_Time_Value &,
148 const void *arg)
150 ACE_DEBUG ((LM_DEBUG,
151 "Reference count in Reference_Counted_Event_Handler::handle_timeout() for arg = %C is %d\n",
152 (const char *) arg,
153 this->reference_count_.load ()));
155 --this->events_;
157 return 0;
161 Reference_Counted_Event_Handler::handle_signal (int,
162 siginfo_t *,
163 ucontext_t *)
165 return 0;
169 Reference_Counted_Event_Handler::handle_close (ACE_HANDLE handle,
170 ACE_Reactor_Mask masks)
172 ACE_DEBUG ((LM_DEBUG,
173 "Reference_Counted_Event_Handler::handle_close() called with handle = %d and masks = %d. "
174 "Reference count is %d\n",
175 handle,
176 masks,
177 this->reference_count_.load ()));
179 return 0;
182 ACE_Event_Handler::Reference_Count
183 Reference_Counted_Event_Handler::add_reference ()
185 ACE_Event_Handler::Reference_Count reference_count =
186 this->ACE_Event_Handler::add_reference ();
188 ACE_DEBUG ((LM_DEBUG,
189 "Reference count after add_reference() is %d\n",
190 this->reference_count_.load ()));
192 return reference_count;
195 ACE_Event_Handler::Reference_Count
196 Reference_Counted_Event_Handler::remove_reference ()
198 ACE_Event_Handler::Reference_Count reference_count =
199 this->ACE_Event_Handler::remove_reference ();
201 ACE_DEBUG ((LM_DEBUG,
202 "Reference count after remove_reference() is %d\n",
203 reference_count));
205 return reference_count;
208 void
209 reference_counted_event_handler_test_1 (ACE_Reactor *reactor)
211 int events = 0;
212 int result = 0;
214 Reference_Counted_Event_Handler *handler =
215 new Reference_Counted_Event_Handler (events);
217 ACE_Event_Handler_var safe_handler (handler);
219 if (test_io)
221 if (-1 == reactor->register_handler (handler->pipe_.read_handle (),
222 handler,
223 ACE_Event_Handler::READ_MASK))
224 ACE_ERROR ((LM_ERROR,
225 ACE_TEXT ("line %l %p\n"),
226 ACE_TEXT ("register pipe read")));
227 else
228 ++events;
230 if (-1 == reactor->register_handler (handler->pipe_.write_handle (),
231 handler,
232 ACE_Event_Handler::WRITE_MASK))
233 ACE_ERROR ((LM_ERROR,
234 ACE_TEXT ("line %l %p\n"),
235 ACE_TEXT ("register pipe write")));
236 else
237 events++;
240 if (test_timers)
242 ACE_Time_Value const one_second (1);
243 long timer_id =
244 reactor->schedule_timer (handler,
245 one_second_timeout,
246 one_second,
247 one_second);
248 if (timer_id == -1)
249 ACE_ERROR ((LM_ERROR,
250 ACE_TEXT ("line %l %p\n"),
251 ACE_TEXT ("schedule_timer")));
252 else
253 if ((result = reactor->cancel_timer (timer_id, 0, 0)) != 1)
254 ACE_ERROR ((LM_ERROR,
255 ACE_TEXT ("cancel_timer returned %d; should be 1\n"),
256 result));
258 timer_id =
259 reactor->schedule_timer (handler,
260 one_second_timeout,
261 one_second,
262 one_second);
263 if (timer_id == -1)
264 ACE_ERROR ((LM_ERROR,
265 ACE_TEXT ("line %l %p\n"),
266 ACE_TEXT ("schedule_timer")));
267 else
268 events += 2; // Wait for the scheduled and one repeating
270 ACE_Time_Value const two_second (2);
271 timer_id =
272 reactor->schedule_timer (handler,
273 two_second_timeout,
274 two_second);
275 if (timer_id == -1)
276 ACE_ERROR ((LM_ERROR,
277 ACE_TEXT ("line %l %p\n"),
278 ACE_TEXT ("schedule_timer")));
279 else
280 events++;
283 while (events > 0)
285 result =
286 reactor->handle_events ();
290 void
291 reference_counted_event_handler_test_2 (ACE_Reactor *reactor)
293 int events = 0;
294 int result = 0;
295 ACE_Time_Value const one_second (1);
297 if (test_find)
299 Reference_Counted_Event_Handler *handler =
300 new Reference_Counted_Event_Handler (events);
302 ACE_Event_Handler_var safe_handler (handler);
304 result =
305 reactor->register_handler (handler->pipe_.read_handle (),
306 handler,
307 ACE_Event_Handler::READ_MASK);
308 if (result != 0)
309 ACE_ERROR ((LM_ERROR,
310 ACE_TEXT ("line %l %p\n"),
311 ACE_TEXT ("register pipe handler read")));
312 else
314 ACE_Event_Handler *result_handler = 0;
316 result =
317 reactor->handler (handler->pipe_.read_handle (),
318 ACE_Event_Handler::READ_MASK,
319 &result_handler);
320 ACE_Event_Handler_var safe_result_handler (result_handler);
322 if (result != 0)
323 ACE_ERROR ((LM_ERROR,
324 ACE_TEXT ("%p\n"),
325 ACE_TEXT ("Looking up pipe read handler")));
326 else
327 if (result_handler != handler)
328 ACE_ERROR ((LM_ERROR,
329 ACE_TEXT ("Mismatch: result_handler %@ should be %@\n"),
330 result_handler, handler));
334 ACE_Event_Handler *result_handler = 0;
336 result =
337 reactor->handler (handler->pipe_.read_handle (),
338 ACE_Event_Handler::WRITE_MASK,
339 &result_handler);
340 ACE_Event_Handler_var safe_result_handler (result_handler);
342 if (result == 0)
343 ACE_ERROR ((LM_ERROR,
344 ACE_TEXT ("Pipe write handler succeeded but shouldn't")));
348 ACE_Event_Handler_var result_handler =
349 reactor->find_handler (handler->pipe_.read_handle ());
351 if (result_handler.handler () != handler)
352 ACE_ERROR ((LM_ERROR,
353 ACE_TEXT ("Mismatch 2: result_handler %@ should be %@\n"),
354 result_handler.handler (), handler));
358 if (test_io)
360 Reference_Counted_Event_Handler *handler =
361 new Reference_Counted_Event_Handler (events);
363 ACE_Event_Handler_var safe_handler (handler);
365 if (-1 == reactor->register_handler (handler->pipe_.read_handle (),
366 handler,
367 ACE_Event_Handler::READ_MASK))
368 ACE_ERROR ((LM_ERROR,
369 ACE_TEXT ("line %l %p\n"),
370 ACE_TEXT ("register pipe read")));
371 else
372 ++events;
374 if (-1 == reactor->register_handler (handler->pipe_.write_handle (),
375 handler,
376 ACE_Event_Handler::WRITE_MASK))
377 ACE_ERROR ((LM_ERROR,
378 ACE_TEXT ("line %l %p\n"),
379 ACE_TEXT ("register pipe write")));
380 else
381 events++;
384 if (test_timers)
386 Reference_Counted_Event_Handler *handler =
387 new Reference_Counted_Event_Handler (events);
389 ACE_Event_Handler_var safe_handler (handler);
391 long timer_id =
392 reactor->schedule_timer (handler,
393 one_second_timeout,
394 one_second,
395 one_second);
396 if (timer_id == -1)
397 ACE_ERROR ((LM_ERROR,
398 ACE_TEXT ("line %l %p\n"),
399 ACE_TEXT ("schedule_timer")));
400 else
401 if ((result = reactor->cancel_timer (timer_id, 0, 0)) != 1)
402 ACE_ERROR ((LM_ERROR,
403 ACE_TEXT ("cancel_timer returned %d; should be 1\n"),
404 result));
407 if (test_timers)
409 Reference_Counted_Event_Handler *handler =
410 new Reference_Counted_Event_Handler (events);
412 ACE_Event_Handler_var safe_handler (handler);
414 long timer_id =
415 reactor->schedule_timer (handler,
416 one_second_timeout,
417 one_second,
418 one_second);
419 if (timer_id == -1)
420 ACE_ERROR ((LM_ERROR,
421 ACE_TEXT ("line %l %p\n"),
422 ACE_TEXT ("schedule_timer")));
423 else
424 events += 2; // Wait for the scheduled and one repeating
426 ACE_Time_Value const two_second (2);
427 timer_id =
428 reactor->schedule_timer (handler,
429 two_second_timeout,
430 two_second);
431 if (timer_id == -1)
432 ACE_ERROR ((LM_ERROR,
433 ACE_TEXT ("line %l %p\n"),
434 ACE_TEXT ("schedule_timer")));
435 else
436 events++;
439 while (events > 0)
441 result =
442 reactor->handle_events ();
446 void
447 reference_count_1 (ACE_Reactor_Impl *impl)
449 ACE_Reactor reactor (impl, 1);
451 ACE_DEBUG ((LM_DEBUG,
452 "\nTesting Reference Counted Event Handler Test 1....\n\n"));
454 reference_counted_event_handler_test_1 (&reactor);
457 void
458 reference_count_2 (ACE_Reactor_Impl *impl)
460 ACE_Reactor reactor (impl, 1);
462 ACE_DEBUG ((LM_DEBUG,
463 "\nTesting Reference Counted Event Handler Test 2....\n\n"));
465 reference_counted_event_handler_test_2 (&reactor);
468 class Simple_Event_Handler : public ACE_Event_Handler
470 public:
471 Simple_Event_Handler (int &events,
472 int close_count);
474 ~Simple_Event_Handler () override;
476 int handle_input (ACE_HANDLE) override;
478 int handle_output (ACE_HANDLE) override;
480 int handle_timeout (const ACE_Time_Value &,
481 const void *) override;
483 int handle_signal (int, siginfo_t *, ucontext_t *) override;
485 int handle_close (ACE_HANDLE, ACE_Reactor_Mask) override;
487 ACE_Pipe pipe_;
489 int &events_;
491 int close_count_;
494 Simple_Event_Handler::Simple_Event_Handler (int &events,
495 int close_count)
496 : events_ (events),
497 close_count_ (close_count)
499 if (-1 == this->pipe_.open ())
500 ACE_ERROR ((LM_ERROR,
501 ACE_TEXT ("%p\n"),
502 ACE_TEXT ("Simple_Event_Handler pipe open")));
504 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Simple_Event_Handler()\n")));
507 Simple_Event_Handler::~Simple_Event_Handler ()
509 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("~Simple_Event_Handler()\n")));
511 this->pipe_.close ();
515 Simple_Event_Handler::handle_input (ACE_HANDLE)
517 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Simple_Event_Handler::handle_input()\n")));
519 --this->events_;
521 char buf[message_size + 1];
523 ssize_t result =
524 ACE::recv_n (this->pipe_.read_handle (),
525 buf,
526 sizeof buf - 1);
527 if (result != message_size)
528 ACE_ERROR ((LM_ERROR, ACE_TEXT ("line %l recv_n got %b; should be %d\n"),
529 result, message_size));
530 buf[message_size] = '\0';
532 ACE_DEBUG ((LM_DEBUG,
533 ACE_TEXT ("Message received: %C\n"),
534 buf));
536 return 0;
540 Simple_Event_Handler::handle_output (ACE_HANDLE)
542 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Simple_Event_Handler::handle_output()\n")));
544 --this->events_;
546 ssize_t result =
547 ACE::send_n (this->pipe_.write_handle (),
548 message,
549 message_size);
550 if (result != message_size)
551 ACE_ERROR ((LM_ERROR,
552 ACE_TEXT ("line %l send_n sent %b; should be %d\n"),
553 result, message_size));
555 // No longer interested in output.
556 return -1;
560 Simple_Event_Handler::handle_timeout (const ACE_Time_Value &,
561 const void *arg)
563 ACE_DEBUG ((LM_DEBUG,
564 ACE_TEXT ("Simple_Event_Handler::handle_timeout() for arg = %C\n"),
565 (const char *) arg));
567 --this->events_;
569 return 0;
573 Simple_Event_Handler::handle_signal (int,
574 siginfo_t *,
575 ucontext_t *)
577 return 0;
581 Simple_Event_Handler::handle_close (ACE_HANDLE handle,
582 ACE_Reactor_Mask masks)
584 ACE_DEBUG ((LM_DEBUG,
585 ACE_TEXT ("Simple_Event_Handler::handle_close() called with ")
586 ACE_TEXT ("handle = %d and masks = %d with close count = %d.\n"),
587 handle,
588 masks,
589 --this->close_count_));
591 if (this->close_count_ == 0)
592 delete this;
594 return 0;
597 void
598 simple_event_handler (ACE_Reactor *reactor)
600 int events = 0;
601 int result = 0;
602 ACE_Time_Value const one_second (1);
604 if (test_find)
606 Simple_Event_Handler handler (events,
609 result =
610 reactor->register_handler (handler.pipe_.read_handle (),
611 &handler,
612 ACE_Event_Handler::READ_MASK);
613 if (result != 0)
614 ACE_ERROR ((LM_ERROR,
615 ACE_TEXT ("line %l %p\n"),
616 ACE_TEXT ("register pipe handler read")));
617 else
619 ACE_Event_Handler *result_handler = 0;
621 result =
622 reactor->handler (handler.pipe_.read_handle (),
623 ACE_Event_Handler::READ_MASK,
624 &result_handler);
625 ACE_Event_Handler_var safe_result_handler (result_handler);
626 if (result != 0)
627 ACE_ERROR ((LM_ERROR,
628 ACE_TEXT ("%p\n"),
629 ACE_TEXT ("Looking up pipe read handler")));
630 else
631 if (result_handler != &handler)
632 ACE_ERROR ((LM_ERROR,
633 ACE_TEXT ("Mismatch: result_handler %@ should be %@\n"),
634 result_handler, &handler));
638 ACE_Event_Handler *result_handler = 0;
640 result =
641 reactor->handler (handler.pipe_.read_handle (),
642 ACE_Event_Handler::WRITE_MASK,
643 &result_handler);
644 ACE_Event_Handler_var safe_result_handler (result_handler);
645 if (result != -1)
646 ACE_ERROR ((LM_ERROR,
647 ACE_TEXT ("line %l handler() suceeded but shouldn't\n")));
651 ACE_Event_Handler_var result_handler =
652 reactor->find_handler (handler.pipe_.read_handle ());
653 if (result_handler.handler () != &handler)
654 ACE_ERROR ((LM_ERROR,
655 ACE_TEXT ("Mismatch: line %l: result_handler.handler %@ ")
656 ACE_TEXT ("should be %@\n"),
657 result_handler.handler (), &handler));
660 result =
661 reactor->remove_handler (handler.pipe_.read_handle (),
662 ACE_Event_Handler::ALL_EVENTS_MASK | ACE_Event_Handler::DONT_CALL);
663 if (result != 0)
664 ACE_ERROR ((LM_ERROR,
665 ACE_TEXT ("line %l: %p\n"),
666 ACE_TEXT ("remove_handler")));
669 if (test_io)
671 Simple_Event_Handler *handler =
672 new Simple_Event_Handler (events,
675 if (-1 == reactor->register_handler (handler->pipe_.read_handle (),
676 handler,
677 ACE_Event_Handler::READ_MASK))
678 ACE_ERROR ((LM_ERROR,
679 ACE_TEXT ("line %l %p\n"),
680 ACE_TEXT ("register pipe read")));
681 else
682 ++events;
684 if (-1 == reactor->register_handler (handler->pipe_.write_handle (),
685 handler,
686 ACE_Event_Handler::WRITE_MASK))
687 ACE_ERROR ((LM_ERROR,
688 ACE_TEXT ("line %l %p\n"),
689 ACE_TEXT ("register pipe write")));
690 else
691 events++;
694 if (test_timers)
696 Simple_Event_Handler *handler =
697 new Simple_Event_Handler (events,
700 long timer_id =
701 reactor->schedule_timer (handler,
702 one_second_timeout,
703 one_second,
704 one_second);
705 if (timer_id == -1)
706 ACE_ERROR ((LM_ERROR,
707 ACE_TEXT ("line %l %p\n"),
708 ACE_TEXT ("schedule_timer")));
709 else
710 if ((result = reactor->cancel_timer (timer_id, 0, 0)) != 1)
711 ACE_ERROR ((LM_ERROR,
712 ACE_TEXT ("cancel_timer returned %d; should be 1\n"),
713 result));
716 if (test_timers)
718 Simple_Event_Handler *handler =
719 new Simple_Event_Handler (events,
722 long timer_id =
723 reactor->schedule_timer (handler,
724 one_second_timeout,
725 one_second,
726 one_second);
727 if (timer_id == -1)
728 ACE_ERROR ((LM_ERROR,
729 ACE_TEXT ("line %l %p\n"),
730 ACE_TEXT ("schedule_timer")));
731 else
732 events += 2; // Wait for the scheduled and one repeating
734 ACE_Time_Value const two_second (2);
735 timer_id =
736 reactor->schedule_timer (handler,
737 two_second_timeout,
738 two_second);
739 if (timer_id == -1)
740 ACE_ERROR ((LM_ERROR,
741 ACE_TEXT ("line %l %p\n"),
742 ACE_TEXT ("schedule_timer")));
743 else
744 events++;
747 while (events > 0)
749 result =
750 reactor->handle_events ();
754 void
755 simple (ACE_Reactor_Impl *impl)
757 ACE_Reactor reactor (impl, 1);
759 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nTesting Simple Event Handler....\n\n")));
761 simple_event_handler (&reactor);
764 class Closed_In_Upcall_Event_Handler : public ACE_Event_Handler
766 public:
767 Closed_In_Upcall_Event_Handler (int &events);
769 ~Closed_In_Upcall_Event_Handler () override;
771 int handle_input (ACE_HANDLE) override;
773 int handle_close (ACE_HANDLE,
774 ACE_Reactor_Mask) override;
776 ACE_Event_Handler::Reference_Count add_reference () override;
778 ACE_Event_Handler::Reference_Count remove_reference () override;
780 ACE_Pipe pipe_;
782 int &events_;
785 Closed_In_Upcall_Event_Handler::Closed_In_Upcall_Event_Handler (int &events)
786 : events_ (events)
788 if (-1 == this->pipe_.open ())
789 ACE_ERROR ((LM_ERROR,
790 ACE_TEXT ("%p\n"),
791 ACE_TEXT ("Closed_In_Upcall_Event_Handler pipe open")));
793 this->reference_counting_policy ().value
794 (ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
796 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Closed_In_Upcall_Event_Handler()\n")));
799 Closed_In_Upcall_Event_Handler::~Closed_In_Upcall_Event_Handler ()
801 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("~Closed_In_Upcall_Event_Handler()\n")));
803 this->pipe_.close ();
807 Closed_In_Upcall_Event_Handler::handle_input (ACE_HANDLE)
809 ACE_DEBUG ((LM_DEBUG,
810 ACE_TEXT ("Closed_In_Upcall_Event_Handler::handle_input()\n")));
812 this->events_--;
814 if (0 != this->reactor ()->remove_handler (this->pipe_.read_handle (),
815 ACE_Event_Handler::ALL_EVENTS_MASK))
816 ACE_ERROR ((LM_ERROR,
817 ACE_TEXT ("line %l %p\n"),
818 ACE_TEXT ("remove_handler")));
820 char buf[message_size + 1];
822 ssize_t recv_result =
823 ACE::recv_n (this->pipe_.read_handle (),
824 buf,
825 sizeof buf - 1);
827 if (recv_result != message_size)
828 ACE_ERROR ((LM_ERROR, ACE_TEXT ("line %l recv_n got %b; should be %d\n"),
829 recv_result, message_size));
831 buf[message_size] = '\0';
833 ACE_DEBUG ((LM_DEBUG,
834 ACE_TEXT ("Message received: %C\n"),
835 buf));
837 return 0;
841 Closed_In_Upcall_Event_Handler::handle_close (ACE_HANDLE handle,
842 ACE_Reactor_Mask masks)
844 ACE_DEBUG ((LM_DEBUG,
845 ACE_TEXT ("Closed_In_Upcall_Event_Handler::handle_close() ")
846 ACE_TEXT ("called with handle = %d and masks = %d. ")
847 ACE_TEXT ("Reference count is %d\n"),
848 handle,
849 masks,
850 this->reference_count_.load ()));
852 return 0;
855 ACE_Event_Handler::Reference_Count
856 Closed_In_Upcall_Event_Handler::add_reference ()
858 ACE_Event_Handler::Reference_Count reference_count =
859 this->ACE_Event_Handler::add_reference ();
861 ACE_DEBUG ((LM_DEBUG,
862 ACE_TEXT ("Reference count after add_reference() is %d\n"),
863 this->reference_count_.load ()));
865 return reference_count;
868 ACE_Event_Handler::Reference_Count
869 Closed_In_Upcall_Event_Handler::remove_reference ()
871 ACE_Event_Handler::Reference_Count reference_count =
872 this->ACE_Event_Handler::remove_reference ();
874 ACE_DEBUG ((LM_DEBUG,
875 ACE_TEXT ("Reference count after remove_reference() is %d\n"),
876 reference_count));
878 return reference_count;
881 void
882 closed_in_upcall_event_handler (ACE_Reactor *reactor)
884 int events = 0;
886 if (test_io)
888 Closed_In_Upcall_Event_Handler *handler = 0;
889 ACE_NEW (handler, Closed_In_Upcall_Event_Handler (events));
891 ACE_Event_Handler_var safe_handler (handler);
893 ssize_t send_n_result =
894 ACE::send_n (handler->pipe_.write_handle (),
895 message,
896 message_size);
898 if (send_n_result != message_size)
899 ACE_ERROR ((LM_ERROR,
900 ACE_TEXT ("line %l send_n sent %b; should be %d\n"),
901 send_n_result, message_size));
903 if (-1 == reactor->register_handler (handler->pipe_.read_handle (),
904 handler,
905 ACE_Event_Handler::READ_MASK))
906 ACE_ERROR ((LM_ERROR,
907 ACE_TEXT ("line %l %p\n"),
908 ACE_TEXT ("register_handler")));
909 else
910 events += 1;
913 while (events > 0)
915 int handle_events_result =
916 reactor->handle_events ();
917 ACE_UNUSED_ARG (handle_events_result);
921 void
922 closed_in_upcall (ACE_Reactor_Impl *impl)
924 ACE_Reactor reactor (impl, 1);
926 ACE_DEBUG ((LM_DEBUG,
927 ACE_TEXT ("\nTesting Closed in Upcall Event Handler....\n\n")));
929 closed_in_upcall_event_handler (&reactor);
932 template <class REACTOR_IMPLEMENTATION>
933 class test
935 public:
936 test ();
939 template <class REACTOR_IMPLEMENTATION>
940 test<REACTOR_IMPLEMENTATION>::test ()
942 if (test_simple_event_handler)
943 simple (new REACTOR_IMPLEMENTATION);
945 if (test_reference_counted_event_handler_1)
946 reference_count_1 (new REACTOR_IMPLEMENTATION);
948 if (test_reference_counted_event_handler_2)
949 reference_count_2 (new REACTOR_IMPLEMENTATION);
951 if (test_closed_in_upcall_event_handler)
952 closed_in_upcall (new REACTOR_IMPLEMENTATION);
955 static int
956 parse_args (int argc, ACE_TCHAR *argv[])
958 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("a:b:c:d:f:g:h:i:k:l:m:z:"));
960 int cc;
961 while ((cc = get_opt ()) != -1)
963 switch (cc)
965 case 'a':
966 test_select_reactor = ACE_OS::atoi (get_opt.opt_arg ());
967 break;
968 case 'b':
969 test_tp_reactor = ACE_OS::atoi (get_opt.opt_arg ());
970 break;
971 case 'c':
972 test_wfmo_reactor = ACE_OS::atoi (get_opt.opt_arg ());
973 break;
974 case 'd':
975 test_dev_poll_reactor = ACE_OS::atoi (get_opt.opt_arg ());
976 break;
977 case 'f':
978 test_simple_event_handler = ACE_OS::atoi (get_opt.opt_arg ());
979 break;
980 case 'g':
981 test_reference_counted_event_handler_1 = ACE_OS::atoi (get_opt.opt_arg ());
982 break;
983 case 'h':
984 test_reference_counted_event_handler_2 = ACE_OS::atoi (get_opt.opt_arg ());
985 break;
986 case 'i':
987 test_closed_in_upcall_event_handler = ACE_OS::atoi (get_opt.opt_arg ());
988 break;
989 case 'k':
990 test_io = ACE_OS::atoi (get_opt.opt_arg ());
991 break;
992 case 'l':
993 test_timers = ACE_OS::atoi (get_opt.opt_arg ());
994 break;
995 case 'm':
996 test_find = ACE_OS::atoi (get_opt.opt_arg ());
997 break;
998 case 'z':
999 debug = ACE_OS::atoi (get_opt.opt_arg ());
1000 break;
1001 case '?':
1002 default:
1003 ACE_ERROR ((LM_ERROR,
1004 ACE_TEXT ("\nusage: %s \n\n")
1005 ACE_TEXT ("\t[-a test Select Reactor] (defaults to %d)\n")
1006 ACE_TEXT ("\t[-b test TP Reactor] (defaults to %d)\n")
1007 ACE_TEXT ("\t[-c test WFMO Reactor] (defaults to %d)\n")
1008 ACE_TEXT ("\t[-d test Dev Poll Reactor] (defaults to %d)\n")
1009 ACE_TEXT ("\t[-f test simple event handler] (defaults to %d)\n")
1010 ACE_TEXT ("\t[-g test reference counted event handler (first test)] (defaults to %d)\n")
1011 ACE_TEXT ("\t[-h test reference counted event handler (second test)] (defaults to %d)\n")
1012 ACE_TEXT ("\t[-i test closed in upcall event handler] (defaults to %d)\n")
1013 ACE_TEXT ("\t[-k test io] (defaults to %d)\n")
1014 ACE_TEXT ("\t[-l test timers] (defaults to %d)\n")
1015 ACE_TEXT ("\t[-m test find] (defaults to %d)\n")
1016 ACE_TEXT ("\t[-z debug] (defaults to %d)\n")
1017 ACE_TEXT ("\n"),
1018 argv[0],
1019 test_select_reactor,
1020 test_tp_reactor,
1021 test_wfmo_reactor,
1022 test_dev_poll_reactor,
1023 test_simple_event_handler,
1024 test_reference_counted_event_handler_1,
1025 test_reference_counted_event_handler_2,
1026 test_closed_in_upcall_event_handler,
1027 test_io,
1028 test_timers,
1029 test_find,
1030 debug));
1031 return -1;
1035 return 0;
1039 run_main (int argc, ACE_TCHAR *argv[])
1041 ACE_START_TEST (ACE_TEXT ("Reference_Counted_Event_Handler_Test"));
1043 // Validate options.
1044 int result =
1045 parse_args (argc, argv);
1046 if (result != 0)
1047 return result;
1049 if (test_select_reactor)
1051 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n\nTesting Select Reactor....\n\n")));
1053 test<ACE_Select_Reactor> test;
1054 ACE_UNUSED_ARG (test);
1057 if (test_tp_reactor)
1059 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n\nTesting TP Reactor....\n\n")));
1061 test<ACE_TP_Reactor> test;
1062 ACE_UNUSED_ARG (test);
1065 #if defined (ACE_WIN32) && \
1066 (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0))
1068 if (test_wfmo_reactor)
1070 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n\nTesting WFMO Reactor....\n\n")));
1072 test<ACE_WFMO_Reactor> test;
1073 ACE_UNUSED_ARG (test);
1076 #endif /* ACE_WIN32 && ACE_HAS_WINSOCK2 */
1078 #if defined (ACE_HAS_DEV_POLL) || defined (ACE_HAS_EVENT_POLL)
1080 if (test_dev_poll_reactor)
1082 ACE_DEBUG ((LM_DEBUG,
1083 ACE_TEXT ("\n\nTesting ACE_Dev_Poll_Reactor....\n\n")));
1085 test<ACE_Dev_Poll_Reactor> test;
1086 ACE_UNUSED_ARG (test);
1089 #endif /* ACE_HAS_DEV_POLL || ACE_HAS_EVENT_POLL */
1091 ACE_END_TEST;
1093 return result;