Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Bug_3912_Regression_Test.cpp
blob4624a585df1fdb3fbf1ad9e2b45aa52e53527a9b
1 // ============================================================================
2 //
3 // = LIBRARY
4 // tests
5 //
6 // = DESCRIPTION
7 // This test asserts that close is called on Modules during
8 // ACE_Service_Repository shutdown
9 //
10 // = AUTHOR
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"
22 #include "ace/Task.h"
23 #include "ace/Module.h"
25 typedef ACE_Task<ACE_SYNCH> MT_Task;
26 typedef ACE_Module<ACE_SYNCH> MT_Module;
28 /**
29 * We use this Task to track if close was called.
31 class Close_Handler : public virtual MT_Task
33 public:
35 Close_Handler(bool* close_called_arg)
36 : close_called_ (close_called_arg)
40 virtual int close(u_long );
42 private:
43 bool* close_called_;
47 int Close_Handler::close(u_long )
49 ACE_DEBUG ((LM_DEBUG,"Close_Handler::close \n"));
50 *close_called_ = true;
51 return 0;
54 int
55 run_test (int argc, ACE_TCHAR *argv[])
57 int status = 0;
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,
65 argv,
66 ACE_DEFAULT_LOGGER_KEY,
67 true,
68 true /*ignore def svc.conf*/);
69 if (status != 0)
71 ACE_ERROR_RETURN ((LM_ERROR,
72 ACE_TEXT ("run_test, %p\n")
73 ACE_TEXT ("ACE_Service_Config::open")),
74 status);
76 else
77 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Service config opened\n")));
80 ACE_Service_Repository *asr = ACE_Service_Repository::instance ();
81 if (asr == 0)
82 ACE_ERROR_RETURN ((LM_ERROR,
83 ACE_TEXT ("run_test, no service repository\n")),
84 -1);
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);
90 if (status != 0)
91 ACE_ERROR_RETURN ((LM_ERROR,
92 ACE_TEXT ("run_test, %p on Close_Test_Module\n")
93 ACE_TEXT ("ACE_Service_Repository::find")),
94 status);
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")),
105 -1);
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);
122 if (status != 0)
124 ACE_ERROR_RETURN ((LM_ERROR,
125 ACE_TEXT ("run_test, %p on Close_Test_Stream\n")
126 ACE_TEXT ("ACE_Service_Repository::find")),
127 status);
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);
138 if (!close_called)
140 ACE_ERROR ((LM_ERROR, ACE_TEXT ("close not called\n")));
141 ++status;
143 else
145 ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" SUCCESS: close called\n")));
147 return status;
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"));
160 #else
161 ACE_OS::strcpy (conf_file_name, ACE_TEXT ("Bug_3912_Regression_Test.conf"));
162 #endif
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 ();
176 ACE_END_TEST;
177 return status;