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 |
|
---|
11 | int 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 |
|
---|
59 | int test_read(const QString &aFile)
|
---|
60 | {
|
---|
61 | printf ("test_read: Read '%s' from child's stdout "
|
---|
62 | "and write it to '%s.out'\n", aFile.toLocal8Bit().constData(),
|
---|
63 | aFile.toLocal8Bit().constData());
|
---|
64 |
|
---|
65 | QString aOutFile = aFile + ".out";
|
---|
66 | QProcess p;
|
---|
67 | QFile f (aOutFile);
|
---|
68 |
|
---|
69 | if (f.open (QIODevice::WriteOnly))
|
---|
70 | {
|
---|
71 | p.start ("child",
|
---|
72 | QStringList() << "1" << aFile,
|
---|
73 | QIODevice::ReadOnly);
|
---|
74 |
|
---|
75 | if (p.waitForStarted())
|
---|
76 | {
|
---|
77 | while (p.waitForReadyRead())
|
---|
78 | {
|
---|
79 | QByteArray ba = p.read (p.bytesAvailable());
|
---|
80 |
|
---|
81 | f.write (ba);
|
---|
82 | }
|
---|
83 |
|
---|
84 | p.waitForFinished();
|
---|
85 | printf ("Process finished with %d (%d)\n",
|
---|
86 | p.exitCode(), p.exitStatus());
|
---|
87 | }
|
---|
88 | else
|
---|
89 | printf ("waitForStarted() failed\n");
|
---|
90 |
|
---|
91 | f.close();
|
---|
92 | }
|
---|
93 | else
|
---|
94 | printf ("Can't open output file %s\n",
|
---|
95 | aOutFile.toLatin1().constData());
|
---|
96 |
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 | int test_sleep()
|
---|
101 | {
|
---|
102 | QProcess p;
|
---|
103 |
|
---|
104 | p.start ("cmd",
|
---|
105 | QStringList() << "/c" << "sleep.cmd");
|
---|
106 |
|
---|
107 | if (p.waitForStarted())
|
---|
108 | {
|
---|
109 | p.waitForFinished();
|
---|
110 | printf ("Process finished with %d (%d)\n",
|
---|
111 | p.exitCode(), p.exitStatus());
|
---|
112 | }
|
---|
113 | else
|
---|
114 | printf ("waitForStarted() failed\n");
|
---|
115 |
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | int test_hang()
|
---|
120 | {
|
---|
121 | QProcess p;
|
---|
122 |
|
---|
123 | p.start ("cmd",
|
---|
124 | QStringList() << "/c" << "pause");
|
---|
125 |
|
---|
126 | if (p.waitForStarted())
|
---|
127 | {
|
---|
128 | p.waitForFinished();
|
---|
129 | printf ("Process finished with %d (%d)\n",
|
---|
130 | p.exitCode(), p.exitStatus());
|
---|
131 | }
|
---|
132 | else
|
---|
133 | printf ("waitForStarted() failed\n");
|
---|
134 |
|
---|
135 | return 0;
|
---|
136 | }
|
---|
137 |
|
---|
138 | int main( int argc, char *argv[] )
|
---|
139 | {
|
---|
140 | if (argc == 3)
|
---|
141 | {
|
---|
142 | if (qstrcmp (argv [1], "read") == 0)
|
---|
143 | return test_read (QLatin1String (argv [2]));
|
---|
144 | if (qstrcmp (argv [1], "write") == 0)
|
---|
145 | return test_write(QLatin1String (argv [2]));
|
---|
146 | }
|
---|
147 | else
|
---|
148 | if (argc == 2)
|
---|
149 | {
|
---|
150 | if (qstrcmp (argv [1], "sleep") == 0)
|
---|
151 | return test_sleep();
|
---|
152 | if (qstrcmp (argv [1], "hang") == 0)
|
---|
153 | return test_hang();
|
---|
154 | }
|
---|
155 |
|
---|
156 | printf ("Usage: testcase read|write <filename>\n"
|
---|
157 | " testcase sleep|hang\n");
|
---|
158 |
|
---|
159 | return 0;
|
---|
160 | }
|
---|