Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Pipe_Test.cpp
blobb98d33d37e3bab026feb622933a842715db614b6
2 //=============================================================================
3 /**
4 * @file Pipe_Test.cpp
6 * Tests the construction of multiple pipes in a process.
8 * @author Irfan Pyarali <irfan@cs.wustl.edu>
9 */
10 //=============================================================================
13 #include "test_config.h"
14 #include "ace/Pipe.h"
15 #include "ace/Process.h"
16 #include "ace/Get_Opt.h"
17 #include "ace/ACE.h"
18 #include "ace/OS_NS_stdio.h"
19 #include "ace/OS_NS_stdlib.h"
20 #include "ace/OS_NS_unistd.h"
22 // Indicates whether we should close the pipe or not.
23 static int close_pipe = 1;
25 // Indicates whether we're running as the child or the parent.
26 static int child_process = 0;
28 // Number of iterations to run the test.
29 static int iterations = ACE_MAX_ITERATIONS;
31 // Explain usage and exit.
32 static void
33 print_usage_and_die (void)
35 ACE_DEBUG ((LM_DEBUG,
36 ACE_TEXT ("usage: %n [-d (don't close pipes)] ")
37 ACE_TEXT ("[-c (child process)] [-i (iterations)]\n")));
38 ACE_OS::exit (1);
41 // Parse the command-line arguments and set options.
43 static void
44 parse_args (int argc, ACE_TCHAR *argv[])
46 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("dci:"));
48 int c;
50 while ((c = get_opt ()) != -1)
51 switch (c)
53 case 'd':
54 close_pipe = 0;
55 break;
56 case 'c':
57 child_process = 1;
58 break;
59 case 'i':
60 iterations = ACE_OS::atoi (get_opt.opt_arg ());
61 break;
62 default:
63 print_usage_and_die ();
64 break;
68 // Consolidate the ACE_Pipe initializations.
70 #ifdef ACE_HAS_PROCESS_SPAWN
71 static void
72 open_pipe (ACE_Pipe &pipe,
73 const char *name)
75 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("opening %C\n"), name));
76 int result = pipe.open ();
78 ACE_TEST_ASSERT (result != -1);
79 result = pipe.read_handle () != ACE_INVALID_HANDLE
80 && pipe.write_handle () != ACE_INVALID_HANDLE;
81 ACE_TEST_ASSERT (result == 1);
83 if (close_pipe)
84 pipe.close ();
86 #endif
88 int
89 run_main (int argc, ACE_TCHAR *argv[])
91 parse_args (argc, argv);
93 #ifndef ACE_HAS_PROCESS_SPAWN
94 ACE_START_TEST (ACE_TEXT ("Pipe_Test"));
95 ACE_END_TEST;
96 #else
98 if (child_process)
100 ACE_APPEND_LOG (ACE_TEXT("Pipe_Test-children"));
101 ACE_Pipe a, b, c, d, e;
103 open_pipe (a, "a");
104 open_pipe (b, "b");
105 open_pipe (c, "c");
106 open_pipe (d, "d");
107 open_pipe (e, "e");
109 ACE_END_LOG;
111 else
113 ACE_START_TEST (ACE_TEXT("Pipe_Test"));
114 ACE_INIT_LOG (ACE_TEXT("Pipe_Test-children"));
116 # if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
117 const ACE_TCHAR *cmdline_fmt = ACE_TEXT ("%s -c%s");
118 # else
119 const ACE_TCHAR *cmdline_fmt = ACE_TEXT ("%ls -c%ls");
120 # endif /* ACE_WIN32 || !ACE_USES_WCHAR */
121 ACE_Process_Options options;
122 # ifndef ACE_LACKS_VA_FUNCTIONS
123 options.command_line (cmdline_fmt,
124 argc > 0 ? argv[0] : ACE_TEXT ("Pipe_Test"),
125 close_pipe == 0 ? ACE_TEXT (" -d") : ACE_TEXT (""));
126 # else
127 ACE_UNUSED_ARG (cmdline_fmt);
128 # endif
130 ACE_exitcode status = 0;
132 for (int i = 0; i < ::iterations; i++)
134 ACE_Process server;
136 if (server.spawn (options) == -1)
138 ACE_ERROR_RETURN ((LM_ERROR,
139 ACE_TEXT ("%p\n"),
140 ACE_TEXT ("spawn failed")),
141 -1);
143 else
145 ACE_DEBUG ((LM_DEBUG,
146 ACE_TEXT ("Server forked with pid = %d.\n"),
147 server.getpid ()));
150 // Wait for the process we just created to exit.
151 server.wait (&status);
153 // Check if child exited without error.
154 if (WIFEXITED (status) != 0
155 && WEXITSTATUS (status) != 0)
157 ACE_ERROR ((LM_ERROR,
158 ACE_TEXT ("Child of server %d finished with error ")
159 ACE_TEXT ("exit status %d\n"),
160 server.getpid (),
161 WEXITSTATUS (status)));
163 ACE_END_TEST;
165 ACE_OS::exit (WEXITSTATUS (status));
168 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Server %d finished\n"),
169 server.getpid ()));
171 ACE_END_TEST;
173 #endif // ACE_HAS_PROCESS_SPAWN
174 return 0;