Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Timeout / client.cpp
blob463a4e638375fa88c089678518fae3cf5d1c693a
1 #include "testC.h"
3 #include "tao/Messaging/Messaging.h"
4 #include "tao/AnyTypeCode/Any.h"
5 #include "ace/Get_Opt.h"
7 const ACE_TCHAR *ior = ACE_TEXT ("file://test.ior");
8 int min_timeout = 0;
9 int max_timeout = 20;
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:l:h:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'k':
21 ior = get_opts.opt_arg ();
22 break;
24 case 'l':
25 min_timeout = ACE_OS::atoi (get_opts.opt_arg ());
26 break;
28 case 'h':
29 max_timeout = ACE_OS::atoi (get_opts.opt_arg ());
30 break;
32 case '?':
33 default:
34 ACE_ERROR_RETURN ((LM_ERROR,
35 "usage: %s "
36 "-k <ior> "
37 "-l <min_timeout> "
38 "-h <max_timeout> "
39 "\n",
40 argv [0]),
41 -1);
43 // Indicates successful parsing of the command line
44 return 0;
47 enum TO_TYPE {none = 0, orb1 = 1, thread1 = 2, object1 = 3};
48 const char *to_type_names[4] ={"none", "orb", "thread", "object"};
50 static int timeout_count[4] = {0, 0, 0, 0};
51 static int in_time_count[4] = {0, 0, 0, 0};
53 void
54 send_echo (TO_TYPE ctype, CORBA::ORB_ptr orb,
55 Simple_Server_ptr server,
56 CORBA::Long t)
58 try
60 server->echo (0, t);
62 in_time_count[ctype]++;
64 catch (const CORBA::TIMEOUT& )
66 timeout_count[ctype]++;
68 // Trap this exception and continue...
69 ACE_DEBUG ((LM_DEBUG,
70 "==> Trapped a TIMEOUT exception (expected)\n"));
72 // Sleep so the server can send the reply...
73 ACE_Time_Value tv (max_timeout / 1000, // max_timeout is in msec, so get seconds
74 (max_timeout % 1000) * 1000); // and usec
76 // This is a non-standard TAO call that's used to give the
77 // client ORB a chance to cleanup the reply that's come back
78 // from the server.
79 orb->run (tv);
84 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
86 try
88 CORBA::ORB_var orb =
89 CORBA::ORB_init (argc, argv);
91 if (parse_args (argc, argv) != 0)
92 return 1;
94 CORBA::Object_var object =
95 orb->string_to_object (ior);
97 Simple_Server_var server =
98 Simple_Server::_narrow (object.in ());
100 if (CORBA::is_nil (server.in ()))
102 ACE_ERROR_RETURN ((LM_ERROR,
103 "Object reference <%s> is nil.\n",
104 ior),
108 object =
109 orb->resolve_initial_references ("ORBPolicyManager");
111 CORBA::PolicyManager_var policy_manager =
112 CORBA::PolicyManager::_narrow (object.in ());
114 object =
115 orb->resolve_initial_references ("PolicyCurrent");
117 CORBA::PolicyCurrent_var policy_current =
118 CORBA::PolicyCurrent::_narrow (object.in ());
120 TimeBase::TimeT mid_value =
121 10000 * (min_timeout + max_timeout) / 2; // convert from msec to "TimeT" (0.1 usec units)
123 CORBA::Any any_orb;
124 any_orb <<= mid_value;
125 CORBA::Any any_thread;
126 any_thread <<= mid_value + 10000; // midvalue + 1 msec
127 CORBA::Any any_object;
128 any_object <<= mid_value + 20000; // midvalue + 2 msec
130 CORBA::PolicyList policy_list (1);
131 policy_list.length (1);
132 policy_list[0] =
133 orb->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
134 any_object);
135 object =
136 server->_set_policy_overrides (policy_list,
137 CORBA::SET_OVERRIDE);
139 Simple_Server_var timeout_server =
140 Simple_Server::_narrow (object.in ());
142 policy_list[0]->destroy ();
143 policy_list[0] = CORBA::Policy::_nil ();
145 ACE_DEBUG ((LM_DEBUG,
146 "client (%P) testing from %d to %d milliseconds\n",
147 min_timeout, max_timeout));
149 for (CORBA::Long t = min_timeout; t < max_timeout; ++t)
151 ACE_DEBUG ((LM_DEBUG,
152 "\n================================\n"
153 "Trying with timeout = %d msec\n", t));
155 ACE_DEBUG ((LM_DEBUG,
156 "Cleanup ORB/Thread/Object policies\n"));
158 policy_list.length (0);
159 policy_manager->set_policy_overrides (policy_list,
160 CORBA::SET_OVERRIDE);
161 policy_current->set_policy_overrides (policy_list,
162 CORBA::SET_OVERRIDE);
164 send_echo (none, orb.in (), server.in (), t);
168 ACE_DEBUG ((LM_DEBUG,
169 "client(%P) Set the ORB policies\n"));
171 policy_list.length (1);
172 policy_list[0] =
173 orb->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
174 any_orb);
176 policy_manager->set_policy_overrides (policy_list,
177 CORBA::SET_OVERRIDE);
179 send_echo (orb1, orb.in (), server.in (), t);
181 policy_list[0]->destroy ();
187 ACE_DEBUG ((LM_DEBUG,
188 "client(%P) Set the thread policies\n"));
190 policy_list.length (1);
191 policy_list[0] =
192 orb->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
193 any_thread);
195 policy_current->set_policy_overrides (policy_list,
196 CORBA::SET_OVERRIDE);
198 send_echo (thread1, orb.in (), server.in (), t);
200 policy_list[0]->destroy ();
206 ACE_DEBUG ((LM_DEBUG,
207 "client(%P) Use the object policies\n"));
208 send_echo (object1, orb.in (), timeout_server.in (), t);
211 ACE_DEBUG ((LM_DEBUG,
212 "\n\n\nclient(%P) Test completed, "
213 "resynch with server\n"));
214 policy_list.length (0);
215 policy_manager->set_policy_overrides (policy_list,
216 CORBA::SET_OVERRIDE);
217 policy_current->set_policy_overrides (policy_list,
218 CORBA::SET_OVERRIDE);
220 send_echo (none, orb.in (), server.in (), 0);
222 server->shutdown ();
224 int timeout_count_total = 0;
225 int in_time_count_total = 0;
226 for (int i = 0; i < 4; i++) {
227 timeout_count_total += timeout_count[i];
228 in_time_count_total += in_time_count[i];
229 ACE_DEBUG ((LM_DEBUG, "in_time_count[%C]= %d timeout_count[%C]= %d\n",
230 to_type_names[i], in_time_count[i],
231 to_type_names[i], timeout_count[i]));
234 if (timeout_count_total == 0)
235 ACE_ERROR ((LM_ERROR,
236 "ERROR: No messages timed out\n"));
238 //FUZZ: disable check_for_lack_ACE_OS
239 if (in_time_count_total == 0)
240 ACE_ERROR ((LM_ERROR,
241 "ERROR: No messages on time (within time limit)\n"));
242 //FUZZ: enable check_for_lack_ACE_OS
244 ACE_DEBUG ((LM_DEBUG, "in_time_count_total = %d, timeout_count_total = %d\n",
245 in_time_count_total, timeout_count_total));
247 orb->destroy ();
249 catch (const CORBA::Exception& ex)
251 ex._tao_print_exception ("Exception caught:");
252 return 1;
254 return 0;