Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Bug_2820_Regression_Test.cpp
blob8ef8f3a6b20d677c5206c0dc01be325b27b9b760
1 /**
2 * @file Bug_2820_Regression_Test.cpp
4 * Verify that the event handler reference counting works correctly
5 * when the reactor is destroyed.
7 * Pushing a notification through the reactor increments the reference
8 * count on the target event handler. Both dispatching and purging
9 * the notification decrement the reference count. However,
10 * destroying the reactor used to not decrement the reference count.
11 * This test reproduces the problem and serves as a regression for it.
13 * @author Carlos O'Ryan <coryan@atdesk.com>
16 #include "test_config.h"
17 #include "ace/Auto_Ptr.h"
18 #include "ace/Reactor.h"
19 #include "ace/Select_Reactor.h"
21 /**
22 * @class Simple_Handler
24 * @brief A simple event handler for the test
26 class Simple_Handler : public ACE_Event_Handler
28 public:
29 /// Constructor
30 Simple_Handler(ACE_Reactor * reactor);
32 /// Destructor
33 ~Simple_Handler();
35 /// Receive (and ignore) the notifications
36 virtual int handle_exception(ACE_HANDLE);
39 int
40 run_main (int, ACE_TCHAR *[])
42 ACE_START_TEST (ACE_TEXT ("Bug_2820_Regression_Test"));
44 int result = 0;
46 #if defined ACE_HAS_CPP11
47 std::unique_ptr<ACE_Reactor> reactor(new ACE_Reactor(new ACE_Select_Reactor, 1));
48 ACE_Event_Handler_var v =
49 ACE::make_event_handler<Simple_Handler> (reactor.get());
50 #else
51 auto_ptr<ACE_Reactor> reactor(new ACE_Reactor(new ACE_Select_Reactor, 1));
52 ACE_Event_Handler_var v(
53 new Simple_Handler(reactor.get()));
54 #endif
56 ACE_Event_Handler::Reference_Count pre_notify_count =
57 v->add_reference();
59 int const notify_count = 4;
60 for(int i = 0; i != notify_count; ++i)
62 reactor->notify(v.handler());
65 ACE_Event_Handler::Reference_Count pos_notify_count =
66 v->add_reference();
68 if(pos_notify_count != pre_notify_count + notify_count + 1)
70 result = -1;
71 ACE_ERROR((LM_ERROR,
72 ACE_TEXT("Reference count should increase by %d.")
73 ACE_TEXT(" Initial count=%d, final count = %d\n"),
74 notify_count, pre_notify_count, pos_notify_count));
77 ACE_auto_ptr_reset(reactor, (ACE_Reactor*)0);
79 // Reset the reactor in the event handler, since it is gone.p
80 v->reactor(0);
82 ACE_Event_Handler::Reference_Count pos_release_count =
83 v->add_reference();
85 // Only our explicit calls to add_reference() should be reflected in
86 // the refence_count...
87 if (pos_release_count != pre_notify_count + 2)
89 result = -1;
90 ACE_ERROR((LM_ERROR,
91 ACE_TEXT("Reference count should have increased by 2.")
92 ACE_TEXT(" Initial count=%d, final count = %d\n"),
93 pre_notify_count, pos_release_count));
96 ACE_DEBUG ((LM_INFO,
97 ACE_TEXT("Ref count results. pre_notify refcount=%d,")
98 ACE_TEXT(" pos_notify=%d, pos_delete=%d\n"),
99 pre_notify_count, pos_notify_count, pos_release_count));
101 // Remove a reference for each time we explicitly increased it.
102 v->remove_reference();
103 v->remove_reference();
104 ACE_Event_Handler::Reference_Count pos_remove_count =
105 v->remove_reference();
107 ACE_DEBUG ((LM_INFO,
108 ACE_TEXT("Ref count results. pre_notify refcount=%d,")
109 ACE_TEXT(" pos_notify=%d, pos_delete=%d, pos_remove=%d\n"),
110 pre_notify_count, pos_notify_count, pos_release_count,
111 pos_remove_count));
113 ACE_END_TEST;
115 return result;
118 // ============================================
120 Simple_Handler::
121 Simple_Handler(
122 ACE_Reactor * r)
123 : ACE_Event_Handler(r)
125 reference_counting_policy().value(
126 ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
129 Simple_Handler::
130 ~Simple_Handler()
134 int Simple_Handler::
135 handle_exception(ACE_HANDLE)
137 return 0;