More tests update
[ACE_TAO.git] / TAO / tests / AMI_Timeouts / timeout_client.cpp
blobdbef6403ab7eb265dcfbdedc003cb3b71683a3e8
1 //=============================================================================
2 /**
3 * @file timeout_client.cpp
5 * Tests for proper handling of timeouts with AMI
7 * @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
8 */
9 //=============================================================================
11 #include "timeout_client.h"
12 #include "tao/Messaging/Messaging.h"
13 #include "tao/PolicyC.h"
14 #include "tao/AnyTypeCode/Any.h"
15 #include "ace/OS_NS_unistd.h"
17 TimeoutClient::TimeoutClient (CORBA::ORB_ptr orb,
18 TimeoutObj_ptr timeoutObject,
19 AMI_TimeoutObjHandler_ptr replyHandlerObject,
20 TimeoutHandler_i *timeoutHandler_i,
21 unsigned long timeToWait)
22 : orb_ (CORBA::ORB::_duplicate (orb))
23 , timeoutObject_ (TimeoutObj::_duplicate (timeoutObject))
24 , replyHandlerObject_ (AMI_TimeoutObjHandler::_duplicate (replyHandlerObject))
25 , timeoutHandler_i_ (timeoutHandler_i)
26 , local_reply_excep_counter_ (0)
27 , INVOKE_SYNCH (0)
28 , INVOKE_ASYNCH (1)
29 , timeToWait_ (timeToWait)
33 TimeoutClient::~TimeoutClient ()
37 int
38 TimeoutClient::svc ()
40 this->initialize ();
42 try
44 // Tests timeouts for synchronous
45 this->synch_test ();
47 // Tests AMI timeouts for influences on non-timeout calls
48 this->none_test ();
50 // Tests AMI timeouts for accuracy
51 this->accuracy_test ();
53 // Tests AMI timeouts for influences on non-timeout calls
54 this->none_test ();
56 // shut down remote ORB
57 timeoutObject_->shutdown ();
59 ACE_Time_Value tv (0, 20); // wait for the ORB to deliver the shutdown
60 ACE_OS::sleep (tv);
62 // shut down local ORB
63 orb_->shutdown (0);
65 catch (const CORBA::Exception& ex)
67 ex._tao_print_exception ("Caught exception:");
68 return 1;
71 ACE_DEBUG ((LM_DEBUG,
72 "TimeoutClient::svc: Done\n\n"));
74 return 0;
77 int
78 TimeoutClient::initialize ()
80 try
82 CORBA::Object_var object =
83 orb_->resolve_initial_references ("ORBPolicyManager");
85 policy_manager_ =
86 CORBA::PolicyManager::_narrow (object.in ());
88 catch (const CORBA::Exception& ex)
90 ex._tao_print_exception ("Caught exception:");
91 return 1;
94 return 0;
98 void
99 TimeoutClient::send (CORBA::Boolean async,
100 unsigned long local_timeout,
101 unsigned long remote_sleep)
103 ACE_DEBUG ((LM_DEBUG,
104 "(%P|%t) Invoking "));
105 if (async)
106 ACE_DEBUG ((LM_DEBUG,
107 "asynch "));
108 else
109 ACE_DEBUG ((LM_DEBUG,
110 "synch "));
112 ACE_DEBUG ((LM_DEBUG,
113 "local: %dms remote: %dms ... ",
114 local_timeout,
115 remote_sleep));
117 CORBA::PolicyList policy_list (1);
121 if (local_timeout != 0)
123 TimeBase::TimeT timeout = 10000 * local_timeout;
125 CORBA::Any any_orb;
126 any_orb <<= timeout;
128 policy_list.length (1);
129 policy_list[0] =
130 orb_->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
131 any_orb);
133 policy_manager_->set_policy_overrides (policy_list,
134 CORBA::SET_OVERRIDE);
136 else
138 policy_list.length (0);
139 policy_manager_->set_policy_overrides (policy_list,
140 CORBA::SET_OVERRIDE);
144 // Start time measurement at the reply handler
145 timeoutHandler_i_->start ();
147 if (async)
149 timeoutObject_->sendc_sendTimeToWait (replyHandlerObject_.in (),
150 remote_sleep);
152 else // synch
154 timeoutObject_->sendTimeToWait (remote_sleep);
157 catch (const CORBA::TIMEOUT& )
159 local_reply_excep_counter_++;
161 // Trap this exception and continue...
162 ACE_DEBUG ((LM_DEBUG,
163 " timeout "));
167 // get rid of the policy, you created before.
170 if (local_timeout != 0)
172 policy_list[0]->destroy ();
175 catch (const CORBA::Exception&)
177 ACE_DEBUG ((LM_DEBUG,
178 "Error: Unexpected exception\n\n"));
181 // wait for responses
182 ACE_Time_Value tv (0, (local_timeout + remote_sleep)*2000 + 4000);
183 ACE_OS::sleep (tv);
185 ACE_Time_Value &elapsed_time = timeoutHandler_i_->elapsed_time ();
187 if (async)
188 ACE_DEBUG ((LM_DEBUG,
189 " .. needed %dms\n",
190 elapsed_time.msec()));
191 else
192 ACE_DEBUG ((LM_DEBUG,
193 " .. done\n"));
198 TimeoutClient::synch_test ()
200 ACE_DEBUG ((LM_DEBUG,
201 "TimeoutClient::synch_test - Begin\n"));
202 timeoutHandler_i_->reset_reply_counter ();
203 timeoutHandler_i_->reset_reply_excep_counter ();
204 local_reply_excep_counter_ = 0;
206 this->send (INVOKE_SYNCH,
207 0, // local
208 0); // remote
210 this->send (INVOKE_SYNCH,
211 timeToWait_, // local
212 timeToWait_*2); // remote
214 this->send (INVOKE_SYNCH,
215 0, // local
216 0); // remote
218 if (timeoutHandler_i_->reply_counter () != 0
219 || timeoutHandler_i_->reply_excep_counter () != 0
220 || local_reply_excep_counter_ != 1)
221 ACE_DEBUG ((LM_DEBUG,
222 "**** Error in replies %d %d %d.\n\n",
223 timeoutHandler_i_->reply_counter (),
224 timeoutHandler_i_->reply_excep_counter (),
225 local_reply_excep_counter_));
227 ACE_DEBUG ((LM_DEBUG,
228 "TimeoutClient::synch_test - End\n\n"));
230 return 0;
234 TimeoutClient::accuracy_test ()
236 ACE_DEBUG ((LM_DEBUG,
237 "TimeoutClient::accuracy_test - Begin\n\n"));
239 timeoutHandler_i_->reset_reply_counter ();
240 timeoutHandler_i_->reset_reply_excep_counter ();
241 local_reply_excep_counter_ = 0;
243 this->send (INVOKE_ASYNCH,
244 timeToWait_,
245 (unsigned long)(timeToWait_*1.5));
247 this->send (INVOKE_ASYNCH,
248 timeToWait_,
249 (unsigned long)(timeToWait_*0.5));
251 if (timeoutHandler_i_->reply_counter () != 1
252 || timeoutHandler_i_->reply_excep_counter () != 1
253 || local_reply_excep_counter_ != 0)
254 ACE_DEBUG ((LM_DEBUG,
255 "**** Error in replies %d %d %d.\n\n",
256 timeoutHandler_i_->reply_counter (),
257 timeoutHandler_i_->reply_excep_counter (),
258 local_reply_excep_counter_));
260 ACE_DEBUG ((LM_DEBUG,
261 "TimeoutClient::accuracy_test - End\n\n"));
263 return 0;
267 TimeoutClient::none_test ()
269 ACE_DEBUG ((LM_DEBUG,
270 "TimeoutClient::none_test - Begin\n\n"));
272 timeoutHandler_i_->reset_reply_counter ();
273 timeoutHandler_i_->reset_reply_excep_counter ();
274 local_reply_excep_counter_ = 0;
276 this->send (INVOKE_ASYNCH,
280 this->send (INVOKE_ASYNCH,
281 timeToWait_,
282 timeToWait_+10); // trigger a timeout
284 this->send (INVOKE_ASYNCH,
285 timeToWait_,
288 if (timeoutHandler_i_->reply_counter () != 2
289 || timeoutHandler_i_->reply_excep_counter () != 1
290 || local_reply_excep_counter_ != 0)
291 ACE_DEBUG ((LM_DEBUG,
292 "**** Error in replies %d %d %d.\n\n",
293 timeoutHandler_i_->reply_counter (),
294 timeoutHandler_i_->reply_excep_counter (),
295 local_reply_excep_counter_));
297 ACE_DEBUG ((LM_DEBUG,
298 "TimeoutClient::none_test - End\n\n"));
300 return 0;