source: tests/process/process-sync/process-sync.cpp@ 925

Last change on this file since 925 was 925, checked in by Dmitry A. Kuminov, 14 years ago

tests/process: Added stderr load test.

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1/*
2 *
3 * Demostrates failure when piping a large ( +100KB ) file to a child process
4 *
5 */
6
7#include <QDebug>
8#include <QFile>
9#include <QProcess>
10
11int test_write(const QString &aFile)
12{
13 printf ("test_write: Write '%s' to child's stdin that will write "
14 "it to '%s.out'\n", aFile.toLocal8Bit().constData(),
15 aFile.toLocal8Bit().constData());
16
17 QProcess p;
18
19 QFile f (aFile);
20 if (f.open (QIODevice::ReadOnly))
21 {
22 QString aOutFile = aFile + ".out";
23
24 p.start ("child",
25 QStringList() << "0" << aOutFile,
26 QIODevice::WriteOnly);
27
28 if (p.waitForStarted())
29 {
30 while (!f.atEnd())
31 {
32 //QByteArray ba = f.read (2048);
33 QByteArray ba = f.read (32768);
34
35 p.write (ba);
36
37 p.waitForBytesWritten (-1);
38 }
39
40 //p.close();
41 p.closeWriteChannel();
42
43 p.waitForFinished();
44 printf ("Process finished with %d (%d)\n",
45 p.exitCode(), p.exitStatus());
46 }
47 else
48 printf ("waitForStarted() failed\n");
49
50 f.close();
51 }
52 else
53 printf ("Can't open output file %s\n",
54 aFile.toLatin1().constData());
55
56 return 0;
57}
58
59int test_read(const QString &aFile, bool aToStdErr)
60{
61 const char *ext = aToStdErr ? "err" : "out";
62
63 printf ("test_read: Read '%s' from child's std%s "
64 "and write it to '%s.%s'\n", aFile.toLocal8Bit().constData(),
65 ext, aFile.toLocal8Bit().constData(), ext);
66
67 QString aOutFile = aFile + "." + ext;
68 QProcess p;
69 QFile f (aOutFile);
70
71 p.setReadChannel (aToStdErr ? QProcess::StandardError :
72 QProcess::StandardOutput);
73
74 if (f.open (QIODevice::WriteOnly))
75 {
76 p.start ("child",
77 QStringList() << (aToStdErr ? "2" : "1") << aFile,
78 QIODevice::ReadOnly);
79
80 if (p.waitForStarted())
81 {
82 while (p.waitForReadyRead())
83 {
84 QByteArray ba = p.read (p.bytesAvailable());
85
86 f.write (ba);
87 }
88
89 p.waitForFinished();
90 printf ("Process finished with %d (%d)\n",
91 p.exitCode(), p.exitStatus());
92 }
93 else
94 printf ("waitForStarted() failed\n");
95
96 f.close();
97 }
98 else
99 printf ("Can't open output file %s\n",
100 aOutFile.toLatin1().constData());
101
102 return 0;
103}
104
105int test_sleep()
106{
107 printf ("test_sleep: start a 3s sleepy and wait for termination.\n");
108
109 QProcess p;
110
111 p.start ("cmd",
112 QStringList() << "/c" <<
113 "process-sync-helper.cmd" << "sleep");
114
115 if (p.waitForStarted())
116 {
117 p.waitForFinished();
118 printf ("Process finished with %d (%d)\n",
119 p.exitCode(), p.exitStatus());
120 }
121 else
122 printf ("waitForStarted() failed\n");
123
124 return 0;
125}
126
127int test_delayed_write()
128{
129 printf ("test_sleep: start a 3s delayed write to child's stdin "
130 "and wait until written.\n");
131
132 QProcess p;
133
134 p.start ("cmd",
135 QStringList() << "/c" <<
136 "process-sync-helper.cmd" << "delayed_write",
137 QIODevice::ReadWrite);
138
139 if (p.waitForStarted())
140 {
141 p.write (QByteArray("Hello from parent.\n"));
142 p.waitForBytesWritten();
143
144 if (p.waitForReadyRead()) {
145 QByteArray ba = p.read (p.bytesAvailable());
146 printf ("Child says:\n%s\n", ba.constData());
147 }
148
149 p.waitForFinished();
150 printf ("Process finished with %d (%d)\n",
151 p.exitCode(), p.exitStatus());
152 }
153 else
154 printf ("waitForStarted() failed\n");
155
156 return 0;
157}
158
159int test_delayed_read()
160{
161 printf ("test_sleep: start a 3s delayed read from child's stdout "
162 "and wait until read.\n");
163
164 QProcess p;
165
166 p.start ("cmd",
167 QStringList() << "/c" <<
168 "process-sync-helper.cmd" << "delayed_read",
169 QIODevice::ReadOnly);
170
171 if (p.waitForStarted())
172 {
173 if (p.waitForReadyRead()) {
174 QByteArray ba = p.read (p.bytesAvailable());
175 printf ("Child says:\n%s\n", ba.constData());
176 }
177
178 p.waitForFinished();
179 printf ("Process finished with %d (%d)\n",
180 p.exitCode(), p.exitStatus());
181 }
182 else
183 printf ("waitForStarted() failed\n");
184
185 return 0;
186}
187
188int test_hang()
189{
190 printf ("test_hang: start cmd /c pause and wait for termination. "
191 "Requires killing cmd.exe.\n");
192
193 QProcess p;
194
195 p.start ("cmd",
196 QStringList() << "/c" << "pause");
197
198 if (p.waitForStarted())
199 {
200 p.waitForFinished();
201 printf ("Process finished with %d (%d)\n",
202 p.exitCode(), p.exitStatus());
203 }
204 else
205 printf ("waitForStarted() failed\n");
206
207 return 0;
208}
209
210int main( int argc, char *argv[] )
211{
212 if (argc == 3)
213 {
214 if (qstrcmp (argv [1], "read_out") == 0)
215 return test_read (QLatin1String (argv [2]), false);
216 if (qstrcmp (argv [1], "read_err") == 0)
217 return test_read (QLatin1String (argv [2]), true);
218 if (qstrcmp (argv [1], "write") == 0)
219 return test_write(QLatin1String (argv [2]));
220 }
221 else
222 if (argc == 2)
223 {
224 if (qstrcmp (argv [1], "sleep") == 0)
225 return test_sleep();
226 if (qstrcmp (argv [1], "hang") == 0)
227 return test_hang();
228 if (qstrcmp (argv [1], "delayed_write") == 0)
229 return test_delayed_write();
230 if (qstrcmp (argv [1], "delayed_read") == 0)
231 return test_delayed_read();
232 }
233
234 printf ("Usage: testcase read_out|read_err|write <filename>\n"
235 " testcase sleep|hang\n");
236
237 return 0;
238}
Note: See TracBrowser for help on using the repository browser.