Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / AMI_Timeouts / timeout_client.cpp
blobd9416804bc33f0d2fb25393cb6137e52dfe5d85b
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 (false);
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 "));
166 // get rid of the policy, you created before.
169 if (local_timeout != 0)
171 policy_list[0]->destroy ();
174 catch (const CORBA::Exception&)
176 ACE_DEBUG ((LM_DEBUG,
177 "Error: Unexpected exception\n\n"));
180 // wait for responses
181 ACE_Time_Value tv (0, (local_timeout + remote_sleep)*2000 + 4000);
182 ACE_OS::sleep (tv);
184 ACE_Time_Value &elapsed_time = timeoutHandler_i_->elapsed_time ();
186 if (async)
187 ACE_DEBUG ((LM_DEBUG,
188 " .. needed %dms\n",
189 elapsed_time.msec()));
190 else
191 ACE_DEBUG ((LM_DEBUG,
192 " .. done\n"));
197 TimeoutClient::synch_test ()
199 ACE_DEBUG ((LM_DEBUG,
200 "TimeoutClient::synch_test - Begin\n"));
201 timeoutHandler_i_->reset_reply_counter ();
202 timeoutHandler_i_->reset_reply_excep_counter ();
203 local_reply_excep_counter_ = 0;
205 this->send (INVOKE_SYNCH,
206 0, // local
207 0); // remote
209 this->send (INVOKE_SYNCH,
210 timeToWait_, // local
211 timeToWait_*2); // remote
213 this->send (INVOKE_SYNCH,
214 0, // local
215 0); // remote
217 if (timeoutHandler_i_->reply_counter () != 0
218 || timeoutHandler_i_->reply_excep_counter () != 0
219 || local_reply_excep_counter_ != 1)
220 ACE_DEBUG ((LM_DEBUG,
221 "**** Error in replies %d %d %d.\n\n",
222 timeoutHandler_i_->reply_counter (),
223 timeoutHandler_i_->reply_excep_counter (),
224 local_reply_excep_counter_));
226 ACE_DEBUG ((LM_DEBUG,
227 "TimeoutClient::synch_test - End\n\n"));
229 return 0;
233 TimeoutClient::accuracy_test ()
235 ACE_DEBUG ((LM_DEBUG,
236 "TimeoutClient::accuracy_test - Begin\n\n"));
238 timeoutHandler_i_->reset_reply_counter ();
239 timeoutHandler_i_->reset_reply_excep_counter ();
240 local_reply_excep_counter_ = 0;
242 this->send (INVOKE_ASYNCH,
243 timeToWait_,
244 (unsigned long)(timeToWait_*1.5));
246 this->send (INVOKE_ASYNCH,
247 timeToWait_,
248 (unsigned long)(timeToWait_*0.5));
250 if (timeoutHandler_i_->reply_counter () != 1
251 || timeoutHandler_i_->reply_excep_counter () != 1
252 || local_reply_excep_counter_ != 0)
253 ACE_DEBUG ((LM_DEBUG,
254 "**** Error in replies %d %d %d.\n\n",
255 timeoutHandler_i_->reply_counter (),
256 timeoutHandler_i_->reply_excep_counter (),
257 local_reply_excep_counter_));
259 ACE_DEBUG ((LM_DEBUG,
260 "TimeoutClient::accuracy_test - End\n\n"));
262 return 0;
266 TimeoutClient::none_test ()
268 ACE_DEBUG ((LM_DEBUG,
269 "TimeoutClient::none_test - Begin\n\n"));
271 timeoutHandler_i_->reset_reply_counter ();
272 timeoutHandler_i_->reset_reply_excep_counter ();
273 local_reply_excep_counter_ = 0;
275 this->send (INVOKE_ASYNCH,
279 this->send (INVOKE_ASYNCH,
280 timeToWait_,
281 timeToWait_+10); // trigger a timeout
283 this->send (INVOKE_ASYNCH,
284 timeToWait_,
287 if (timeoutHandler_i_->reply_counter () != 2
288 || timeoutHandler_i_->reply_excep_counter () != 1
289 || local_reply_excep_counter_ != 0)
290 ACE_DEBUG ((LM_DEBUG,
291 "**** Error in replies %d %d %d.\n\n",
292 timeoutHandler_i_->reply_counter (),
293 timeoutHandler_i_->reply_excep_counter (),
294 local_reply_excep_counter_));
296 ACE_DEBUG ((LM_DEBUG,
297 "TimeoutClient::none_test - End\n\n"));
299 return 0;