ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / tests / Reference_Counted_Event_Handler_Test.cpp
blob7e2081b837deaef9f022780c881ca1ef0793e199
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:
768 Closed_In_Upcall_Event_Handler (int &events);
770 ~Closed_In_Upcall_Event_Handler () override;
772 int handle_input (ACE_HANDLE) override;
774 int handle_close (ACE_HANDLE,
775 ACE_Reactor_Mask) override;
777 ACE_Event_Handler::Reference_Count add_reference () override;
779 ACE_Event_Handler::Reference_Count remove_reference () override;
781 ACE_Pipe pipe_;
783 int &events_;
787 Closed_In_Upcall_Event_Handler::Closed_In_Upcall_Event_Handler (int &events)
788 : events_ (events)
790 if (-1 == this->pipe_.open ())
791 ACE_ERROR ((LM_ERROR,
792 ACE_TEXT ("%p\n"),
793 ACE_TEXT ("Closed_In_Upcall_Event_Handler pipe open")));
795 this->reference_counting_policy ().value
796 (ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
798 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Closed_In_Upcall_Event_Handler()\n")));
801 Closed_In_Upcall_Event_Handler::~Closed_In_Upcall_Event_Handler ()
803 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("~Closed_In_Upcall_Event_Handler()\n")));
805 this->pipe_.close ();
809 Closed_In_Upcall_Event_Handler::handle_input (ACE_HANDLE)
811 ACE_DEBUG ((LM_DEBUG,
812 ACE_TEXT ("Closed_In_Upcall_Event_Handler::handle_input()\n")));
814 this->events_--;
816 if (0 != this->reactor ()->remove_handler (this->pipe_.read_handle (),
817 ACE_Event_Handler::ALL_EVENTS_MASK))
818 ACE_ERROR ((LM_ERROR,
819 ACE_TEXT ("line %l %p\n"),
820 ACE_TEXT ("remove_handler")));
822 char buf[message_size + 1];
824 ssize_t recv_result =
825 ACE::recv_n (this->pipe_.read_handle (),
826 buf,
827 sizeof buf - 1);
829 if (recv_result != message_size)
830 ACE_ERROR ((LM_ERROR, ACE_TEXT ("line %l recv_n got %b; should be %d\n"),
831 recv_result, message_size));
833 buf[message_size] = '\0';
835 ACE_DEBUG ((LM_DEBUG,
836 ACE_TEXT ("Message received: %C\n"),
837 buf));
839 return 0;
843 Closed_In_Upcall_Event_Handler::handle_close (ACE_HANDLE handle,
844 ACE_Reactor_Mask masks)
846 ACE_DEBUG ((LM_DEBUG,
847 ACE_TEXT ("Closed_In_Upcall_Event_Handler::handle_close() ")
848 ACE_TEXT ("called with handle = %d and masks = %d. ")
849 ACE_TEXT ("Reference count is %d\n"),
850 handle,
851 masks,
852 this->reference_count_.load ()));
854 return 0;
857 ACE_Event_Handler::Reference_Count
858 Closed_In_Upcall_Event_Handler::add_reference ()
860 ACE_Event_Handler::Reference_Count reference_count =
861 this->ACE_Event_Handler::add_reference ();
863 ACE_DEBUG ((LM_DEBUG,
864 ACE_TEXT ("Reference count after add_reference() is %d\n"),
865 this->reference_count_.load ()));
867 return reference_count;
870 ACE_Event_Handler::Reference_Count
871 Closed_In_Upcall_Event_Handler::remove_reference ()
873 ACE_Event_Handler::Reference_Count reference_count =
874 this->ACE_Event_Handler::remove_reference ();
876 ACE_DEBUG ((LM_DEBUG,
877 ACE_TEXT ("Reference count after remove_reference() is %d\n"),
878 reference_count));
880 return reference_count;
883 void
884 closed_in_upcall_event_handler (ACE_Reactor *reactor)
886 int events = 0;
888 if (test_io)
890 Closed_In_Upcall_Event_Handler *handler = 0;
891 ACE_NEW (handler, Closed_In_Upcall_Event_Handler (events));
893 ACE_Event_Handler_var safe_handler (handler);
895 ssize_t send_n_result =
896 ACE::send_n (handler->pipe_.write_handle (),
897 message,
898 message_size);
900 if (send_n_result != message_size)
901 ACE_ERROR ((LM_ERROR,
902 ACE_TEXT ("line %l send_n sent %b; should be %d\n"),
903 send_n_result, message_size));
905 if (-1 == reactor->register_handler (handler->pipe_.read_handle (),
906 handler,
907 ACE_Event_Handler::READ_MASK))
908 ACE_ERROR ((LM_ERROR,
909 ACE_TEXT ("line %l %p\n"),
910 ACE_TEXT ("register_handler")));
911 else
912 events += 1;
915 while (events > 0)
917 int handle_events_result =
918 reactor->handle_events ();
919 ACE_UNUSED_ARG (handle_events_result);
923 void
924 closed_in_upcall (ACE_Reactor_Impl *impl)
926 ACE_Reactor reactor (impl, 1);
928 ACE_DEBUG ((LM_DEBUG,
929 ACE_TEXT ("\nTesting Closed in Upcall Event Handler....\n\n")));
931 closed_in_upcall_event_handler (&reactor);
934 template <class REACTOR_IMPLEMENTATION>
935 class test
937 public:
938 test ();
941 template <class REACTOR_IMPLEMENTATION>
942 test<REACTOR_IMPLEMENTATION>::test ()
944 if (test_simple_event_handler)
945 simple (new REACTOR_IMPLEMENTATION);
947 if (test_reference_counted_event_handler_1)
948 reference_count_1 (new REACTOR_IMPLEMENTATION);
950 if (test_reference_counted_event_handler_2)
951 reference_count_2 (new REACTOR_IMPLEMENTATION);
953 if (test_closed_in_upcall_event_handler)
954 closed_in_upcall (new REACTOR_IMPLEMENTATION);
957 static int
958 parse_args (int argc, ACE_TCHAR *argv[])
960 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("a:b:c:d:f:g:h:i:k:l:m:z:"));
962 int cc;
963 while ((cc = get_opt ()) != -1)
965 switch (cc)
967 case 'a':
968 test_select_reactor = ACE_OS::atoi (get_opt.opt_arg ());
969 break;
970 case 'b':
971 test_tp_reactor = ACE_OS::atoi (get_opt.opt_arg ());
972 break;
973 case 'c':
974 test_wfmo_reactor = ACE_OS::atoi (get_opt.opt_arg ());
975 break;
976 case 'd':
977 test_dev_poll_reactor = ACE_OS::atoi (get_opt.opt_arg ());
978 break;
979 case 'f':
980 test_simple_event_handler = ACE_OS::atoi (get_opt.opt_arg ());
981 break;
982 case 'g':
983 test_reference_counted_event_handler_1 = ACE_OS::atoi (get_opt.opt_arg ());
984 break;
985 case 'h':
986 test_reference_counted_event_handler_2 = ACE_OS::atoi (get_opt.opt_arg ());
987 break;
988 case 'i':
989 test_closed_in_upcall_event_handler = ACE_OS::atoi (get_opt.opt_arg ());
990 break;
991 case 'k':
992 test_io = ACE_OS::atoi (get_opt.opt_arg ());
993 break;
994 case 'l':
995 test_timers = ACE_OS::atoi (get_opt.opt_arg ());
996 break;
997 case 'm':
998 test_find = ACE_OS::atoi (get_opt.opt_arg ());
999 break;
1000 case 'z':
1001 debug = ACE_OS::atoi (get_opt.opt_arg ());
1002 break;
1003 case '?':
1004 default:
1005 ACE_ERROR ((LM_ERROR,
1006 ACE_TEXT ("\nusage: %s \n\n")
1007 ACE_TEXT ("\t[-a test Select Reactor] (defaults to %d)\n")
1008 ACE_TEXT ("\t[-b test TP Reactor] (defaults to %d)\n")
1009 ACE_TEXT ("\t[-c test WFMO Reactor] (defaults to %d)\n")
1010 ACE_TEXT ("\t[-d test Dev Poll Reactor] (defaults to %d)\n")
1011 ACE_TEXT ("\t[-f test simple event handler] (defaults to %d)\n")
1012 ACE_TEXT ("\t[-g test reference counted event handler (first test)] (defaults to %d)\n")
1013 ACE_TEXT ("\t[-h test reference counted event handler (second test)] (defaults to %d)\n")
1014 ACE_TEXT ("\t[-i test closed in upcall event handler] (defaults to %d)\n")
1015 ACE_TEXT ("\t[-k test io] (defaults to %d)\n")
1016 ACE_TEXT ("\t[-l test timers] (defaults to %d)\n")
1017 ACE_TEXT ("\t[-m test find] (defaults to %d)\n")
1018 ACE_TEXT ("\t[-z debug] (defaults to %d)\n")
1019 ACE_TEXT ("\n"),
1020 argv[0],
1021 test_select_reactor,
1022 test_tp_reactor,
1023 test_wfmo_reactor,
1024 test_dev_poll_reactor,
1025 test_simple_event_handler,
1026 test_reference_counted_event_handler_1,
1027 test_reference_counted_event_handler_2,
1028 test_closed_in_upcall_event_handler,
1029 test_io,
1030 test_timers,
1031 test_find,
1032 debug));
1033 return -1;
1037 return 0;
1041 run_main (int argc, ACE_TCHAR *argv[])
1043 ACE_START_TEST (ACE_TEXT ("Reference_Counted_Event_Handler_Test"));
1045 // Validate options.
1046 int result =
1047 parse_args (argc, argv);
1048 if (result != 0)
1049 return result;
1051 if (test_select_reactor)
1053 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n\nTesting Select Reactor....\n\n")));
1055 test<ACE_Select_Reactor> test;
1056 ACE_UNUSED_ARG (test);
1059 if (test_tp_reactor)
1061 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n\nTesting TP Reactor....\n\n")));
1063 test<ACE_TP_Reactor> test;
1064 ACE_UNUSED_ARG (test);
1067 #if defined (ACE_WIN32) && \
1068 (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0))
1070 if (test_wfmo_reactor)
1072 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n\nTesting WFMO Reactor....\n\n")));
1074 test<ACE_WFMO_Reactor> test;
1075 ACE_UNUSED_ARG (test);
1078 #endif /* ACE_WIN32 && ACE_HAS_WINSOCK2 */
1080 #if defined (ACE_HAS_DEV_POLL) || defined (ACE_HAS_EVENT_POLL)
1082 if (test_dev_poll_reactor)
1084 ACE_DEBUG ((LM_DEBUG,
1085 ACE_TEXT ("\n\nTesting ACE_Dev_Poll_Reactor....\n\n")));
1087 test<ACE_Dev_Poll_Reactor> test;
1088 ACE_UNUSED_ARG (test);
1091 #endif /* ACE_HAS_DEV_POLL || ACE_HAS_EVENT_POLL */
1093 ACE_END_TEST;
1095 return result;