Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Framework_Component_Test.cpp
blob407d225bd155852f2ea9f5ef1d8458ff3a6acea9
1 // ============================================================================
2 //
3 // = LIBRARY
4 // tests
5 //
6 // = DESCRIPTION
7 // This program tests both the ACE_Framework_Compondent and ACE_Repository.
8 // Since Framework Components are singletons that can live in dlls loaded
9 // via the Service Configurator framework, this test uses that framework
10 // to load services from a dll that has a singleton based on ACE_DLL_Singleton.
11 // When the dll is finally ready to be unloaded, the singleton will be
12 // automatically cleaned up just-in-time.
14 // = AUTHOR
15 // Don Hinton <dhinton@ieee.org>
17 // ============================================================================
19 #include "test_config.h"
20 #include "ace/Service_Config.h"
21 #include "ace/ARGV.h"
22 #include "ace/DLL_Manager.h"
26 // Define a few macros--because they're so much fun, and keep the
27 // code below a little cleaner...
28 #if (ACE_USES_CLASSIC_SVC_CONF == 1)
30 # define ADD_SERVICE(X) \
31 ACE_TEXT ("dynamic Server_") ACE_TEXT(#X) \
32 ACE_TEXT (" Service_Object * ") \
33 ACE_TEXT ("Framework_Component_DLL:_make_Server_") ACE_TEXT(#X) \
34 ACE_TEXT ("() ''")
36 # define REMOVE_SERVICE(X) \
37 ACE_TEXT ("remove Server_") ACE_TEXT(#X)
39 #else /* ACE_USES_CLASSIC_SVC_CONF */
41 # define ADD_SERVICE(X) \
42 ACE_TEXT ("<?xml version='1.0'?> <dynamic id='Server_") ACE_TEXT(#X) ACE_TEXT("' ") \
43 ACE_TEXT ("type='service_object'> <initializer init='_make_Server_") ACE_TEXT(#X) ACE_TEXT("' ") \
44 ACE_TEXT ("path='Framework_Component_DLL' params=''/> </dynamic>")
46 # define REMOVE_SERVICE(X) \
47 ACE_TEXT ( "<?xml version='1.0'?> <remove id='Server_") ACE_TEXT(#X) \
48 ACE_TEXT("'> </remove>")
50 #endif /* ACE_USES_CLASSIC_SVC_CONF */
52 int
53 run_test (u_long unload_mask = 0)
55 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Running test with mask = %s|%s\n"),
56 ACE_BIT_ENABLED(unload_mask, ACE_DLL_UNLOAD_POLICY_PER_DLL) == 0
57 ? ACE_TEXT ("PER_PROCESS") : ACE_TEXT ("PER_DLL"),
58 ACE_BIT_ENABLED(unload_mask, ACE_DLL_UNLOAD_POLICY_LAZY) == 0
59 ? ACE_TEXT ("EAGER") : ACE_TEXT ("LAZY")));
61 ACE_DLL_Manager::instance ()->unload_policy (unload_mask);
63 // Now, let the ACE Service Configurator framework load our service from a
64 // dll, which contains a singleton.
65 ACE_Service_Config::open (ACE_TEXT ("Framework_Component_Test"),
66 ACE_DEFAULT_LOGGER_KEY,
67 1, 1, 1);
69 // Now add server 1.
70 ACE_Service_Config::process_directive (ADD_SERVICE(1));
72 // And unload the first one, could unload the dll.
73 ACE_Service_Config::process_directive (REMOVE_SERVICE(1));
75 // Now readd server 1.
76 ACE_Service_Config::process_directive (ADD_SERVICE(1));
78 // And load another service from the same library.
79 ACE_Service_Config::process_directive (ADD_SERVICE(2));
81 // Unload the first one again, should *not* unload the dll this time.
82 ACE_Service_Config::process_directive (REMOVE_SERVICE(1));
84 // And unload the second service. Since the ACE_DLL_Handle will no longer
85 // have any references, the ACE_DLL_Manager will apply it's current unloading
86 // strategy and either call ACE_OS::dlclose() immediately, schedule a timeout
87 // the the reactor to call dlclose() some time in the future, or keep the
88 // dll loaded until program termination.
89 ACE_Service_Config::process_directive (REMOVE_SERVICE(2));
91 // Force unloading so we'll be ready for the next test.
92 ACE_DLL_Manager::instance ()->unload_policy (ACE_DLL_UNLOAD_POLICY_DEFAULT);
94 ACE_Service_Config::close ();
95 return 0;
98 int
99 run_main (int, ACE_TCHAR *[])
101 ACE_START_TEST (ACE_TEXT("Framework_Component_Test"));
103 int retval = 0;
105 // Use defaults, i.e., per process, eager unloading.
106 retval += run_test (ACE_DLL_UNLOAD_POLICY_DEFAULT);
108 // Per process, lazy unloading
109 retval += run_test (ACE_DLL_UNLOAD_POLICY_LAZY);
111 // Per dll, eager unloading
112 retval += run_test (ACE_DLL_UNLOAD_POLICY_PER_DLL);
114 // Per dll, lazy unloading.
115 retval += run_test (ACE_DLL_UNLOAD_POLICY_PER_DLL | ACE_DLL_UNLOAD_POLICY_LAZY);
117 ACE_END_TEST;
118 return retval == 0 ? 0 : -1;