Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / MT_Timeout / Client_Task.cpp
blob97770fe0546f20e87f580dbe7a8518b668907643
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 (void) const
27 return this->successful_calls_;
30 int
31 Client_Task::timed_out_calls (void) const
33 return this->timed_out_calls_;
36 int
37 Client_Task::too_big_difference_calls (void) const
39 return this->too_big_difference_;
42 int
43 Client_Task::svc (void)
46 int successful_calls = 0;
47 int timed_out_calls = 0;
49 try
51 this->validate_connection ();
53 CORBA::Object_var object =
54 this->orb_->resolve_initial_references ("PolicyCurrent");
55 CORBA::PolicyCurrent_var policy_current =
56 CORBA::PolicyCurrent::_narrow (object.in ());
58 TimeBase::TimeT timeout_period = 10 * this->timeout_;
60 CORBA::Any timeout_as_any;
61 timeout_as_any <<= timeout_period;
63 CORBA::PolicyList policy_list (1);
64 policy_list.length (1);
65 policy_list[0] =
66 this->orb_->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
67 timeout_as_any);
69 policy_current->set_policy_overrides (policy_list,
70 CORBA::ADD_OVERRIDE);
72 for (int i = 0; i != this->iterations_; ++i)
74 int retval = this->one_iteration ();
76 if (retval == 1)
77 ++successful_calls;
78 else if (retval == 0)
79 ++timed_out_calls;
81 #if 0
82 if (i % 50 == 0)
84 ACE_DEBUG ((LM_DEBUG,
85 "(%P|%t) - Client_Task::svc %d / %d iterations\n",
86 i, this->iterations_));
88 #endif /* 0 */
91 catch (const CORBA::Exception& ex)
93 ex._tao_print_exception ("Exception caught:");
94 return -1;
97 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->mutex_, -1);
98 this->successful_calls_ += successful_calls;
99 this->timed_out_calls_ += timed_out_calls;
101 return 0;
104 void
105 Client_Task::validate_connection (void)
109 for (int i = 0; i != 100; ++i)
111 (void) this->sleep_service_->go_to_sleep (0);
114 catch (const CORBA::TRANSIENT& )
116 // Ignore transient exceptions
121 Client_Task::one_iteration (void)
125 ACE_Time_Value start = ACE_OS::gettimeofday ();
127 this->sleep_service_->go_to_sleep (this->sleep_time_);
129 ACE_Time_Value end = ACE_OS::gettimeofday ();
131 if (this->sleep_time_ > this->timeout_)
133 // If we don't sleep then the next request will also
134 // timeout:
135 // The server is already sleeping, the client sends a new
136 // request, but the server cannot process it until the
137 // previous sleep completes. The sleep times for multiple
138 // requests accumulate, and the client always fails.
139 CORBA::ULong remainder =
140 this->sleep_time_ - this->timeout_;
141 ACE_Time_Value tv (0, remainder);
142 ACE_OS::sleep (tv);
145 if (this->timeout_ > 0)
147 CORBA::ULong max_milliseconds = this->timeout_ / 1000;
149 ACE_Time_Value elapsed = end - start;
150 CORBA::ULong elapsed_milliseconds = elapsed.msec ();
152 if (elapsed_milliseconds > max_milliseconds)
154 CORBA::ULong difference =
155 elapsed_milliseconds - max_milliseconds;
157 // If the different is more than 10 milliseconds we are
158 // *way* off, this is an error.
159 if (difference > 10)
161 this->too_big_difference_++;
162 ACE_ERROR ((LM_ERROR,
163 "(%P|%t) Elapsed time = %d, expected %d\n",
164 elapsed_milliseconds, max_milliseconds));
169 return 1;
171 catch (const CORBA::TIMEOUT& )
173 // Ignore this exception, it is usually caused by a transient
174 // condition
175 return 0;
177 catch (const CORBA::Exception& ex)
179 ex._tao_print_exception ("Exception caught:");
182 return -1;