Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / tests / Bug_2820_Regression_Test.cpp
blobca504860ec0d1e92f3e1a820d560e04c70420ff7
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 <memory>
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() override;
35 /// Receive (and ignore) the notifications
36 int handle_exception(ACE_HANDLE) override;
39 int
40 run_main (int, ACE_TCHAR *[])
42 ACE_START_TEST (ACE_TEXT ("Bug_2820_Regression_Test"));
44 int result = 0;
46 std::unique_ptr<ACE_Reactor> reactor(new ACE_Reactor(new ACE_Select_Reactor, 1));
47 ACE_Event_Handler_var v =
48 ACE::make_event_handler<Simple_Handler> (reactor.get());
50 ACE_Event_Handler::Reference_Count pre_notify_count =
51 v->add_reference();
53 int const notify_count = 4;
54 for(int i = 0; i != notify_count; ++i)
56 reactor->notify(v.handler());
59 ACE_Event_Handler::Reference_Count pos_notify_count =
60 v->add_reference();
62 if(pos_notify_count != pre_notify_count + notify_count + 1)
64 result = -1;
65 ACE_ERROR((LM_ERROR,
66 ACE_TEXT("Reference count should increase by %d.")
67 ACE_TEXT(" Initial count=%d, final count = %d\n"),
68 notify_count, pre_notify_count, pos_notify_count));
71 reactor.reset ();
73 // Reset the reactor in the event handler, since it is gone.p
74 v->reactor(0);
76 ACE_Event_Handler::Reference_Count pos_release_count =
77 v->add_reference();
79 // Only our explicit calls to add_reference() should be reflected in
80 // the refence_count...
81 if (pos_release_count != pre_notify_count + 2)
83 result = -1;
84 ACE_ERROR((LM_ERROR,
85 ACE_TEXT("Reference count should have increased by 2.")
86 ACE_TEXT(" Initial count=%d, final count = %d\n"),
87 pre_notify_count, pos_release_count));
90 ACE_DEBUG ((LM_INFO,
91 ACE_TEXT("Ref count results. pre_notify refcount=%d,")
92 ACE_TEXT(" pos_notify=%d, pos_delete=%d\n"),
93 pre_notify_count, pos_notify_count, pos_release_count));
95 // Remove a reference for each time we explicitly increased it.
96 v->remove_reference();
97 v->remove_reference();
98 ACE_Event_Handler::Reference_Count pos_remove_count =
99 v->remove_reference();
101 ACE_DEBUG ((LM_INFO,
102 ACE_TEXT("Ref count results. pre_notify refcount=%d,")
103 ACE_TEXT(" pos_notify=%d, pos_delete=%d, pos_remove=%d\n"),
104 pre_notify_count, pos_notify_count, pos_release_count,
105 pos_remove_count));
107 ACE_END_TEST;
109 return result;
112 // ============================================
114 Simple_Handler::
115 Simple_Handler(
116 ACE_Reactor * r)
117 : ACE_Event_Handler(r)
119 reference_counting_policy().value(
120 ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
123 Simple_Handler::
124 ~Simple_Handler()
128 int Simple_Handler::
129 handle_exception(ACE_HANDLE)
131 return 0;