diff options
author | Jo Asplin <jo.asplin@nokia.com> | 2012-06-01 12:49:12 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-06-04 10:29:53 +0200 |
commit | 5a57e529e03ad92f33ae8de4a154960649e44fad (patch) | |
tree | df8c0e45df5ecca4efbbed2b15aceb65b1b095e8 /tests | |
parent | d756da3214b372df92c987a8152dc3f10a93580b (diff) |
Refactoring and bug fixes in TestHelper
This patch contains the following improvements in the code that launches the test daemon:
- Introducing a _helper function to eliminate code duplication in launchJsonDbDaemon()
and launchJsonDbDaemonDetached()
- Fixing two bugs when launching the test daemon in detached mode:
- valgrind didn't work
- a null-pointer access would have happened if the working directory was explicitly set
Change-Id: I761ecfb2833d79263bed69eea2dd537c37f51099
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/shared/testhelper.cpp | 118 | ||||
-rw-r--r-- | tests/shared/testhelper.h | 2 |
2 files changed, 39 insertions, 81 deletions
diff --git a/tests/shared/testhelper.cpp b/tests/shared/testhelper.cpp index a93e6b7d..b0dcd7c9 100644 --- a/tests/shared/testhelper.cpp +++ b/tests/shared/testhelper.cpp @@ -113,79 +113,16 @@ QJsonDocument TestHelper::readJsonFile(const QString &filename, QJsonParseError void TestHelper::launchJsonDbDaemon(const QStringList &args, const char *sourceFile, bool skipConnection) { - if (dontLaunch()) - return; - - QString configfile; - if (!args.contains(QLatin1String("-config-path"))) { - configfile = QTest::qFindTestData("partitions.json", sourceFile); - if (configfile.isEmpty()) { - qDebug() << "Cannot find partitions.json configuration file for jsondb"; - return; - } - } - - QString jsondb_app = QDir(QString::fromLocal8Bit(JSONDB_DAEMON_BASE)).absoluteFilePath(QLatin1String("jsondb")); - if (!QFile::exists(jsondb_app)) - jsondb_app = QLatin1String("jsondb"); // rely on the PATH - - mProcess = new QProcess; - mProcess->setProcessChannelMode(QProcess::ForwardedChannels); - connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)), - this, SLOT(processFinished(int,QProcess::ExitStatus))); - - QString socketName = QString("testjsondb_%1_%2").arg(getpid()).arg(mProcessIndex++); - - QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); - env.insert("JSONDB_SOCKET", socketName); - mProcess->setProcessEnvironment(env); - ::setenv("JSONDB_SOCKET", qPrintable(socketName), 1); - - QStringList argList = args; - argList << QLatin1String("-reject-stale-updates"); - if (!configfile.isEmpty()) - argList << QLatin1String("-config-path") << QFileInfo(configfile).absolutePath().toLocal8Bit(); - - if (!mWorkingDirectory.isEmpty()) - // We specified a particular directory - mProcess->setWorkingDirectory(mWorkingDirectory); - - qDebug() << "Starting process" << jsondb_app << argList << "with socket" << socketName - << "with working directory" << mProcess->workingDirectory(); - - if (useValgrind()) { - QStringList args1 = argList; - args1.prepend(jsondb_app); - mProcess->start("valgrind", args1); - } else { - mProcess->start(jsondb_app, argList); - } - - if (!mProcess->waitForStarted()) - qFatal("Unable to start jsondb database process"); - - if (skipConnection) - return; - - /* Wait until the jsondb is accepting connections */ - int tries = 0; - bool connected = false; - while (!connected && tries++ < 100) { - QLocalSocket socket; - socket.connectToServer(socketName); - if (socket.waitForConnected()) { - connected = true; - socket.close(); - } - QTest::qWait(250); - } - - if (!connected) - qFatal("Unable to connect to jsondb process"); + launchJsonDbDaemon_helper(args, sourceFile, skipConnection, false); } qint64 TestHelper::launchJsonDbDaemonDetached(const QStringList &args, const char *sourceFile, bool skipConnection) { + return launchJsonDbDaemon_helper(args, sourceFile, skipConnection, true); +} + +qint64 TestHelper::launchJsonDbDaemon_helper(const QStringList &args, const char *sourceFile, bool skipConnection, bool detached) +{ if (dontLaunch()) return 0; @@ -202,7 +139,24 @@ qint64 TestHelper::launchJsonDbDaemonDetached(const QStringList &args, const cha if (!QFile::exists(jsondb_app)) jsondb_app = QLatin1String("jsondb"); // rely on the PATH + if (!detached) { + mProcess = new QProcess; + mProcess->setProcessChannelMode(QProcess::ForwardedChannels); + connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)), + this, SLOT(processFinished(int,QProcess::ExitStatus))); + } + QString socketName = QString("testjsondb_%1_%2").arg(getpid()).arg(mProcessIndex++); + + const QString effectiveWorkingDir = mWorkingDirectory.isEmpty() ? QDir::currentPath() : mWorkingDirectory; + + if (!detached) { + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + env.insert("JSONDB_SOCKET", socketName); + mProcess->setProcessEnvironment(env); + mProcess->setWorkingDirectory(effectiveWorkingDir); + } + ::setenv("JSONDB_SOCKET", qPrintable(socketName), 1); QStringList argList = args; @@ -210,21 +164,23 @@ qint64 TestHelper::launchJsonDbDaemonDetached(const QStringList &args, const cha if (!configfile.isEmpty()) argList << QLatin1String("-config-path") << QFileInfo(configfile).absolutePath().toLocal8Bit(); - if (!mWorkingDirectory.isEmpty()) { - // We specified a particular directory - mProcess->setWorkingDirectory(mWorkingDirectory); - qDebug() << "Starting process" << jsondb_app << argList << "with socket" << socketName - << "with working directory" << mProcess->workingDirectory(); - } else - qDebug() << "Starting process" << jsondb_app << argList << "with socket" << socketName; + qDebug() << "Starting process" << jsondb_app << argList << "with socket" << socketName + << "with working directory" << effectiveWorkingDir; + + qint64 pid = 0; - qint64 pid; + QString program = jsondb_app; if (useValgrind()) { - QStringList args1 = argList; - args1.prepend(jsondb_app); - QProcess::startDetached(jsondb_app, args1, QDir::currentPath(), &pid ); + argList.prepend(program); + program = "valgrind"; + } + + if (detached) { + QProcess::startDetached(program, argList, effectiveWorkingDir, &pid); } else { - QProcess::startDetached(jsondb_app, argList, QDir::currentPath(), &pid); + mProcess->start(program, argList); + if (!mProcess->waitForStarted()) + qFatal("Unable to start jsondb database process"); } if (skipConnection) diff --git a/tests/shared/testhelper.h b/tests/shared/testhelper.h index ae51c286..9cdc8b10 100644 --- a/tests/shared/testhelper.h +++ b/tests/shared/testhelper.h @@ -133,6 +133,8 @@ private: static bool dontLaunch(); static bool useValgrind(); + qint64 launchJsonDbDaemon_helper(const QStringList &args, const char *sourceFile, bool skipConnection, bool detached); + QString mWorkingDirectory; int mRequestsPending; static int mProcessIndex; |