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 //=============================================================================
15 #include "test_config.h"
16 #include "randomize.h"
17 #include "ace/Lib_Find.h"
18 #include "ace/SString.h"
19 #include "ace/Naming_Context.h"
20 #include "ace/Profile_Timer.h"
21 #include "ace/OS_NS_stdio.h"
22 #include "ace/OS_NS_string.h"
23 #include "ace/OS_NS_unistd.h"
26 #if !defined ACE_LACKS_FCNTL || defined ACE_WIN32
28 static char name
[BUFSIZ
];
29 static char value
[BUFSIZ
];
30 static char type
[BUFSIZ
];
33 initialize_array (int * array
, int size
)
35 for (int n
= 0; n
< size
; ++n
)
40 print_time (ACE_Profile_Timer
&timer
,
43 ACE_Profile_Timer::ACE_Elapsed_Time et
;
45 timer
.elapsed_time (et
);
47 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT (" ***** %C ***** \n"), test
));
49 ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
50 et
.real_time
, et
.user_time
, et
.system_time
));
51 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("time per call = %f usecs\n"),
52 (et
.real_time
/ double (ACE_NS_MAX_ENTRIES
)) * 1000000));
56 test_bind (ACE_Naming_Context
&ns_context
)
58 int array
[ACE_NS_MAX_ENTRIES
];
60 initialize_array (array
, sizeof (array
) / sizeof (array
[0]));
61 randomize (array
, sizeof (array
) / sizeof (array
[0]));
64 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
66 ACE_OS::snprintf (name
, BUFSIZ
, "%s%d", "name", array
[i
]);
67 ACE_NS_WString
w_name (name
);
69 ACE_OS::snprintf (value
, BUFSIZ
, "%s%d", "value", array
[i
]);
70 ACE_NS_WString
w_value (value
);
72 ACE_OS::snprintf (type
, BUFSIZ
, "%s%d", "type", array
[i
]);
73 int bind_result
= ns_context
.bind (w_name
, w_value
, type
);
74 ACE_TEST_ASSERT (bind_result
!= -1);
79 test_find_failure (ACE_Naming_Context
&ns_context
)
81 ACE_OS::snprintf (name
, BUFSIZ
, "%s", "foo-bar");
82 ACE_NS_WString
w_name (name
);
83 ACE_NS_WString w_value
;
87 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
89 int resolve
= ns_context
.resolve (w_name
, w_value
, l_type
);
90 ACE_TEST_ASSERT (resolve
== -1);
95 test_rebind (ACE_Naming_Context
&ns_context
)
97 int array
[ACE_NS_MAX_ENTRIES
];
99 initialize_array (array
, sizeof (array
) / sizeof (array
[0]));
100 randomize (array
, sizeof (array
) / sizeof (array
[0]));
103 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
105 ACE_OS::snprintf (name
, BUFSIZ
, "%s%d", "name", array
[i
]);
106 ACE_NS_WString
w_name (name
);
108 ACE_OS::snprintf (value
, BUFSIZ
, "%s%d", "value", -array
[i
]);
109 ACE_NS_WString
w_value (value
);
111 ACE_OS::snprintf (type
, BUFSIZ
, "%s%d", "type", -array
[i
]);
112 int rebind
= ns_context
.rebind (w_name
, w_value
, type
);
113 ACE_TEST_ASSERT (rebind
!= -1);
118 test_unbind (ACE_Naming_Context
&ns_context
)
120 int array
[ACE_NS_MAX_ENTRIES
];
122 initialize_array (array
, sizeof (array
) / sizeof (array
[0]));
123 randomize (array
, sizeof (array
) / sizeof (array
[0]));
126 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
128 ACE_OS::snprintf (name
, BUFSIZ
, "%s%d", "name", array
[i
]);
129 ACE_NS_WString
w_name (name
);
130 int unbind
= ns_context
.unbind (w_name
);
131 ACE_TEST_ASSERT (unbind
!= -1);
136 test_find (ACE_Naming_Context
&ns_context
, int sign
, int result
)
138 char temp_val
[BUFSIZ
];
139 char temp_type
[BUFSIZ
];
141 int array
[ACE_NS_MAX_ENTRIES
];
143 initialize_array (array
, sizeof (array
) / sizeof (array
[0]));
144 randomize (array
, sizeof (array
) / sizeof (array
[0]));
147 for (size_t i
= 0; i
< ACE_NS_MAX_ENTRIES
; i
++)
151 ACE_OS::snprintf (temp_val
, BUFSIZ
, "%s%d", "value", array
[i
]);
152 ACE_OS::snprintf (temp_type
, BUFSIZ
, "%s%d", "type", array
[i
]);
156 ACE_OS::snprintf (temp_val
, BUFSIZ
, "%s%d", "value", -array
[i
]);
157 ACE_OS::snprintf (temp_type
, BUFSIZ
, "%s%d", "type", -array
[i
]);
160 ACE_OS::snprintf (name
, BUFSIZ
, "%s%d", "name", array
[i
]);
162 ACE_NS_WString
w_name (name
);
163 ACE_NS_WString w_value
;
165 ACE_NS_WString
val (temp_val
);
167 int const resolve_result
= ns_context
.resolve (w_name
, w_value
, type_out
);
168 if (resolve_result
!= result
)
169 ACE_ERROR ((LM_ERROR
,
170 ACE_TEXT ("Error, resolve result not equal to resutlt (%d != %d)\n"),
171 resolve_result
, result
));
173 char *l_value
= w_value
.char_rep ();
177 ACE_TEST_ASSERT (w_value
== val
);
178 if (ns_context
.name_options ()->debug ())
181 ACE_DEBUG ((LM_DEBUG
,
182 ACE_TEXT ("Name: %C\tValue: %C\tType: %C\n"),
183 name
, l_value
, type_out
));
185 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Name: %C\tValue: %C\n"),
191 ACE_TEST_ASSERT (ACE_OS::strcmp (type_out
, temp_type
) == 0);
202 run_main (int argc
, ACE_TCHAR
*argv
[])
204 ACE_START_TEST (ACE_TEXT ("Naming_Test"));
205 #if defined ACE_LACKS_FCNTL && !defined ACE_WIN32
206 ACE_UNUSED_ARG (argc
);
207 ACE_UNUSED_ARG (argv
);
209 ACE_TCHAR temp_file
[BUFSIZ
];
210 ACE_Naming_Context
*ns_context
= 0;
211 ACE_NEW_RETURN (ns_context
, ACE_Naming_Context
, -1);
213 ACE_Name_Options
*name_options
= ns_context
->name_options ();
215 name_options
->parse_args (argc
, argv
);
217 ** NOTE! This is an experimental value and is not magic in any way. It
218 ** works for me, on one system. It's needed because in the particular
219 ** case here where the underlying mmap will allocate a small area and
220 ** then try to grow it, it always moves it to a new location, which
221 ** totally screws things up. I once tried forcing the realloc to do
222 ** MAP_FIXED but that's not a good solution since it may overwrite other
223 ** mapped areas of memory, like the heap, or the C library, and get very
224 ** unexpected results. (Steve Huston, 24-August-2007)
226 # if defined (ACE_LINUX) && defined (__x86_64__)
227 name_options
->base_address ((char*)0x3c00000000);
229 bool unicode
= false;
230 # if (defined (ACE_WIN32) && defined (ACE_USES_WCHAR))
232 # endif /* ACE_WIN32 && ACE_USES_WCHAR */
233 if (unicode
&& name_options
->use_registry () == 1)
235 name_options
->namespace_dir (ACE_TEXT ("Software\\ACE\\Name Service"));
236 name_options
->database (ACE_TEXT ("Version 1"));
240 const ACE_TCHAR
* pname
= ACE::basename (name_options
->process_name (),
241 ACE_DIRECTORY_SEPARATOR_CHAR
);
242 // Allow the user to determine where the context file will be
243 // located just in case the current directory is not suitable for
244 // locking. We don't just set namespace_dir () on name_options
245 // because that is not sufficient to work around locking problems
246 // for Tru64 when the current directory is NFS mounted from a
247 // system that does not properly support locking.
248 ACE_TCHAR temp_dir
[MAXPATHLEN
];
249 if (ACE::get_temp_dir (temp_dir
, MAXPATHLEN
) == -1)
251 ACE_ERROR_RETURN ((LM_ERROR
,
252 ACE_TEXT ("Temporary path too long, ")
253 ACE_TEXT ("defaulting to current directory\n")),
258 ACE_OS::chdir (temp_dir
);
260 // Set the database name using the pid. mktemp isn't always available.
261 ACE_OS::snprintf(temp_file
, BUFSIZ
,
262 #if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
268 (int)(ACE_OS::getpid ()));
270 name_options
->database (temp_file
);
272 if (ns_context
->open (ACE_Naming_Context::PROC_LOCAL
, 1) == -1)
274 ACE_ERROR_RETURN ((LM_ERROR
,
275 ACE_TEXT ("ns_context->open (PROC_LOCAL) %p\n"),
276 ACE_TEXT ("failed")),
279 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("time to test %d iterations using %s\n"),
280 ACE_NS_MAX_ENTRIES
, name_options
->use_registry () ?
281 ACE_TEXT ("Registry") : ACE_TEXT ("ACE")));
283 ACE_Profile_Timer timer
;
286 // Add some bindings to the database
287 test_bind (*ns_context
);
288 print_time (timer
, "Binds");
291 // Should find the entries
292 test_find (*ns_context
, 1, 0);
293 print_time (timer
, "Successful Finds");
296 // Rebind with negative values
297 test_rebind (*ns_context
);
298 print_time (timer
, "Rebinds");
301 // Should find the entries
302 test_find (*ns_context
, -1, 0);
303 print_time (timer
, "Successful Finds");
306 // Should not find the entries
307 test_find_failure (*ns_context
);
308 print_time (timer
, "UnSuccessful Finds");
311 // Remove all bindings from database
312 test_unbind (*ns_context
);
313 print_time (timer
, "Unbinds");
315 ACE_OS::snprintf (temp_file
, BUFSIZ
, ACE_TEXT ("%s%s%s"),
316 name_options
->namespace_dir (),
317 ACE_DIRECTORY_SEPARATOR_STR
,
318 name_options
->database ());
322 // Remove any existing files. No need to check return value here
323 // since we don't care if the file doesn't exist.
324 ACE_OS::unlink (temp_file
);
326 #endif // !defined ACE_LACKS_FCNTL || defined ACE_WIN32