| 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 | printf ("test_sleep: start a 3s sleepy and wait for termination.\n");
|
|---|
| 103 |
|
|---|
| 104 | QProcess p;
|
|---|
| 105 |
|
|---|
| 106 | p.start ("cmd",
|
|---|
| 107 | QStringList() << "/c" <<
|
|---|
| 108 | "process-sync-helper.cmd" << "sleep");
|
|---|
| 109 |
|
|---|
| 110 | if (p.waitForStarted())
|
|---|
| 111 | {
|
|---|
| 112 | p.waitForFinished();
|
|---|
| 113 | printf ("Process finished with %d (%d)\n",
|
|---|
| 114 | p.exitCode(), p.exitStatus());
|
|---|
| 115 | }
|
|---|
| 116 | else
|
|---|
| 117 | printf ("waitForStarted() failed\n");
|
|---|
| 118 |
|
|---|
| 119 | return 0;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | int test_delayed_write()
|
|---|
| 123 | {
|
|---|
| 124 | printf ("test_sleep: start a 3s delayed write to child's stdin "
|
|---|
| 125 | "and wait until written.\n");
|
|---|
| 126 |
|
|---|
| 127 | QProcess p;
|
|---|
| 128 |
|
|---|
| 129 | p.start ("cmd",
|
|---|
| 130 | QStringList() << "/c" <<
|
|---|
| 131 | "process-sync-helper.cmd" << "delayed_write",
|
|---|
| 132 | QIODevice::ReadWrite);
|
|---|
| 133 |
|
|---|
| 134 | if (p.waitForStarted())
|
|---|
| 135 | {
|
|---|
| 136 | p.write (QByteArray("Hello from parent.\n"));
|
|---|
| 137 | p.waitForBytesWritten();
|
|---|
| 138 |
|
|---|
| 139 | if (p.waitForReadyRead()) {
|
|---|
| 140 | QByteArray ba = p.read (p.bytesAvailable());
|
|---|
| 141 | printf ("Child says:\n%s\n", ba.constData());
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | p.waitForFinished();
|
|---|
| 145 | printf ("Process finished with %d (%d)\n",
|
|---|
| 146 | p.exitCode(), p.exitStatus());
|
|---|
| 147 | }
|
|---|
| 148 | else
|
|---|
| 149 | printf ("waitForStarted() failed\n");
|
|---|
| 150 |
|
|---|
| 151 | return 0;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | int test_delayed_read()
|
|---|
| 155 | {
|
|---|
| 156 | printf ("test_sleep: start a 3s delayed read from child's stdout "
|
|---|
| 157 | "and wait until read.\n");
|
|---|
| 158 |
|
|---|
| 159 | QProcess p;
|
|---|
| 160 |
|
|---|
| 161 | p.start ("cmd",
|
|---|
| 162 | QStringList() << "/c" <<
|
|---|
| 163 | "process-sync-helper.cmd" << "delayed_read",
|
|---|
| 164 | QIODevice::ReadOnly);
|
|---|
| 165 |
|
|---|
| 166 | if (p.waitForStarted())
|
|---|
| 167 | {
|
|---|
| 168 | if (p.waitForReadyRead()) {
|
|---|
| 169 | QByteArray ba = p.read (p.bytesAvailable());
|
|---|
| 170 | printf ("Child says:\n%s\n", ba.constData());
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | p.waitForFinished();
|
|---|
| 174 | printf ("Process finished with %d (%d)\n",
|
|---|
| 175 | p.exitCode(), p.exitStatus());
|
|---|
| 176 | }
|
|---|
| 177 | else
|
|---|
| 178 | printf ("waitForStarted() failed\n");
|
|---|
| 179 |
|
|---|
| 180 | return 0;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | int test_hang()
|
|---|
| 184 | {
|
|---|
| 185 | printf ("test_hang: start cmd /c pause and wait for termination. "
|
|---|
| 186 | "Requires killing cmd.exe.\n");
|
|---|
| 187 |
|
|---|
| 188 | QProcess p;
|
|---|
| 189 |
|
|---|
| 190 | p.start ("cmd",
|
|---|
| 191 | QStringList() << "/c" << "pause");
|
|---|
| 192 |
|
|---|
| 193 | if (p.waitForStarted())
|
|---|
| 194 | {
|
|---|
| 195 | p.waitForFinished();
|
|---|
| 196 | printf ("Process finished with %d (%d)\n",
|
|---|
| 197 | p.exitCode(), p.exitStatus());
|
|---|
| 198 | }
|
|---|
| 199 | else
|
|---|
| 200 | printf ("waitForStarted() failed\n");
|
|---|
| 201 |
|
|---|
| 202 | return 0;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | int main( int argc, char *argv[] )
|
|---|
| 206 | {
|
|---|
| 207 | if (argc == 3)
|
|---|
| 208 | {
|
|---|
| 209 | if (qstrcmp (argv [1], "read") == 0)
|
|---|
| 210 | return test_read (QLatin1String (argv [2]));
|
|---|
| 211 | if (qstrcmp (argv [1], "write") == 0)
|
|---|
| 212 | return test_write(QLatin1String (argv [2]));
|
|---|
| 213 | }
|
|---|
| 214 | else
|
|---|
| 215 | if (argc == 2)
|
|---|
| 216 | {
|
|---|
| 217 | if (qstrcmp (argv [1], "sleep") == 0)
|
|---|
| 218 | return test_sleep();
|
|---|
| 219 | if (qstrcmp (argv [1], "hang") == 0)
|
|---|
| 220 | return test_hang();
|
|---|
| 221 | if (qstrcmp (argv [1], "delayed_write") == 0)
|
|---|
| 222 | return test_delayed_write();
|
|---|
| 223 | if (qstrcmp (argv [1], "delayed_read") == 0)
|
|---|
| 224 | return test_delayed_read();
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | printf ("Usage: testcase read|write <filename>\n"
|
|---|
| 228 | " testcase sleep|hang\n");
|
|---|
| 229 |
|
|---|
| 230 | return 0;
|
|---|
| 231 | }
|
|---|