Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / MT_Timeout / Client_Task.cpp
blobc32815919d084a38a278f16e69feda6f1ecf715d
2 #include "Client_Task.h"
3 #include "ace/OS_NS_unistd.h"
4 #include "tao/TimeBaseC.h"
5 #include "tao/Messaging/Messaging.h"
6 #include "tao/AnyTypeCode/Any.h"
8 Client_Task::Client_Task (CORBA::ORB_ptr orb,
9 Test::Sleep_Service_ptr sleep_service,
10 int iterations,
11 CORBA::ULong sleep_time,
12 CORBA::ULong timeout)
13 : orb_ (CORBA::ORB::_duplicate (orb))
14 , sleep_service_ (Test::Sleep_Service::_duplicate (sleep_service))
15 , iterations_ (iterations)
16 , sleep_time_ (sleep_time)
17 , timeout_ (timeout)
18 , successful_calls_ (0)
19 , timed_out_calls_ (0)
20 , too_big_difference_ (0)
24 int
25 Client_Task::successful_calls () const
27 return this->successful_calls_;
30 int
31 Client_Task::timed_out_calls () const
33 return this->timed_out_calls_;
36 int
37 Client_Task::too_big_difference_calls () const
39 return this->too_big_difference_;
42 int
43 Client_Task::svc ()
45 int successful_calls = 0;
46 int timed_out_calls = 0;
48 try
50 this->validate_connection ();
52 CORBA::Object_var object =
53 this->orb_->resolve_initial_references ("PolicyCurrent");
54 CORBA::PolicyCurrent_var policy_current =
55 CORBA::PolicyCurrent::_narrow (object.in ());
57 TimeBase::TimeT timeout_period = 10 * this->timeout_;
59 CORBA::Any timeout_as_any;
60 timeout_as_any <<= timeout_period;
62 CORBA::PolicyList policy_list (1);
63 policy_list.length (1);
64 policy_list[0] =
65 this->orb_->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
66 timeout_as_any);
68 policy_current->set_policy_overrides (policy_list,
69 CORBA::ADD_OVERRIDE);
71 for (int i = 0; i != this->iterations_; ++i)
73 int retval = this->one_iteration ();
75 if (retval == 1)
76 ++successful_calls;
77 else if (retval == 0)
78 ++timed_out_calls;
80 #if 0
81 if (i % 50 == 0)
83 ACE_DEBUG ((LM_DEBUG,
84 "(%P|%t) - Client_Task::svc %d / %d iterations\n",
85 i, this->iterations_));
87 #endif /* 0 */
90 catch (const CORBA::Exception& ex)
92 ex._tao_print_exception ("Exception caught:");
93 return -1;
96 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->mutex_, -1);
97 this->successful_calls_ += successful_calls;
98 this->timed_out_calls_ += timed_out_calls;
100 return 0;
103 void
104 Client_Task::validate_connection ()
108 for (int i = 0; i != 100; ++i)
110 (void) this->sleep_service_->go_to_sleep (0);
113 catch (const CORBA::TRANSIENT& )
115 // Ignore transient exceptions
120 Client_Task::one_iteration ()
124 ACE_Time_Value start = ACE_OS::gettimeofday ();
126 this->sleep_service_->go_to_sleep (this->sleep_time_);
128 ACE_Time_Value end = ACE_OS::gettimeofday ();
130 if (this->sleep_time_ > this->timeout_)
132 // If we don't sleep then the next request will also
133 // timeout:
134 // The server is already sleeping, the client sends a new
135 // request, but the server cannot process it until the
136 // previous sleep completes. The sleep times for multiple
137 // requests accumulate, and the client always fails.
138 CORBA::ULong remainder =
139 this->sleep_time_ - this->timeout_;
140 ACE_Time_Value tv (0, remainder);
141 ACE_OS::sleep (tv);
144 if (this->timeout_ > 0)
146 CORBA::ULong max_milliseconds = this->timeout_ / 1000;
148 ACE_Time_Value elapsed = end - start;
149 CORBA::ULong elapsed_milliseconds = elapsed.msec ();
151 if (elapsed_milliseconds > max_milliseconds)
153 CORBA::ULong difference =
154 elapsed_milliseconds - max_milliseconds;
156 // If the different is more than 10 milliseconds we are
157 // *way* off, this is an error.
158 if (difference > 10)
160 this->too_big_difference_++;
161 ACE_ERROR ((LM_ERROR,
162 "(%P|%t) Elapsed time = %d, expected %d\n",
163 elapsed_milliseconds, max_milliseconds));
168 return 1;
170 catch (const CORBA::TIMEOUT& )
172 // Ignore this exception, it is usually caused by a transient
173 // condition
174 return 0;
176 catch (const CORBA::Exception& ex)
178 ex._tao_print_exception ("Exception caught:");
181 return -1;