Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / Reliable_Oneways / client.cpp
blob90190845885576b0e9ca88dac70575459a053d53
1 #include "TestC.h"
2 #include "tao/Messaging/Messaging.h"
3 #include "tao/AnyTypeCode/Any.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/OS_NS_sys_time.h"
7 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
8 int test_sync_with_target = 0;
9 int test_sync_with_server = 0;
10 int test_sync_with_transport = 0;
11 int test_sync_none = 0;
12 int iterations = 500;
14 const int ALIVE = 0;
15 const int DESTROYED = 1;
16 const int SHUTDOWN = 2;
17 int server_status = ALIVE;
19 int successful_calls = 0;
20 int failed_calls = 0;
22 int
23 parse_args (int argc, ACE_TCHAR *argv[])
25 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:i:ntsr"));
26 int c;
28 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'k':
32 ior = get_opts.opt_arg ();
33 break;
35 case 'i':
36 iterations = ACE_OS::atoi (get_opts.opt_arg ());
37 break;
39 case 'n':
40 test_sync_none = 1;
41 break;
43 case 't':
44 test_sync_with_transport = 1;
45 break;
47 case 's':
48 test_sync_with_server = 1;
49 break;
51 case 'r':
52 test_sync_with_target = 1;
53 break;
55 case '?':
56 default:
57 ACE_ERROR_RETURN ((LM_ERROR,
58 "usage: %s "
59 "-k <ior> "
60 "-n -t -s -r "
61 "\n",
62 argv [0]),
63 -1);
65 // Indicates successful parsing of the command line
66 return 0;
69 void run_test_sync_with_target (Test::Oneway_Receiver_ptr oneway_receiver);
70 void run_test_sync_with_server (Test::Oneway_Receiver_ptr oneway_receiver);
71 void run_test_sync_with_transport (Test::Oneway_Receiver_ptr oneway_receiver);
72 void run_test_sync_none (Test::Oneway_Receiver_ptr oneway_receiver);
75 int
76 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
78 try
80 CORBA::ORB_var orb =
81 CORBA::ORB_init (argc, argv);
83 if (parse_args (argc, argv) != 0)
84 return 1;
86 CORBA::Object_var tmp =
87 orb->string_to_object(ior);
89 Test::Oneway_Receiver_var oneway_receiver =
90 Test::Oneway_Receiver::_narrow(tmp.in ());
92 if (CORBA::is_nil (oneway_receiver.in ()))
94 ACE_ERROR_RETURN ((LM_DEBUG,
95 "Nil oneway receiver reference <%s>\n",
96 ior),
97 1);
100 if (test_sync_with_target)
102 ACE_DEBUG ((LM_DEBUG, "Running SYNC_WITH_TARGET\n"));
103 run_test_sync_with_target (oneway_receiver.in ());
105 else if (test_sync_with_server)
107 ACE_DEBUG ((LM_DEBUG, "Running SYNC_WITH_SERVER\n"));
108 run_test_sync_with_server (oneway_receiver.in ());
110 else if (test_sync_with_transport)
112 ACE_DEBUG ((LM_DEBUG, "Running SYNC_WITH_TRANSPORT\n"));
113 run_test_sync_with_transport (oneway_receiver.in ());
115 else
117 ACE_DEBUG ((LM_DEBUG, "Running SYNC_NONE\n"));
118 run_test_sync_none (oneway_receiver.in ());
121 //FUZZ: disable check_for_lack_ACE_OS
122 // Run the orb for 3 seconds, this way we make sure things are flushed
123 // to the transport.
124 ACE_Time_Value time (3, 0);
125 //FUZZ: enable check_for_lack_ACE_OS
127 orb->run (time);
129 if (successful_calls == 0)
130 ACE_ERROR ((LM_ERROR, "ERROR: No requests were successful\n"));
132 if (failed_calls != 0)
133 ACE_ERROR ((LM_ERROR, "ERROR: At least %d requests failed\n",
134 failed_calls));
136 if (failed_calls + successful_calls != iterations)
137 ACE_ERROR ((LM_ERROR,
138 "ERROR: Mismatched number of calls (%d + %d != %d)\n",
139 successful_calls, failed_calls, iterations));
141 orb->destroy ();
143 catch (const CORBA::Exception& ex)
145 ex._tao_print_exception ("Exception caught:");
146 return 1;
149 return 0;
152 void
153 set_sync_scope_policy (Messaging::SyncScope sync_scope)
155 int argc = 0;
156 ACE_TCHAR **argv = 0;
157 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
159 CORBA::Object_var object =
160 orb->resolve_initial_references ("PolicyCurrent");
162 CORBA::PolicyCurrent_var policy_current =
163 CORBA::PolicyCurrent::_narrow (object.in ());
165 if (CORBA::is_nil (policy_current.in ()))
167 ACE_ERROR ((LM_ERROR, "ERROR: Nil policy current\n"));
168 return;
170 CORBA::Any scope_as_any;
171 scope_as_any <<= sync_scope;
173 CORBA::PolicyList policies(1); policies.length (1);
174 policies[0] =
175 orb->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
176 scope_as_any);
178 policy_current->set_policy_overrides (policies, CORBA::ADD_OVERRIDE);
180 policies[0]->destroy ();
183 void
184 run_one_iteration (int i,
185 Test::Oneway_Receiver_ptr oneway_receiver,
186 Test::Shutdown_Helper_ptr shutdown_helper)
190 if (i == iterations / 3)
192 ACE_DEBUG ((LM_DEBUG, "Destroying object\n"));
193 server_status = DESTROYED;
194 oneway_receiver->destroy ();
196 if (i == 2 * iterations / 3)
198 ACE_DEBUG ((LM_DEBUG, "Shutting down server process\n"));
199 server_status = SHUTDOWN;
200 shutdown_helper->shutdown ();
203 catch (const CORBA::Exception&)
207 oneway_receiver->raise_no_permission ();
210 /// Helper routine to set breakpoints during failures
211 void
212 call_failed (const CORBA::SystemException &ex)
214 failed_calls++;
216 ex._tao_print_exception ("Exception raised");
219 void
220 run_test_sync_with_target (Test::Oneway_Receiver_ptr oneway_receiver)
222 set_sync_scope_policy (Messaging::SYNC_WITH_TARGET);
224 Test::Shutdown_Helper_var shutdown_helper =
225 oneway_receiver->get_shutdown_helper ();
227 int had_comm_failure = 0;
228 for (int i = 0; i != iterations; ++i)
232 run_one_iteration (i, oneway_receiver, shutdown_helper.in ());
234 catch (const CORBA::NO_PERMISSION& ex)
236 if (server_status == ALIVE)
237 successful_calls++;
238 else
239 call_failed (ex);
241 catch (const CORBA::OBJECT_NOT_EXIST& ex)
243 if (server_status == DESTROYED)
244 successful_calls++;
245 else
246 call_failed (ex);
248 catch (const CORBA::COMM_FAILURE& ex)
250 if (server_status == SHUTDOWN && had_comm_failure == 0)
251 successful_calls++;
252 else
253 call_failed (ex);
254 had_comm_failure = 1;
256 catch (const CORBA::TRANSIENT& ex)
258 if (server_status == SHUTDOWN)
259 successful_calls++;
260 else
261 call_failed (ex);
263 catch (const CORBA::SystemException& ex)
265 call_failed (ex);
270 void
271 run_test_sync_with_server (Test::Oneway_Receiver_ptr oneway_receiver)
273 set_sync_scope_policy (Messaging::SYNC_WITH_SERVER);
275 Test::Shutdown_Helper_var shutdown_helper =
276 oneway_receiver->get_shutdown_helper ();
278 int had_comm_failure = 0;
279 for (int i = 0; i != iterations; ++i)
283 run_one_iteration (i, oneway_receiver, shutdown_helper.in ());
285 if (server_status == DESTROYED
286 || server_status == SHUTDOWN)
287 failed_calls++;
288 else
289 successful_calls++;
291 catch (const CORBA::NO_PERMISSION& ex)
293 call_failed (ex);
295 catch (const CORBA::OBJECT_NOT_EXIST& ex)
297 if (server_status == DESTROYED)
298 successful_calls++;
299 else
300 call_failed (ex);
302 catch (const CORBA::COMM_FAILURE& ex)
304 if (server_status == SHUTDOWN && had_comm_failure == 0)
305 successful_calls++;
306 else
307 call_failed (ex);
308 had_comm_failure = 1;
310 catch (const CORBA::TRANSIENT& ex)
312 if (server_status == SHUTDOWN)
313 successful_calls++;
314 else
315 call_failed (ex);
317 catch (const CORBA::SystemException& ex)
319 call_failed (ex);
324 /// Helper routine to run the sync_with_transport and sync_none tests.
325 void
326 run_test_unreliable (Test::Oneway_Receiver_ptr oneway_receiver);
328 void
329 run_test_sync_with_transport (Test::Oneway_Receiver_ptr oneway_receiver)
331 set_sync_scope_policy (Messaging::SYNC_WITH_TRANSPORT);
333 run_test_unreliable (oneway_receiver);
336 void
337 run_test_sync_none (Test::Oneway_Receiver_ptr oneway_receiver)
339 set_sync_scope_policy (Messaging::SYNC_NONE);
341 run_test_unreliable (oneway_receiver);
345 void
346 run_test_unreliable (Test::Oneway_Receiver_ptr oneway_receiver)
348 Test::Shutdown_Helper_var shutdown_helper =
349 oneway_receiver->get_shutdown_helper ();
351 int undetected = 0;
352 int had_transient = 0;
353 for (int i = 0; i != iterations; ++i)
357 run_one_iteration (i, oneway_receiver, shutdown_helper.in ());
359 // A few failures can go undetected...
360 if (server_status == SHUTDOWN)
361 undetected++;
363 // Once a TRANSIENT is raised all future calls should fail.
364 if (had_transient)
365 failed_calls++;
366 else
367 successful_calls++;
369 catch (const CORBA::NO_PERMISSION& ex)
371 call_failed (ex);
373 catch (const CORBA::OBJECT_NOT_EXIST& ex)
375 call_failed (ex);
377 catch (const CORBA::COMM_FAILURE& ex)
379 call_failed (ex);
381 catch (const CORBA::TRANSIENT& ex)
383 if (server_status == SHUTDOWN)
384 successful_calls++;
385 else
386 call_failed (ex);
387 had_transient = 1;
389 catch (const CORBA::SystemException& ex)
391 call_failed (ex);
394 ACE_DEBUG ((LM_DEBUG, "Undetected TRANSIENT count = %d\n",
395 undetected));