2 //=============================================================================
6 * This is the test class for the concurrency service. The class
7 * implements a client to the concurrency service.
8 * This file contains the main function for the test.
10 * @author Torben Worm <tworm@cs.wustl.edu>
12 //=============================================================================
15 #include "CC_client.h"
17 #include "tao/debug.h"
19 #include "ace/Read_Buffer.h"
20 #include "ace/OS_NS_stdio.h"
21 #include "ace/OS_NS_unistd.h"
22 #include "ace/OS_NS_ctype.h"
23 #include "ace/OS_NS_string.h"
24 #include "ace/OS_NS_fcntl.h"
28 CC_Client::CC_Client ()
29 : naming_service_ (0),
30 cc_factory_ior_file_ (0),
32 f_handle_ (ACE_INVALID_HANDLE
),
34 use_naming_service_ (1),
36 run_extended_tests_ (0),
42 CC_Client::~CC_Client ()
44 // Free resources and close the ior files.
45 if (this->cc_factory_ior_file_
)
46 ACE_OS::fclose (this->cc_factory_ior_file_
);
48 if (this->f_handle_
!= ACE_INVALID_HANDLE
)
49 ACE_OS::close (this->f_handle_
);
51 if (this->cc_factory_key_
!= 0)
52 ACE_OS::free (this->cc_factory_key_
);
54 if (naming_service_
!=0)
55 delete naming_service_
;
58 // Reads the lock set factory ior from a file
61 CC_Client::read_ior (ACE_TCHAR
*filename
)
63 // Open the file for reading.
64 this->f_handle_
= ACE_OS::open (filename
,0);
66 if (this->f_handle_
== ACE_INVALID_HANDLE
)
67 ACE_ERROR_RETURN ((LM_ERROR
,
68 "Unable to open %s for writing: %p\n",
71 ACE_Read_Buffer
ior_buffer (this->f_handle_
);
72 this->cc_factory_key_
= ior_buffer
.read ();
74 if (this->cc_factory_key_
== 0)
75 ACE_ERROR_RETURN ((LM_ERROR
,
76 "Unable to allocate memory to read ior: %p\n"),
81 // Parses the command line arguments and returns an error status.
84 CC_Client::parse_args ()
86 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("dc:sf:k:xbhe:"));
90 if(argc_
==1) // No arguments given on command line
96 while ((c
= get_opts ()) != -1)
99 case 'b': // debug flag
100 this->run_basic_tests_
= 1;
103 this->use_script_file_
= 1;
104 this->script_file_
= ACE_OS::strdup (get_opts
.opt_arg ());
106 case 'd': // debug flag
109 case 'e': // debug flag
110 run_extended_tests_
= 1;
111 this->extended_tests_params_
= ACE_OS::strdup (get_opts
.opt_arg ());
113 case 'f': // read the IOR from the file.
114 result
= this->read_ior (get_opts
.opt_arg ());
116 ACE_ERROR_RETURN ((LM_ERROR
,
117 "Unable to read ior from %s : %p\n",
118 get_opts
.opt_arg ()),
121 case 'k': // read the cubit IOR from the command-line.
122 this->cc_factory_key_
=
123 ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts
.opt_arg ()));
128 case 's': // Don't use the TAO Naming Service.
129 this->use_naming_service_
= 0;
134 ACE_ERROR_RETURN ((LM_ERROR
, ""), -1);
137 // Indicates successful parsing of command line.
141 // Execute client example code.
147 // Tells whether any tests have been run
149 int success
= CC_SUCCESS
;
152 if (this->run_basic_tests_
&& success
== CC_SUCCESS
)
154 success
= run_basic_tests ();
156 ACE_DEBUG((LM_DEBUG
, "Basic tests did not succeed\n"));
160 if (this->run_extended_tests_
&& success
== CC_SUCCESS
)
162 success
= run_extended_tests (this->extended_tests_params_
);
164 ACE_DEBUG((LM_DEBUG
, "Extended tests did not succeed\n"));
168 if(this->use_script_file_
&& success
== CC_SUCCESS
)
170 cmdlist
= new CC_CommandList();
173 // Open the command file for parsing if the filename!=stdin
174 if(ACE_OS::strcmp(this->script_file_
, ACE_TEXT("stdin"))!=0)
176 f
= ACE_OS::fopen(this->script_file_
, "r");
178 ACE_ERROR_RETURN ((LM_ERROR
,
179 "Unable to open %s\n",
187 // Other tests go here
188 // if (other_test_flag && success == CC_SUCCESS) ...
191 // @@TAO is this needed??
196 ACE_ERROR_RETURN ((LM_ERROR
,
204 // This function runs basic tests concerned with only one lock set
207 CC_Client::run_basic_tests ()
209 Test_Single_Lock_With_Mode
t1 (naming_service_
,
210 CosConcurrencyControl::read
);
211 Test_Single_Lock_With_Mode
t2 (naming_service_
,
212 CosConcurrencyControl::write
);
213 Test_Single_Lock_With_Mode
t3 (naming_service_
,
214 CosConcurrencyControl::upgrade
);
215 Test_Single_Lock_With_Mode
t4 (naming_service_
,
216 CosConcurrencyControl::intention_read
);
217 Test_Single_Lock_With_Mode
t5 (naming_service_
,
218 CosConcurrencyControl::intention_write
);
219 // This test should be run for several different lock mode, but
220 // since we do not support
221 Test_Release_Not_Held_Lock
t6 (naming_service_
,
222 CosConcurrencyControl::read
);
223 if (t1
.run () == CC_SUCCESS
&&
224 t2
.run () == CC_SUCCESS
&&
225 t3
.run () == CC_SUCCESS
&&
226 t4
.run () == CC_SUCCESS
&&
227 t5
.run () == CC_SUCCESS
&&
228 t6
.run () == CC_SUCCESS
)
235 CC_Client::check_extended_test_params(ACE_TCHAR
*params
)
237 // Format (regexp): [0-9]+;.*;.*
238 int no_of_params
= 0;
239 ACE_TCHAR
*cp
= params
; // pointer to walk along the string
240 enum {TAO_START
, TAO_NUMBER
, TAO_ARG
, TAO_ERROR
} state
= TAO_START
;
247 if(ACE_OS::ace_isdigit(*cp
))
260 if(!ACE_OS::ace_isdigit(*cp
))
276 ACE_ERROR_RETURN((LM_ERROR
,
277 "CC_Client::check_extended_test_params\n"), -1);
281 if (state
==TAO_ERROR
) // there was only one character given and it was wrong
288 CC_Client::run_extended_tests (ACE_TCHAR
*params
)
290 int success
= CC_FAIL
;
293 ACE_DEBUG ((LM_DEBUG
,
297 no_of_args
= check_extended_test_params(params
);
300 ACE_ERROR_RETURN((LM_ERROR
,
301 "Error in parameter string (%s). Format: '<test#>;<arg1>;<arg2>'\n", params
), CC_FAIL
);
304 ACE_DEBUG((LM_DEBUG
, "Number of arguments: %i\n", no_of_args
));
306 char *cmd
= ACE_OS::strtok (ACE_TEXT_ALWAYS_CHAR(params
), ";");
307 char *arg1
= ACE_OS::strtok (0, ";");
308 // char *arg2 = ACE_OS::strtok (0, ";");
310 // A possible scenario using test 1,2, and 3 Create and lock the
311 // lock set with the name 'Name'
312 // ./CC_client -e '1;Name'
313 // Try to lock the same lock set. The process will hang
314 // ./CC_client -e '2:Name'
315 // Unlocks the lock set. Now test 2 will continue.
316 // ./CC_client -e '3:Name'
318 if (ACE_OS::strcmp (cmd
, "1") == 0)
320 Test_Setup_LockSet
t1 (naming_service_
, arg1
);
324 if (ACE_OS::strcmp (cmd
, "2") == 0)
326 Test_Use_Already_Created_LockSet
t2 (naming_service_
, arg1
);
330 if (ACE_OS::strcmp (cmd
, "3") == 0)
332 Test_Unlock_Already_Created_LockSet
t3 (naming_service_
, arg1
);
340 CC_Client::print_usage ()
342 ACE_ERROR ((LM_ERROR
,
345 " [-c] cc-test-script"
347 " [-f cc_factory-obj-ref-key-file]"
348 " [-k cc-obj-ref-key]"
356 CC_Client::init_naming_service ()
360 ACE_NEW_RETURN (naming_service_
,
364 this->naming_service_
->Init (this->orb_
);
366 catch (const CORBA::Exception
&)
374 CC_Client::init (int argc
, ACE_TCHAR
**argv
)
383 this->orb_
= CORBA::ORB_init (this->argc_
,
387 // Parse command line and verify parameters.
388 if (this->parse_args () == -1)
391 if (this->use_naming_service_
)
393 naming_result
= this->init_naming_service ();
394 if (naming_result
< 0)
395 return naming_result
;
399 if (this->cc_factory_key_
== 0)
400 ACE_ERROR_RETURN ((LM_ERROR
,
401 "%s: no lock set factory key specified\n",
406 CORBA::Object_var factory_object
=
407 this->orb_
->string_to_object (this->cc_factory_key_
);
410 // The test cannot currently run without the naming service.
412 CosConcurrencyControl::LockSetFactory::_narrow
413 (factory_object
.in ());
415 if (CORBA::is_nil (this->factory_
.in ()))
416 ACE_ERROR_RETURN ((LM_ERROR
,
417 "invalid factory key <%s>\n",
418 this->cc_factory_key_
),
423 ACE_DEBUG ((LM_DEBUG
,
424 "Factory received OK\n"));
426 catch (const CORBA::Exception
& ex
)
428 ex
._tao_print_exception ("CC_Client::init");
435 // This function runs the test.
438 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
442 ACE_DEBUG ((LM_DEBUG
,
443 "\n \t CosConcurrencyControl: client \n\n"));
445 if (cc_client
.init (argc
, argv
) == -1)
447 ACE_DEBUG ((LM_DEBUG
,
448 "Did not initialize correctly\n"));
453 ACE_DEBUG ((LM_DEBUG
,
454 "Running the test\n"));
455 return cc_client
.run ();