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 typedef ACE_Task
<ACE_SYNCH
> MT_Task
;
26 typedef ACE_Module
<ACE_SYNCH
> MT_Module
;
29 * We use this Task to track if close was called.
31 class Close_Handler
: public virtual MT_Task
35 Close_Handler(bool* close_called_arg
)
36 : close_called_ (close_called_arg
)
40 virtual int close(u_long
);
47 int Close_Handler::close(u_long
)
49 ACE_DEBUG ((LM_DEBUG
,"Close_Handler::close \n"));
50 *close_called_
= true;
55 run_test (int argc
, ACE_TCHAR
*argv
[])
58 bool close_called
= false;
59 Close_Handler
* close_handler
= 0;
60 ACE_NEW_RETURN(close_handler
, Close_Handler (&close_called
), -1);
62 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Opening service config\n")));
64 status
= ACE_Service_Config::open (argc
,
66 ACE_DEFAULT_LOGGER_KEY
,
68 true /*ignore def svc.conf*/);
71 ACE_ERROR_RETURN ((LM_ERROR
,
72 ACE_TEXT ("run_test, %p\n")
73 ACE_TEXT ("ACE_Service_Config::open")),
77 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Service config opened\n")));
80 ACE_Service_Repository
*asr
= ACE_Service_Repository::instance ();
82 ACE_ERROR_RETURN ((LM_ERROR
,
83 ACE_TEXT ("run_test, no service repository\n")),
86 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Finding close test module\n")));
88 const ACE_Service_Type
* st
= 0;
89 status
= asr
->find (ACE_TEXT ("Close_Test_Module"), &st
);
91 ACE_ERROR_RETURN ((LM_ERROR
,
92 ACE_TEXT ("run_test, %p on Close_Test_Module\n")
93 ACE_TEXT ("ACE_Service_Repository::find")),
97 // Put our Close_Handler Task into the Module
99 MT_Module
* close_test_module
=
100 static_cast <MT_Module
*> (st
->type()->object ());
102 if (close_test_module
== 0)
103 ACE_ERROR_RETURN ((LM_ERROR
,
104 ACE_TEXT ("run_test, no close test module\n")),
106 close_test_module
->reader (close_handler
);
109 // Remove the Module from the Stream.
110 // This is what happens during ACE_Service_Repository::fini
111 // We want to make sure that close is called on Modules and their Tasks
112 // at this time to ensure proper cleanup during the shutdown sequence
113 // by getting the close methods called so user shutdown hooks can fire.
116 const ACE_Module_Type
* module_type
=
117 static_cast< const ACE_Module_Type
*>(st
->type ());
119 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Finding close test stream\n")));
121 status
= asr
->find (ACE_TEXT ("Close_Test_Stream"), &st
);
124 ACE_ERROR_RETURN ((LM_ERROR
,
125 ACE_TEXT ("run_test, %p on Close_Test_Stream\n")
126 ACE_TEXT ("ACE_Service_Repository::find")),
129 const ACE_Stream_Type
* close_test_stream
=
130 static_cast<const ACE_Stream_Type
*> (st
->type ());
132 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Removing module\n")));
134 ACE_Stream_Type
*nc_stream
= const_cast<ACE_Stream_Type
*>(close_test_stream
);
135 ACE_Module_Type
*nc_module
= const_cast<ACE_Module_Type
*>(module_type
);
136 nc_stream
->remove (nc_module
);
140 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("close not called\n")));
145 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT (" SUCCESS: close called\n")));
151 run_main(int, ACE_TCHAR
*argv
[])
153 ACE_START_TEST (ACE_TEXT ("Bug_3912_Regression_Test"));
155 ACE_TCHAR conf_file_name
[MAXPATHLEN
];
156 #if defined (TEST_DIR)
157 ACE_OS::strcpy (conf_file_name
, TEST_DIR
);
158 ACE_OS::strcat (conf_file_name
, ACE_DIRECTORY_SEPARATOR_STR
);
159 ACE_OS::strcat (conf_file_name
, ACE_TEXT ("Bug_3912_Regression_Test.conf"));
161 ACE_OS::strcpy (conf_file_name
, ACE_TEXT ("Bug_3912_Regression_Test.conf"));
164 ACE_TCHAR
* _argv
[3] = {argv
[0],
165 const_cast<ACE_TCHAR
*> (ACE_TEXT ("-f")),
166 const_cast<ACE_TCHAR
*> (conf_file_name
)};
168 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Starting test\n")));
170 int status
= run_test (3,_argv
);
172 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Closing service config\n")));
174 ACE_Service_Config::fini_svcs ();