summaryrefslogtreecommitdiff
path: root/TAO/tests/Reliable_Oneways/client.cpp
blob: a49e15b2807ff26b46af19970a72fc865e4044cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// $Id$

#include "TestC.h"
#include "tao/Messaging.h"
#include "ace/Get_Opt.h"

ACE_RCSID(Reliable_Oneways, client, "$Id$")

const char *ior = "file://test.ior";
int test_sync_with_target    = 0;
int test_sync_with_server    = 0;
int test_sync_with_transport = 0;
int test_sync_none           = 0;
int iterations = 500;

const int ALIVE     = 0;
const int DESTROYED = 1;
const int SHUTDOWN  = 2;
int server_status = ALIVE;

int successful_calls = 0;
int failed_calls = 0;

int
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "k:i:ntsr");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'k':
        ior = get_opts.opt_arg ();
        break;

      case 'i':
        iterations = ACE_OS::atoi (get_opts.opt_arg ());
        break;

      case 'n':
        test_sync_none = 1;
        break;

      case 't':
        test_sync_with_transport = 1;
        break;

      case 's':
        test_sync_with_server = 1;
        break;

      case 'r':
        test_sync_with_target = 1;
        break;

      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-k <ior> "
                           "-n -t -s -r "
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates successful parsing of the command line
  return 0;
}

void run_test_sync_with_target (Test::Oneway_Receiver_ptr oneway_receiver
                                ACE_ENV_ARG_DECL);
void run_test_sync_with_server (Test::Oneway_Receiver_ptr oneway_receiver
                                ACE_ENV_ARG_DECL);
void run_test_sync_with_transport (Test::Oneway_Receiver_ptr oneway_receiver
                                   ACE_ENV_ARG_DECL);
void run_test_sync_none (Test::Oneway_Receiver_ptr oneway_receiver
                         ACE_ENV_ARG_DECL);


