Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Pipe_Test.cpp
blob5cb4b10e1b407bbff0ad8472c27f9b70df6f86d8
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 ()
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 ACE_Process_Options options;
117 # ifndef ACE_LACKS_VA_FUNCTIONS
118 options.command_line (ACE_TEXT ("%") ACE_TEXT_PRIs
119 ACE_TEXT (" -c%") ACE_TEXT_PRIs,
120 argc > 0 ? argv[0] : ACE_TEXT ("Pipe_Test"),
121 close_pipe == 0 ? ACE_TEXT (" -d") : ACE_TEXT (""));
122 # else
123 ACE_UNUSED_ARG (cmdline_fmt);
124 # endif
126 ACE_exitcode status = 0;
128 for (int i = 0; i < ::iterations; i++)
130 ACE_Process server;
132 if (server.spawn (options) == -1)
134 ACE_ERROR_RETURN ((LM_ERROR,
135 ACE_TEXT ("%p\n"),
136 ACE_TEXT ("spawn failed")),
137 -1);
139 else
141 ACE_DEBUG ((LM_DEBUG,
142 ACE_TEXT ("Server forked with pid = %d.\n"),
143 server.getpid ()));
146 // Wait for the process we just created to exit.
147 server.wait (&status);
149 // Check if child exited without error.
150 if (WIFEXITED (status) != 0
151 && WEXITSTATUS (status) != 0)
153 ACE_ERROR ((LM_ERROR,
154 ACE_TEXT ("Child of server %d finished with error ")
155 ACE_TEXT ("exit status %d\n"),
156 server.getpid (),
157 WEXITSTATUS (status)));
159 ACE_END_TEST;
161 ACE_OS::exit (WEXITSTATUS (status));
164 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Server %d finished\n"),
165 server.getpid ()));
167 ACE_END_TEST;
169 #endif // ACE_HAS_PROCESS_SPAWN
170 return 0;