summaryrefslogtreecommitdiff
path: root/tests/Process_Manual_Event_Test.cpp
blob: bd7255912f2a89f406ea1098b98d6ce2a2547131 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Process_Manual_Event_Test.cpp
//
// = DESCRIPTION
//    This test verifies the functionality of the <ACE_Manual_Event>
//    process-shared implementation.
//
// = AUTHOR
//    Martin Corino <mcorino@remedy.nl>
//
// ============================================================================

#include "test_config.h"
#include "ace/Process.h"
#include "ace/Manual_Event.h"
#include "ace/Time_Value.h"
#include "ace/Get_Opt.h"
#include "ace/ACE.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/os_include/os_dirent.h"


#if (!defined (ACE_LACKS_FORK) || defined (ACE_WIN32)) && \
  (defined (ACE_WIN32) || \
   (defined (ACE_HAS_PTHREADS) && defined (_POSIX_THREAD_PROCESS_SHARED) && \
      !defined (ACE_LACKS_MUTEXATTR_PSHARED) && !defined (ACE_LACKS_CONDATTR_PSHARED)) || \
   defined (ACE_USES_FIFO_SEM) || \
   (defined (ACE_HAS_POSIX_SEM) && defined (ACE_HAS_POSIX_SEM_TIMEOUT) && !defined (ACE_LACKS_NAMED_POSIX_SEM)))
static int iterations = 10;
static int child_process = 0;
static const ACE_TCHAR *event_ping_name = ACE_TEXT ("ACE_Ping_Event");
static const ACE_TCHAR *event_pong_name = ACE_TEXT ("ACE_Pong_Event");

// Explain usage and exit.
static void
print_usage_and_die (void)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("usage: %n [-i #iterations] [-c (child process)]\n")));
  ACE_OS::exit (1);
}

// Parse the command-line arguments and set options.
static void
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Arg_Opt<ACE_TCHAR>  get_opt (argc, argv, ACE_TEXT ("i:c"));

  int c;

  while ((c = get_opt ()) != -1)
    switch (c)
      {
      case 'i':
        iterations = ACE_OS::atoi (get_opt.opt_arg ());
        break;
      case 'c':
        child_process = 1;
        break;
      default:
        print_usage_and_die ();
        break;
      }
}

static void
acquire_release (void)
{
  ACE_Manual_Event event_ping (0, USYNC_PROCESS, event_ping_name);
  ACE_Manual_Event event_pong (0, USYNC_PROCESS, event_pong_name);

  // Make sure the constructor succeeded
  ACE_ASSERT (ACE_LOG_MSG->op_status () == 0);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P) Begin ping-pong\n")));

  int i;
  if (child_process)
    {
      for (i = 0; i < iterations; ++i)
        {
          event_ping.signal ();

          if (event_pong.wait ())
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("(%P) %p\n"),
                        ACE_TEXT ("Failed acquiring pong")));
          else
            {
              ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P) Pong\n")));
              event_pong.reset ();
            }
        }

      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P) Testing timeouts\n")));

      // test timed wait
      ACE_Time_Value wait = ACE_OS::gettimeofday ();
      wait.sec (wait.sec () + 3); // timeout in 3 secs

      if (event_pong.wait (&wait))
        {
          if (errno != ETIME)
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("(%P) %p, but expected ETIME\n"),
                        ACE_TEXT ("event_pong.wait()")));
        }
      else
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("(%P) Acquired pong without release()\n")));

      event_ping.signal ();   // release waiting parent before timeout
    }
  else
    {
      for (i = 0; i < iterations; ++i)
        {
          if (event_ping.wait ())
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("(%P) %p\n"),
                        ACE_TEXT ("Failed acquiring ping")));
          else
            {
              ACE_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P) Ping\n")));
              event_ping.reset ();
            }

          event_pong.signal ();
        }

      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P) Testing timeouts\n")));

      // test timed wait
      ACE_Time_Value wait = ACE_OS::gettimeofday ();
      wait.sec (wait.sec () + 10); // timeout in 10 secs

      if (event_ping.wait (&wait))
        {
          if (errno != ETIME)
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("(%P) %p but should be ETIME\n"),
                        ACE_TEXT ("Acquire pong")));

          ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P) Acquire pong timed out\n")));
        }
    }
}
#endif /* ! ACE_LACKS_FORK */

int
run_main (int argc, ACE_TCHAR *argv[])
{
#if defined (ACE_LACKS_FORK) && !defined (ACE_WIN32)
  ACE_UNUSED_ARG (argc);
  ACE_UNUSED_ARG (argv);

  ACE_START_TEST (ACE_TEXT ("Process_Manual_Event_Test"));
  ACE_ERROR ((LM_INFO,
              ACE_TEXT ("fork is not supported on this platform\n")));
  ACE_END_TEST;
#elif defined (ACE_WIN32) || \
       (defined (ACE_HAS_PTHREADS) && defined (_POSIX_THREAD_PROCESS_SHARED) && \
          !defined (ACE_LACKS_MUTEXATTR_PSHARED) && !defined (ACE_LACKS_CONDATTR_PSHARED)) || \
       defined (ACE_USES_FIFO_SEM) || \
       (defined (ACE_HAS_POSIX_SEM) && defined (ACE_HAS_POSIX_SEM_TIMEOUT) && !defined (ACE_LACKS_NAMED_POSIX_SEM))

  parse_args (argc, argv);

  // Child process code.
  if (child_process)
    {
      ACE_START_TEST (ACE_TEXT ("Process_Manual_Event_Test-child"));
      acquire_release ();
      ACE_END_LOG;
    }
  else
    {
      ACE_START_TEST (ACE_TEXT ("Process_Manual_Event_Test"));
#if defined (ACE_WIN32)
      const ACE_TCHAR *cmdline_format = ACE_TEXT("\"%s\" -c -i %d");
#elif !defined (ACE_USES_WCHAR)
const ACE_TCHAR *cmdline_format = ACE_TEXT (".") ACE_DIRECTORY_SEPARATOR_STR ACE_TEXT("%s -c -i %d");
#else
const ACE_TCHAR *cmdline_format = ACE_TEXT (".") ACE_DIRECTORY_SEPARATOR_STR ACE_TEXT("%ls -c -i %d");
#endif

      ACE_Process_Options options;
      options.command_line (cmdline_format,
                            argv[0],
                            iterations);
      // Spawn a child process that will contend for the
      // lock.
      ACE_Process child;

      // Spawn the child process.
      int result = child.spawn (options);
      ACE_ASSERT (result != -1);
      ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("Parent spawned child process with pid = %d.\n"),
                child.getpid ()));

      // start test
      acquire_release ();

      ACE_exitcode child_status;
      // Wait for the child processes we created to exit.
      int wait_result = child.wait (&child_status);
      ACE_ASSERT (wait_result != -1);
      if (child_status == 0)
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Child %d finished ok\n"),
                    child.getpid ()));
      else
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Child %d finished with status %d\n"),
                    child.getpid (), child_status));
      ACE_END_TEST;
    }
#else /* !ACE_LACKS_FORK */
  ACE_UNUSED_ARG (argc);
  ACE_UNUSED_ARG (argv);

  ACE_START_TEST (ACE_TEXT ("Process_Manual_Event_Test"));
  ACE_ERROR ((LM_INFO,
              ACE_TEXT ("Process shared events are not supported on this platform\n")));
  ACE_END_TEST;
#endif /* ! ACE_LACKS_FORK  */

  return 0;
}