int
main (int argc, char *argv[])
{
  ACE_TRY_NEW_ENV
    {
      CORBA::ORB_var orb =
        CORBA::ORB_init (argc, argv, "" ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (parse_args (argc, argv) != 0)
        return 1;

      CORBA::Object_var tmp =
        orb->string_to_object(ior ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      Test::Oneway_Receiver_var oneway_receiver =
        Test::Oneway_Receiver::_narrow(tmp.in () ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (oneway_receiver.in ()))
        {
          ACE_ERROR_RETURN ((LM_DEBUG,
                             "Nil oneway receiver reference <%s>\n",
                             ior),
                            1);
        }

      if (test_sync_with_target)
        {
          ACE_DEBUG ((LM_DEBUG, "Running SYNC_WITH_TARGET\n"));
          run_test_sync_with_target (oneway_receiver.in ()
                                     ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
      else if (test_sync_with_server)
        {
          ACE_DEBUG ((LM_DEBUG, "Running SYNC_WITH_SERVER\n"));
          run_test_sync_with_server (oneway_receiver.in ()
                                     ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
      else if (test_sync_with_transport)
        {
          ACE_DEBUG ((LM_DEBUG, "Running SYNC_WITH_TRANSPORT\n"));
          run_test_sync_with_transport (oneway_receiver.in ()
                                        ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
      else
        {
          ACE_DEBUG ((LM_DEBUG, "Running SYNC_NONE\n"));
          run_test_sync_none (oneway_receiver.in ()
                              ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }

      if (successful_calls == 0)
        ACE_ERROR ((LM_ERROR, "ERROR: No requests were successful\n"));

      if (failed_calls != 0)
        ACE_ERROR ((LM_ERROR, "ERROR: At least %d requests failed\n",
                    failed_calls));

      if (failed_calls + successful_calls != iterations)
        ACE_ERROR ((LM_ERROR,
                    "ERROR: Mismatched number of calls (%d + %d != %d)\n",
                    successful_calls, failed_calls, iterations));

      orb->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception caught:");
      return 1;
    }
  ACE_ENDTRY;

  return 0;
}

void
set_sync_scope_policy (Messaging::SyncScope sync_scope
                       ACE_ENV_ARG_DECL)
{
  int argc = 0;
  CORBA::ORB_var orb = CORBA::ORB_init (argc, 0, "" ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::Object_var object =
    orb->resolve_initial_references ("PolicyCurrent" ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::PolicyCurrent_var policy_current =
    CORBA::PolicyCurrent::_narrow (object.in () ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (CORBA::is_nil (policy_current.in ()))
    {
      ACE_ERROR ((LM_ERROR, "ERROR: Nil policy current\n"));
      return;
    }
  CORBA::Any scope_as_any;
  scope_as_any <<= sync_scope;

  CORBA::PolicyList policies(1); policies.length (1);
  policies[0] =
    orb->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
                        scope_as_any
                        ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  policy_current->set_policy_overrides (policies, CORBA::ADD_OVERRIDE
                                        ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  policies[0]->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}

void
run_one_iteration (int i,
                   Test::Oneway_Receiver_ptr oneway_receiver,
                   Test::Shutdown_Helper_ptr shutdown_helper
                   ACE_ENV_ARG_DECL)
{
  ACE_TRY
    {
      if (i == iterations / 3)
        {
          ACE_DEBUG ((LM_DEBUG, "Destroying object\n"));
          server_status = DESTROYED;
          oneway_receiver->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
      if (i == 2 * iterations / 3)
        {
          ACE_DEBUG ((LM_DEBUG, "Shutting down server process\n"));
          server_status = SHUTDOWN;
          shutdown_helper->shutdown (ACE_ENV_SINGLE_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
    }
  ACE_CATCHANY
    {
    }
  ACE_ENDTRY;

  oneway_receiver->raise_no_permission (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}

/// Helper routine to set breakpoints during failures
void
call_failed (const CORBA::SystemException &ex)
{
  failed_calls++;

  ACE_PRINT_EXCEPTION (ex, "Exception raised");
}

void
run_test_sync_with_target (Test::Oneway_Receiver_ptr oneway_receiver
                           ACE_ENV_ARG_DECL)
{
  set_sync_scope_policy (Messaging::SYNC_WITH_TARGET ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  Test::Shutdown_Helper_var shutdown_helper =
    oneway_receiver->get_shutdown_helper (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  int had_comm_failure = 0;
  for (int i = 0; i != iterations; ++i)
    {
      ACE_TRY
        {
          run_one_iteration (i, oneway_receiver, shutdown_helper.in ()
                             ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
      ACE_CATCH (CORBA::NO_PERMISSION, ex)
        {
          if (server_status == ALIVE)
            successful_calls++;
          else
            call_failed (ex);
        }
      ACE_CATCH (CORBA::OBJECT_NOT_EXIST, ex)
        {
          if (server_status == DESTROYED)
            successful_calls++;
          else
            call_failed (ex);
        }
      ACE_CATCH (CORBA::COMM_FAILURE, ex)
        {
          if (server_status == SHUTDOWN && had_comm_failure == 0)
            successful_calls++;
          else
            call_failed (ex);
          had_comm_failure = 1;
        }
      ACE_CATCH (CORBA::TRANSIENT, ex)
        {
          if (server_status == SHUTDOWN)
            successful_calls++;
          else
            call_failed (ex);
        }
      ACE_CATCH (CORBA::SystemException, ex)
        {
          call_failed (ex);
        }
      ACE_ENDTRY;
    }
}

void
run_test_sync_with_server (Test::Oneway_Receiver_ptr oneway_receiver
                           ACE_ENV_ARG_DECL)
{
  set_sync_scope_policy (Messaging::SYNC_WITH_SERVER ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  Test::Shutdown_Helper_var shutdown_helper =
    oneway_receiver->get_shutdown_helper (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  int had_comm_failure = 0;
  for (int i = 0; i != iterations; ++i)
    {
      ACE_TRY
        {
          run_one_iteration (i, oneway_receiver, shutdown_helper.in ()
                             ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          if (server_status == DESTROYED
              || server_status == SHUTDOWN)
            failed_calls++;
          else
            successful_calls++;
        }
      ACE_CATCH (CORBA::NO_PERMISSION, ex)
        {
          call_failed (ex);
        }
      ACE_CATCH (CORBA::OBJECT_NOT_EXIST, ex)
        {
          if (server_status == DESTROYED)
            successful_calls++;
          else
            call_failed (ex);
        }
      ACE_CATCH (CORBA::COMM_FAILURE, ex)
        {
          if (server_status == SHUTDOWN && had_comm_failure == 0)
            successful_calls++;
          else
            call_failed (ex);
          had_comm_failure = 1;
        }
      ACE_CATCH (CORBA::TRANSIENT, ex)
        {
          if (server_status == SHUTDOWN)
            successful_calls++;
          else
            call_failed (ex);
        }
      ACE_CATCH (CORBA::SystemException, ex)
        {
          call_failed (ex);
        }
      ACE_ENDTRY;
    }
}

/// Helper routine to run the sync_with_transport and sync_none tests.
void
run_test_unreliable (Test::Oneway_Receiver_ptr oneway_receiver
                     ACE_ENV_ARG_DECL);

void
run_test_sync_with_transport (Test::Oneway_Receiver_ptr oneway_receiver
                              ACE_ENV_ARG_DECL)
{
  set_sync_scope_policy (Messaging::SYNC_WITH_TRANSPORT ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  run_test_unreliable (oneway_receiver ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
}

void
run_test_sync_none (Test::Oneway_Receiver_ptr oneway_receiver
                    ACE_ENV_ARG_DECL)
{
  set_sync_scope_policy (Messaging::SYNC_NONE ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  run_test_unreliable (oneway_receiver ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
}


void
run_test_unreliable (Test::Oneway_Receiver_ptr oneway_receiver
                     ACE_ENV_ARG_DECL)
{
  Test::Shutdown_Helper_var shutdown_helper =
    oneway_receiver->get_shutdown_helper (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  int undetected = 0;
  int had_transient = 0;
  for (int i = 0; i != iterations; ++i)
    {
      ACE_TRY
        {
          run_one_iteration (i, oneway_receiver, shutdown_helper.in ()
                             ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          // A few failures can go undetected...
          if (server_status == SHUTDOWN)
            undetected++;

          // Once a TRANSIENT is raised all future calls should fail.
          if (had_transient)
            failed_calls++;
          else
            successful_calls++;
        }
      ACE_CATCH (CORBA::NO_PERMISSION, ex)
        {
          call_failed (ex);
        }
      ACE_CATCH (CORBA::OBJECT_NOT_EXIST, ex)
        {
          call_failed (ex);
        }
      ACE_CATCH (CORBA::COMM_FAILURE, ex)
        {
          call_failed (ex);
        }
      ACE_CATCH (CORBA::TRANSIENT, ex)
        {
          if (server_status == SHUTDOWN)
            successful_calls++;
          else
            call_failed (ex);
          had_transient = 1;
        }
      ACE_CATCH (CORBA::SystemException, ex)
        {
          call_failed (ex);
        }
      ACE_ENDTRY;
    }
  ACE_DEBUG ((LM_DEBUG, "Undetected TRANSIENT count = %d\n",
              undetected));
}