2 //=============================================================================
4 * @file Naming_Test.cpp
6 * This is a test to illustrate the Naming Services. The test
7 * does binds, rebinds, finds, and unbinds on name bindings using
8 * the local naming context.
10 * @author Prashant Jain <pjain@cs.wustl.edu> and Irfan Pyarali <irfan@cs.wustl.edu>
12 //=============================================================================
14 #include "test_config.h"
15 #include "randomize.h"
16 #include "ace/Lib_Find.h"
17 #include "ace/SString.h"
18 #include "ace/Naming_Context.h"
19 #include "ace/Profile_Timer.h"
20 #include "ace/OS_NS_stdio.h"
21 #include "ace/OS_NS_string.h"
22 #include "ace/OS_NS_unistd.h"
24 #if !defined ACE_LACKS_FCNTL || defined ACE_WIN32
26 static char name
[BUFSIZ
];
27 static char value
[BUFSIZ
];
28 static char type
[BUFSIZ
];
31 initialize_array (int * array
, int size
)
33 for (int n
= 0; n
< size
; ++n
)
38 print_time (ACE_Profile_Timer
&timer
,
41 ACE_Profile_Timer::ACE_Elapsed_Time et
;
43 timer
.elapsed_time (et
);
45 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT (" ***** %C ***** \n"), test
));
47 ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
48 et
.real_time
, et
.user_time
, et
.system_time
));
49 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("time per call = %f usecs\n"),
50 (et
.real_time
/ double (ACE_NS_MAX_ENTRIES
)) * 1000000));
54 test_bind (ACE_Naming_Context
&ns_context
)
56 int array
[ACE_NS_MAX_ENTRIES
];
58 initialize_array (array
, sizeof (array
) / sizeof (array
[0]));
59 randomize (array
, sizeof (array
) / sizeof (array
[0]));
62 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
64 ACE_OS::snprintf (name
, BUFSIZ
, "%s%d", "name", array
[i
]);
65 ACE_NS_WString
w_name (name
);
67 ACE_OS::snprintf (value
, BUFSIZ
, "%s%d", "value", array
[i
]);
68 ACE_NS_WString
w_value (value
);
70 ACE_OS::snprintf (type
, BUFSIZ
, "%s%d", "type", array
[i
]);
71 int bind_result
= ns_context
.bind (w_name
, w_value
, type
);
72 ACE_TEST_ASSERT (bind_result
!= -1);
77 test_find_failure (ACE_Naming_Context
&ns_context
)
79 ACE_OS::snprintf (name
, BUFSIZ
, "%s", "foo-bar");
80 ACE_NS_WString
w_name (name
);
81 ACE_NS_WString w_value
;
85 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
87 int resolve
= ns_context
.resolve (w_name
, w_value
, l_type
);
88 ACE_TEST_ASSERT (resolve
== -1);
93 test_rebind (ACE_Naming_Context
&ns_context
)
95 int array
[ACE_NS_MAX_ENTRIES
];
97 initialize_array (array
, sizeof (array
) / sizeof (array
[0]));
98 randomize (array
, sizeof (array
) / sizeof (array
[0]));
101 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
103 ACE_OS::snprintf (name
, BUFSIZ
, "%s%d", "name", array
[i
]);
104 ACE_NS_WString
w_name (name
);
106 ACE_OS::snprintf (value
, BUFSIZ
, "%s%d", "value", -array
[i
]);
107 ACE_NS_WString
w_value (value
);
109 ACE_OS::snprintf (type
, BUFSIZ
, "%s%d", "type", -array
[i
]);
110 int rebind
= ns_context
.rebind (w_name
, w_value
, type
);
111 ACE_TEST_ASSERT (rebind
!= -1);
116 test_unbind (ACE_Naming_Context
&ns_context
)
118 int array
[ACE_NS_MAX_ENTRIES
];
120 initialize_array (array
, sizeof (array
) / sizeof (array
[0]));
121 randomize (array
, sizeof (array
) / sizeof (array
[0]));
124 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
126 ACE_OS::snprintf (name
, BUFSIZ
, "%s%d", "name", array
[i
]);
127 ACE_NS_WString
w_name (name
);
128 int unbind
= ns_context
.unbind (w_name
);
129 ACE_TEST_ASSERT (unbind
!= -1);
134 test_find (ACE_Naming_Context
&ns_context
, int sign
, int result
)
136 char temp_val
[BUFSIZ
];
137 char temp_type
[BUFSIZ
];
139 int array
[ACE_NS_MAX_ENTRIES
];
141 initialize_array (array
, sizeof (array
) / sizeof (array
[0]));
142 randomize (array
, sizeof (array
) / sizeof (array
[0]));
145 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
149 ACE_OS::snprintf (temp_val
, BUFSIZ
, "%s%d", "value", array
[i
]);
150 ACE_OS::snprintf (temp_type
, BUFSIZ
, "%s%d", "type", array
[i
]);
154 ACE_OS::snprintf (temp_val
, BUFSIZ
, "%s%d", "value", -array
[i
]);
155 ACE_OS::snprintf (temp_type
, BUFSIZ
, "%s%d", "type", -array
[i
]);
158 ACE_OS::snprintf (name
, BUFSIZ
, "%s%d", "name", array
[i
]);
160 ACE_NS_WString
w_name (name
);
161 ACE_NS_WString w_value
;
163 ACE_NS_WString
val (temp_val
);
165 int const resolve_result
= ns_context
.resolve (w_name
, w_value
, type_out
);
166 if (resolve_result
!= result
)
167 ACE_ERROR ((LM_ERROR
,
168 ACE_TEXT ("Error, resolve result not equal to resutlt (%d != %d)\n"),
169 resolve_result
, result
));
171 char *l_value
= w_value
.char_rep ();
175 ACE_TEST_ASSERT (w_value
== val
);
176 if (ns_context
.name_options ()->debug ())
179 ACE_DEBUG ((LM_DEBUG
,
180 ACE_TEXT ("Name: %C\tValue: %C\tType: %C\n"),
181 name
, l_value
, type_out
));
183 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Name: %C\tValue: %C\n"),
189 ACE_TEST_ASSERT (ACE_OS::strcmp (type_out
, temp_type
) == 0);
200 run_main (int argc
, ACE_TCHAR
*argv
[])
202 ACE_START_TEST (ACE_TEXT ("Naming_Test"));
203 #if defined ACE_LACKS_FCNTL && !defined ACE_WIN32
204 ACE_UNUSED_ARG (argc
);
205 ACE_UNUSED_ARG (argv
);
207 ACE_TCHAR temp_file
[BUFSIZ
];
208 ACE_Naming_Context
*ns_context
= 0;
209 ACE_NEW_RETURN (ns_context
, ACE_Naming_Context
, -1);
211 ACE_Name_Options
*name_options
= ns_context
->name_options ();
213 name_options
->parse_args (argc
, argv
);
215 ** NOTE! This is an experimental value and is not magic in any way. It
216 ** works for me, on one system. It's needed because in the particular
217 ** case here where the underlying mmap will allocate a small area and
218 ** then try to grow it, it always moves it to a new location, which
219 ** totally screws things up. I once tried forcing the realloc to do
220 ** MAP_FIXED but that's not a good solution since it may overwrite other
221 ** mapped areas of memory, like the heap, or the C library, and get very
222 ** unexpected results. (Steve Huston, 24-August-2007)
224 # if defined (ACE_LINUX) && defined (__x86_64__)
225 name_options
->base_address ((char*)0x3c00000000);
227 bool unicode
= false;
228 # if (defined (ACE_WIN32) && defined (ACE_USES_WCHAR))
230 # endif /* ACE_WIN32 && ACE_USES_WCHAR */
231 if (unicode
&& name_options
->use_registry () == 1)
233 name_options
->namespace_dir (ACE_TEXT ("Software\\ACE\\Name Service"));
234 name_options
->database (ACE_TEXT ("Version 1"));
238 const ACE_TCHAR
* pname
= ACE::basename (name_options
->process_name (),
239 ACE_DIRECTORY_SEPARATOR_CHAR
);
240 // Allow the user to determine where the context file will be
241 // located just in case the current directory is not suitable for
242 // locking. We don't just set namespace_dir () on name_options
243 // because that is not sufficient to work around locking problems
244 // for Tru64 when the current directory is NFS mounted from a
245 // system that does not properly support locking.
246 ACE_TCHAR temp_dir
[MAXPATHLEN
];
247 if (ACE::get_temp_dir (temp_dir
, MAXPATHLEN
) == -1)
249 ACE_ERROR_RETURN ((LM_ERROR
,
250 ACE_TEXT ("Temporary path too long, ")
251 ACE_TEXT ("defaulting to current directory\n")),
256 ACE_OS::chdir (temp_dir
);
258 // Set the database name using the pid. mktemp isn't always available.
259 ACE_OS::snprintf(temp_file
, BUFSIZ
,
260 ACE_TEXT ("%") ACE_TEXT_PRIs
ACE_TEXT ("%d"),
262 (int)ACE_OS::getpid ());
264 name_options
->database (temp_file
);
266 if (ns_context
->open (ACE_Naming_Context::PROC_LOCAL
, 1) == -1)
268 ACE_ERROR_RETURN ((LM_ERROR
,
269 ACE_TEXT ("ns_context->open (PROC_LOCAL) %p\n"),
270 ACE_TEXT ("failed")),
273 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("time to test %d iterations using %s\n"),
274 ACE_NS_MAX_ENTRIES
, name_options
->use_registry () ?
275 ACE_TEXT ("Registry") : ACE_TEXT ("ACE")));
277 ACE_Profile_Timer timer
;
280 // Add some bindings to the database
281 test_bind (*ns_context
);
282 print_time (timer
, "Binds");
285 // Should find the entries
286 test_find (*ns_context
, 1, 0);
287 print_time (timer
, "Successful Finds");
290 // Rebind with negative values
291 test_rebind (*ns_context
);
292 print_time (timer
, "Rebinds");
295 // Should find the entries
296 test_find (*ns_context
, -1, 0);
297 print_time (timer
, "Successful Finds");
300 // Should not find the entries
301 test_find_failure (*ns_context
);
302 print_time (timer
, "UnSuccessful Finds");
305 // Remove all bindings from database
306 test_unbind (*ns_context
);
307 print_time (timer
, "Unbinds");
309 ACE_OS::snprintf (temp_file
, BUFSIZ
, ACE_TEXT ("%s%s%s"),
310 name_options
->namespace_dir (),
311 ACE_DIRECTORY_SEPARATOR_STR
,
312 name_options
->database ());
316 // Remove any existing files. No need to check return value here
317 // since we don't care if the file doesn't exist.
318 ACE_OS::unlink (temp_file
);
320 #endif // !defined ACE_LACKS_FCNTL || defined ACE_WIN32