/* * * Demostrates failure when piping a large ( +100KB ) file to a child process * */ #include #include #include int test_write(const QString &aFile) { printf ("test_write: Write '%s' to child's stdin that will write " "it to '%s.out'\n", aFile.toLocal8Bit().constData(), aFile.toLocal8Bit().constData()); QProcess p; QFile f (aFile); if (f.open (QIODevice::ReadOnly)) { QString aOutFile = aFile + ".out"; p.start ("child", QStringList() << "0" << aOutFile, QIODevice::WriteOnly); if (p.waitForStarted()) { while (!f.atEnd()) { //QByteArray ba = f.read (2048); QByteArray ba = f.read (32768); p.write (ba); p.waitForBytesWritten (-1); } //p.close(); p.closeWriteChannel(); p.waitForFinished(); printf ("Process finished with %d (%d)\n", p.exitCode(), p.exitStatus()); } else printf ("waitForStarted() failed\n"); f.close(); } else printf ("Can't open output file %s\n", aFile.toLatin1().constData()); return 0; } int test_read(const QString &aFile) { printf ("test_read: Read '%s' from child's stdout " "and write it to '%s.out'\n", aFile.toLocal8Bit().constData(), aFile.toLocal8Bit().constData()); QString aOutFile = aFile + ".out"; QProcess p; QFile f (aOutFile); if (f.open (QIODevice::WriteOnly)) { p.start ("child", QStringList() << "1" << aFile, QIODevice::ReadOnly); if (p.waitForStarted()) { while (p.waitForReadyRead()) { QByteArray ba = p.read (p.bytesAvailable()); f.write (ba); } p.waitForFinished(); printf ("Process finished with %d (%d)\n", p.exitCode(), p.exitStatus()); } else printf ("waitForStarted() failed\n"); f.close(); } else printf ("Can't open output file %s\n", aOutFile.toLatin1().constData()); return 0; } int test_sleep() { printf ("test_sleep: start a 3s sleepy and wait for termination.\n"); QProcess p; p.start ("cmd", QStringList() << "/c" << "process-sync-helper.cmd" << "sleep"); if (p.waitForStarted()) { p.waitForFinished(); printf ("Process finished with %d (%d)\n", p.exitCode(), p.exitStatus()); } else printf ("waitForStarted() failed\n"); return 0; } int test_delayed_write() { printf ("test_sleep: start a 3s delayed write to child's stdin " "and wait until written.\n"); QProcess p; p.start ("cmd", QStringList() << "/c" << "process-sync-helper.cmd" << "delayed_write", QIODevice::ReadWrite); if (p.waitForStarted()) { p.write (QByteArray("Hello from parent.\n")); p.waitForBytesWritten(); if (p.waitForReadyRead()) { QByteArray ba = p.read (p.bytesAvailable()); printf ("Child says:\n%s\n", ba.constData()); } p.waitForFinished(); printf ("Process finished with %d (%d)\n", p.exitCode(), p.exitStatus()); } else printf ("waitForStarted() failed\n"); return 0; } int test_delayed_read() { printf ("test_sleep: start a 3s delayed read from child's stdout " "and wait until read.\n"); QProcess p; p.start ("cmd", QStringList() << "/c" << "process-sync-helper.cmd" << "delayed_read", QIODevice::ReadOnly); if (p.waitForStarted()) { if (p.waitForReadyRead()) { QByteArray ba = p.read (p.bytesAvailable()); printf ("Child says:\n%s\n", ba.constData()); } p.waitForFinished(); printf ("Process finished with %d (%d)\n", p.exitCode(), p.exitStatus()); } else printf ("waitForStarted() failed\n"); return 0; } int test_hang() { printf ("test_hang: start cmd /c pause and wait for termination. " "Requires killing cmd.exe.\n"); QProcess p; p.start ("cmd", QStringList() << "/c" << "pause"); if (p.waitForStarted()) { p.waitForFinished(); printf ("Process finished with %d (%d)\n", p.exitCode(), p.exitStatus()); } else printf ("waitForStarted() failed\n"); return 0; } int main( int argc, char *argv[] ) { if (argc == 3) { if (qstrcmp (argv [1], "read") == 0) return test_read (QLatin1String (argv [2])); if (qstrcmp (argv [1], "write") == 0) return test_write(QLatin1String (argv [2])); } else if (argc == 2) { if (qstrcmp (argv [1], "sleep") == 0) return test_sleep(); if (qstrcmp (argv [1], "hang") == 0) return test_hang(); if (qstrcmp (argv [1], "delayed_write") == 0) return test_delayed_write(); if (qstrcmp (argv [1], "delayed_read") == 0) return test_delayed_read(); } printf ("Usage: testcase read|write \n" " testcase sleep|hang\n"); return 0; }