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

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

tests: Add testing sync process access on a GUI thread.

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