Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Timeout / client.cpp
blob6faa56e1516cf131ab92c1a2761edee9db12b58e
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);
169 ACE_DEBUG ((LM_DEBUG,
170 "client(%P) Set the ORB policies\n"));
172 policy_list.length (1);
173 policy_list[0] =
174 orb->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
175 any_orb);
177 policy_manager->set_policy_overrides (policy_list,
178 CORBA::SET_OVERRIDE);
180 send_echo (orb1, orb.in (), server.in (), t);
182 policy_list[0]->destroy ();
190 ACE_DEBUG ((LM_DEBUG,
191 "client(%P) Set the thread policies\n"));
193 policy_list.length (1);
194 policy_list[0] =
195 orb->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
196 any_thread);
198 policy_current->set_policy_overrides (policy_list,
199 CORBA::SET_OVERRIDE);
201 send_echo (thread1, orb.in (), server.in (), t);
203 policy_list[0]->destroy ();
211 ACE_DEBUG ((LM_DEBUG,
212 "client(%P) Use the object policies\n"));
213 send_echo (object1, orb.in (), timeout_server.in (), t);
216 ACE_DEBUG ((LM_DEBUG,
217 "\n\n\nclient(%P) Test completed, "
218 "resynch with server\n"));
219 policy_list.length (0);
220 policy_manager->set_policy_overrides (policy_list,
221 CORBA::SET_OVERRIDE);
222 policy_current->set_policy_overrides (policy_list,
223 CORBA::SET_OVERRIDE);
225 send_echo (none, orb.in (), server.in (), 0);
227 server->shutdown ();
229 int timeout_count_total = 0;
230 int in_time_count_total = 0;
231 for (int i = 0; i < 4; i++) {
232 timeout_count_total += timeout_count[i];
233 in_time_count_total += in_time_count[i];
234 ACE_DEBUG ((LM_DEBUG, "in_time_count[%C]= %d timeout_count[%C]= %d\n",
235 to_type_names[i], in_time_count[i],
236 to_type_names[i], timeout_count[i]));
239 if (timeout_count_total == 0)
240 ACE_ERROR ((LM_ERROR,
241 "ERROR: No messages timed out\n"));
243 //FUZZ: disable check_for_lack_ACE_OS
244 if (in_time_count_total == 0)
245 ACE_ERROR ((LM_ERROR,
246 "ERROR: No messages on time (within time limit)\n"));
247 //FUZZ: enable check_for_lack_ACE_OS
249 ACE_DEBUG ((LM_DEBUG, "in_time_count_total = %d, timeout_count_total = %d\n",
250 in_time_count_total, timeout_count_total));
252 orb->destroy ();
254 catch (const CORBA::Exception& ex)
256 ex._tao_print_exception ("Exception caught:");
257 return 1;
259 return 0;