2 #include "tao/Strategies/advanced_resource.h"
3 #include "tao/Messaging/Messaging.h"
4 #include "tao/AnyTypeCode/TAOA.h"
5 #include "tao/AnyTypeCode/Any.h"
7 #include "ace/streams.h"
8 #include "ace/High_Res_Timer.h"
9 #include "ace/Arg_Shifter.h"
13 using namespace CORBA
;
14 using namespace PortableServer
;
18 const ACE_TCHAR
*non_existent_ior
= ACE_TEXT("corbaloc:iiop:1.2@63.246.9.65:12345/test");
19 const int TIME_THRESHOLD
= 100; //ms
21 int request_timeout
= 0;
22 Messaging::SyncScope sync_scope
;
23 bool use_buf_constraints
= false;
24 bool use_sync_scope
= false;
25 int bc_mode
= TAO::BUFFER_FLUSH
;
29 int num_requests
= 10;
30 int request_interval
= 50;
31 int connect_timeout
= 0;
32 int run_orb_delay
= 0;
34 bool force_timeout
= false;
35 // This will force a blocking connection before starting the test
36 // by sending the num_requests as a twoway.
37 bool force_connect
= false;
38 bool use_sleep
= false;
39 unsigned int max_request_time
= 0;
40 bool use_twoway
= false;
41 bool retry_transients
= false;
42 bool retry_timeouts
= false;
43 bool make_request_queued
= false;
44 const ACE_TCHAR
*server_ior
= ACE_TEXT ("file://test.ior");
46 void print_usage (const ACE_TCHAR
*argv0
)
49 "%s [-request_timeout ms=0] "
50 "[-connect_timeout ms=0] "
51 "[-request_interval ms=100] "
52 "[-run_orb_delay ms=0] "
53 "[-run_orb_time ms=0] "
54 "[-max_request_time ms=0] "
55 "[-num_requests n=10] "
57 "[-retry_transients] "
63 "[-buffer_bytes n=0] "
64 "[-buffer_timeout ms=0] "
65 "[-sync delayed|none] "
66 "[-make_request_queued] "
67 "[-server_ior <ior>]\n",
71 bool parse_command_line (int ac
, ACE_TCHAR
*av
[])
73 ACE_Arg_Shifter
args (ac
, av
);
76 while (args
.is_anything_left ())
78 if (args
.cur_arg_strncasecmp (ACE_TEXT("-request_timeout")) == 0)
81 request_timeout
= ACE_OS::atoi (args
.get_current ());
84 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-connect_timeout")) == 0)
87 connect_timeout
= ACE_OS::atoi (args
.get_current ());
90 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-request_interval")) == 0)
93 request_interval
= ACE_OS::atoi (args
.get_current ());
96 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-run_orb_delay")) == 0)
99 run_orb_delay
= ACE_OS::atoi (args
.get_current ());
102 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-run_orb_time")) == 0)
105 run_orb_time
= ACE_OS::atoi(args
.get_current ());
108 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-max_request_time")) == 0)
111 max_request_time
= ACE_OS::atoi (args
.get_current ());
114 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-num_requests")) == 0)
117 num_requests
= ACE_OS::atoi (args
.get_current ());
120 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-use_twoway")) == 0)
125 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-retry_transients")) == 0)
127 retry_transients
= true;
130 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-retry_timeouts")) == 0)
132 retry_timeouts
= true;
135 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-use_sleep")) == 0)
140 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-force_timeout")) == 0)
142 force_timeout
= true;
145 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-force_connect")) == 0)
147 force_connect
= true;
150 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-buffer_count")) == 0)
153 use_buf_constraints
= true;
154 bc_count
= ACE_OS::atoi (args
.get_current ());
157 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-buffer_bytes")) == 0)
160 use_buf_constraints
= true;
161 bc_bytes
= ACE_OS::atoi (args
.get_current ());
164 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-buffer_timeout")) == 0)
167 use_buf_constraints
= true;
168 bc_timeout
= ACE_OS::atoi (args
.get_current ());
171 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-sync")) == 0)
174 if (args
.cur_arg_strncasecmp (ACE_TEXT("delayed")) == 0)
176 sync_scope
= TAO::SYNC_DELAYED_BUFFERING
;
177 use_sync_scope
= true;
179 else if (args
.cur_arg_strncasecmp (ACE_TEXT("none")) == 0)
181 sync_scope
= Messaging::SYNC_NONE
;
182 use_sync_scope
= true;
192 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-make_request_queued")) == 0)
194 make_request_queued
= true;
197 else if (args
.cur_arg_strncasecmp (ACE_TEXT("-server_ior")) == 0)
200 server_ior
= args
.get_current ();
205 ACE_ERROR ((LM_ERROR
, "Error: Unknown argument \"%s\"\n",
206 args
.get_current ()));
216 Tester_ptr
set_request_timeout (Tester_ptr tst
, ORB_ptr orb
)
218 if (request_timeout
<= 0)
220 return Tester::_duplicate (tst
);
224 a
<<= static_cast<TimeBase::TimeT
> (request_timeout
* 10000);
228 orb
->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE
, a
);
229 Object_var obj
= tst
->_set_policy_overrides (pols
, ADD_OVERRIDE
);
231 return Tester::_unchecked_narrow (obj
.in ());
235 void set_connect_timeout (ORB_ptr orb
)
237 if (connect_timeout
<= 0)
239 Object_var obj
= orb
->resolve_initial_references ("PolicyCurrent");
240 PolicyCurrent_var policy_current
= PolicyCurrent::_narrow (obj
.in ());
242 a
<<= static_cast<TimeBase::TimeT
> (connect_timeout
* 10000);
245 pols
[0] = orb
->create_policy (TAO::CONNECTION_TIMEOUT_POLICY_TYPE
, a
);
246 policy_current
->set_policy_overrides (pols
, ADD_OVERRIDE
);
251 void set_buffering (ORB_ptr orb
)
253 Object_var obj
= orb
->resolve_initial_references ("PolicyCurrent");
254 PolicyCurrent_var policy_current
= PolicyCurrent::_narrow (obj
.in ());
261 if (make_request_queued
)
262 a
<<= Messaging::SYNC_NONE
;
265 pols
[0] = orb
->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE
, a
);
266 policy_current
->set_policy_overrides (pols
, ADD_OVERRIDE
);
271 if (use_buf_constraints
)
273 TAO::BufferingConstraint bc
;
276 bc_mode
|= TAO::BUFFER_MESSAGE_COUNT
;
281 bc_mode
|= TAO::BUFFER_MESSAGE_BYTES
;
286 bc_mode
|= TAO::BUFFER_TIMEOUT
;
290 bc
.message_count
= bc_count
;
291 bc
.message_bytes
= bc_bytes
;
292 bc
.timeout
= static_cast<TimeBase::TimeT
> (bc_timeout
* 10000);
296 orb
->create_policy (TAO::BUFFERING_CONSTRAINT_POLICY_TYPE
, a
);
297 policy_current
->set_policy_overrides (pols
, ADD_OVERRIDE
);
304 void reset_buffering (ORB_ptr orb
)
306 Object_var obj
= orb
->resolve_initial_references ("PolicyCurrent");
307 PolicyCurrent_var policy_current
= PolicyCurrent::_narrow (obj
.in ());
313 pols
[0] = orb
->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE
, a
);
314 policy_current
->set_policy_overrides (pols
, ADD_OVERRIDE
);
321 int ACE_TMAIN (int ac
, ACE_TCHAR
*av
[])
323 ACE_Time_Value before
= ACE_High_Res_Timer::gettimeofday_hr ();
325 int num_requests_sent
= 0;
329 ORB_var orb
= ORB_init (ac
, av
);
331 if (!parse_command_line (ac
, av
))
336 set_connect_timeout (orb
.in ());
337 set_buffering (orb
.in ());
339 ACE_TString
ior (server_ior
);
342 ior
= non_existent_ior
;
345 Object_var obj
= orb
->string_to_object (ior
.c_str ());
347 ACE_ASSERT (! is_nil (obj
.in ()));
349 Tester_var tmp_tester
;
352 tmp_tester
= Tester::_narrow (obj
.in ());
353 tmp_tester
->test2 (-2);
354 ACE_DEBUG ((LM_DEBUG
, "Connected...\n"));
357 tmp_tester
= Tester::_unchecked_narrow (obj
.in ());
359 Tester_var tester
= set_request_timeout (tmp_tester
.in (), orb
.in ());
361 ACE_ASSERT (! is_nil (tester
.in ()));
365 // Using make_request_queued option to work around test failure due to
366 // different connection establishment behavior between OS. Some system
367 // can connect immediately and some may take longer time. With the flag on,
368 // the test sets the SYNC_NONE scope and sends a request so the transport
369 // queue is not empty for some SYNC_DELAYED_BUFFERING test case and hence
370 // the requests are all queued and will be received by server continuously
371 // during a short period.
372 if (make_request_queued
)
374 //Send this message while using SYNC_NONE.
375 //This would leave the request in transport queue.
377 //Set to SYNC_DELAYED_BUFFERING.
378 //The requests will be queued since queue is not
380 reset_buffering (orb
.in ());
383 for (; i
< num_requests
; ++i
)
385 before
= ACE_High_Res_Timer::gettimeofday_hr ();
398 catch (const CORBA::TRANSIENT
&)
400 ACE_DEBUG ((LM_DEBUG
,
401 "Transient exception during test () invocation %d\n",
403 if (retry_transients
)
404 ACE_DEBUG ((LM_DEBUG
,"retrying\n"));
408 catch (const CORBA::TIMEOUT
&)
410 ACE_DEBUG ((LM_DEBUG
,
411 "Timeout exception during test () invocation %d\n",
414 // This exception is expected with forced timeouts, since the
415 // IOR is invalid. Unless we want to retry the invocation
416 // go ahead and rethrow and let the outer catch deal with it.
417 // Likewise if force_timeouts is not set, then throw it anyway
418 // because the exception should not occur because these are
421 ACE_DEBUG ((LM_DEBUG
,"retrying\n"));
428 ACE_Time_Value after
= ACE_High_Res_Timer::gettimeofday_hr ();
429 if (max_request_time
> 0 &&
430 (after
- before
).msec () > max_request_time
)
432 ACE_DEBUG ((LM_DEBUG
,
433 "note: test() took %d ms, max is %d ms\n",
434 (after
- before
).msec (), max_request_time
));
437 ACE_DEBUG ((LM_DEBUG
, "c%d\n", i
));
439 if (request_interval
> 0)
441 ACE_Time_Value
tv (0, request_interval
* 1000);
442 ACE_Time_Value done
= tv
+
443 ACE_High_Res_Timer::gettimeofday_hr ();
453 while (ACE_High_Res_Timer::gettimeofday_hr () < done
)
460 ACE_DEBUG ((LM_DEBUG
,"request loop complete\n"));
463 if (run_orb_delay
> 0)
465 ACE_Time_Value
tv (0, run_orb_delay
* 1000);
470 if (run_orb_time
> 0)
472 ACE_Time_Value
tv (0, run_orb_time
* 1000);
476 ACE_DEBUG ((LM_DEBUG
,"Sending synch request to shutdown server\n"));
478 use_sync_scope
= false;
482 // we have one more invocation that may time out.
483 before
= ACE_High_Res_Timer::gettimeofday_hr ();
488 // At this point, we aren't interested in the time it takes, we
489 // want the peer to shut down, so use the non-timeout reference.
490 // BUT IF THIS DOES raise a timeout, it will be reported as an
492 tmp_tester
->test2 (-1);
495 orb
->shutdown (true);
501 ACE_ERROR_RETURN ((LM_ERROR
,
502 "Error: Connection did not time out.\n"),
508 catch (const CORBA::TRANSIENT
&)
510 ACE_DEBUG ((LM_DEBUG
, "caught transient exception\n"));
513 ACE_Time_Value after
= ACE_High_Res_Timer::gettimeofday_hr ();
514 long ms
= (after
- before
).msec ();
515 if ((use_twoway
|| !use_sync_scope
)
516 && request_timeout
> 0
517 && request_timeout
< connect_timeout
)
519 connect_timeout
= request_timeout
;
521 else if (use_sync_scope
&& !use_sleep
)
523 if (ms
> TIME_THRESHOLD
)
525 ACE_DEBUG ((LM_DEBUG
,
526 "note: Buffered request took %dms\n", ms
));
529 ms
= num_requests_sent
* request_interval
;
532 if (ms
- connect_timeout
> TIME_THRESHOLD
||
533 connect_timeout
- ms
> TIME_THRESHOLD
)
535 ACE_DEBUG ((LM_DEBUG
,
536 "note: Timeout expected in %d ms, "
537 "but took %d ms\n", connect_timeout
, ms
));
544 ACE_ERROR_RETURN ((LM_ERROR
, "Error: Unexpected\n"), 1);
547 catch (const CORBA::TIMEOUT
&)
549 ACE_DEBUG ((LM_DEBUG
, "caught timeout exception\n"));
552 ACE_Time_Value after
= ACE_High_Res_Timer::gettimeofday_hr ();
553 long ms
= (after
- before
).msec ();
554 if ((use_twoway
|| !use_sync_scope
)
555 && request_timeout
> 0
556 && request_timeout
< connect_timeout
)
558 connect_timeout
= request_timeout
;
560 else if (use_sync_scope
&& !use_sleep
)
562 if (ms
> TIME_THRESHOLD
)
564 ACE_DEBUG ((LM_DEBUG
,
565 "note: Buffered request took %d ms\n",
569 ms
= num_requests_sent
* request_interval
;
572 if (ms
- connect_timeout
> TIME_THRESHOLD
||
573 connect_timeout
- ms
> TIME_THRESHOLD
)
575 ACE_DEBUG ((LM_DEBUG
,
576 "note: Timeout expected in %d ms, "
577 "but took %d ms\n", connect_timeout
, ms
));
584 ACE_ERROR_RETURN ((LM_ERROR
, "Error: Unexpected\n"), 1);
588 catch (Exception
&ex
)
590 ACE_ERROR ((LM_ERROR
, "client: %s\n\nLast operation took %d ms.\n",
592 (ACE_High_Res_Timer::gettimeofday_hr () - before
).msec ()));