1 // ============================================================================
7 // This test asserts that close is called on Modules during
8 // ACE_Service_Repository shutdown
11 // Chad Beaulac <chad@objectivesolutions.com>
13 // ============================================================================
15 #include "test_config.h"
17 #include "ace/Log_Msg.h"
18 #include "ace/Service_Config.h"
19 #include "ace/Service_Repository.h"
20 #include "ace/Service_Object.h"
21 #include "ace/Service_Types.h"
23 #include "ace/Module.h"
25 using MT_Task
= ACE_Task
<ACE_MT_SYNCH
>;
26 using MT_Module
= ACE_Module
<ACE_MT_SYNCH
>;
29 * We use this Task to track if close was called.
31 class Close_Handler
: public virtual MT_Task
34 Close_Handler(bool* close_called_arg
)
35 : close_called_ (close_called_arg
)
39 int close(u_long
) override
;
46 int Close_Handler::close(u_long
)
48 ACE_DEBUG ((LM_DEBUG
,"Close_Handler::close \n"));
49 *close_called_
= true;
54 run_test (int argc
, ACE_TCHAR
*argv
[])
57 bool close_called
= false;
58 Close_Handler
* close_handler
= 0;
59 ACE_NEW_RETURN(close_handler
, Close_Handler (&close_called
), -1);
61 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Opening service config\n")));
63 status
= ACE_Service_Config::open (argc
,
65 ACE_DEFAULT_LOGGER_KEY
,
67 true /*ignore def svc.conf*/);
70 ACE_ERROR_RETURN ((LM_ERROR
,
71 ACE_TEXT ("run_test, %p\n")
72 ACE_TEXT ("ACE_Service_Config::open")),
76 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Service config opened\n")));
79 ACE_Service_Repository
*asr
= ACE_Service_Repository::instance ();
81 ACE_ERROR_RETURN ((LM_ERROR
,
82 ACE_TEXT ("run_test, no service repository\n")),
85 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Finding close test module\n")));
87 const ACE_Service_Type
* st
= 0;
88 status
= asr
->find (ACE_TEXT ("Close_Test_Module"), &st
);
90 ACE_ERROR_RETURN ((LM_ERROR
,
91 ACE_TEXT ("run_test, %p on Close_Test_Module\n")
92 ACE_TEXT ("ACE_Service_Repository::find")),
96 // Put our Close_Handler Task into the Module
98 MT_Module
* close_test_module
=
99 static_cast <MT_Module
*> (st
->type()->object ());
101 if (close_test_module
== 0)
102 ACE_ERROR_RETURN ((LM_ERROR
,
103 ACE_TEXT ("run_test, no close test module\n")),
105 close_test_module
->reader (close_handler
);
108 // Remove the Module from the Stream.
109 // This is what happens during ACE_Service_Repository::fini
110 // We want to make sure that close is called on Modules and their Tasks
111 // at this time to ensure proper cleanup during the shutdown sequence
112 // by getting the close methods called so user shutdown hooks can fire.
115 const ACE_Module_Type
* module_type
=
116 static_cast< const ACE_Module_Type
*>(st
->type ());
118 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Finding close test stream\n")));
120 status
= asr
->find (ACE_TEXT ("Close_Test_Stream"), &st
);
123 ACE_ERROR_RETURN ((LM_ERROR
,
124 ACE_TEXT ("run_test, %p on Close_Test_Stream\n")
125 ACE_TEXT ("ACE_Service_Repository::find")),
128 const ACE_Stream_Type
* close_test_stream
=
129 static_cast<const ACE_Stream_Type
*> (st
->type ());
131 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Removing module\n")));
133 ACE_Stream_Type
*nc_stream
= const_cast<ACE_Stream_Type
*>(close_test_stream
);
134 ACE_Module_Type
*nc_module
= const_cast<ACE_Module_Type
*>(module_type
);
135 nc_stream
->remove (nc_module
);
139 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("close not called\n")));
144 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT (" SUCCESS: close called\n")));
150 run_main(int, ACE_TCHAR
*argv
[])
152 ACE_START_TEST (ACE_TEXT ("Bug_3912_Regression_Test"));
154 ACE_TCHAR conf_file_name
[MAXPATHLEN
];
155 #if defined (TEST_DIR)
156 ACE_OS::strcpy (conf_file_name
, TEST_DIR
);
157 ACE_OS::strcat (conf_file_name
, ACE_DIRECTORY_SEPARATOR_STR
);
158 ACE_OS::strcat (conf_file_name
, ACE_TEXT ("Bug_3912_Regression_Test.conf"));
160 ACE_OS::strcpy (conf_file_name
, ACE_TEXT ("Bug_3912_Regression_Test.conf"));
163 ACE_TCHAR
* _argv
[3] = {argv
[0],
164 const_cast<ACE_TCHAR
*> (ACE_TEXT ("-f")),
165 const_cast<ACE_TCHAR
*> (conf_file_name
)};
167 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Starting test\n")));
169 int status
= run_test (3,_argv
);
171 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Closing service config\n")));
173 ACE_Service_Config::fini_svcs ();