1/****************************************************************************
2**
3** Copyright (C) 2019 Ford Motor Company
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtRemoteObjects module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QtTest>
30#include <QMetaType>
31#include <QProcess>
32#include <QStandardPaths>
33
34namespace {
35
36QString findExecutable(const QString &executableName, const QStringList &paths)
37{
38 const auto path = QStandardPaths::findExecutable(executableName, paths);
39 if (!path.isEmpty()) {
40 return path;
41 }
42
43 qWarning() << "Could not find executable:" << executableName << "in any of" << paths;
44 return QString();
45}
46
47}
48
49class tst_Restart: public QObject
50{
51 Q_OBJECT
52
53public:
54 enum RunMode { Baseline, ServerRestartGraceful, ServerRestartFatal };
55 enum ObjectMode { NullPointer, ObjectPointer };
56 Q_ENUM(RunMode)
57 Q_ENUM(ObjectMode)
58
59private slots:
60 void initTestCase()
61 {
62 QLoggingCategory::setFilterRules("qt.remoteobjects.warning=false");
63 }
64
65 void cleanup()
66 {
67 // wait for delivery of RemoveObject events to the source
68 QTest::qWait(ms: 200);
69 }
70
71 void testRun_data()
72 {
73 QTest::addColumn<RunMode>(name: "runMode");
74 QTest::addColumn<ObjectMode>(name: "objectMode");
75 auto runModeMeta = QMetaEnum::fromType<RunMode>();
76 auto objectModeMeta = QMetaEnum::fromType<ObjectMode>();
77 for (int i = 0; i < runModeMeta.keyCount(); i++) {
78 for (int j = 0; j < objectModeMeta.keyCount(); j++) {
79 auto ba = QByteArray(runModeMeta.valueToKey(value: i));
80 ba = ba.append(s: "_").append(s: objectModeMeta.valueToKey(value: j));
81 QTest::newRow(dataTag: ba.data()) << static_cast<RunMode>(i) << static_cast<ObjectMode>(j);
82 }
83 }
84 }
85
86 void testRun()
87 {
88 QFETCH(RunMode, runMode);
89 QFETCH(ObjectMode, objectMode);
90
91 qDebug() << "Starting server process" << runMode;
92 bool serverRestart = runMode == ServerRestartFatal || runMode == ServerRestartGraceful;
93 QProcess serverProc;
94 serverProc.setProcessChannelMode(QProcess::ForwardedChannels);
95 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
96 env.insert(name: "RunMode", value: QVariant::fromValue(value: runMode).toString());
97 env.insert(name: "ObjectMode", value: QVariant::fromValue(value: objectMode).toString());
98 serverProc.setProcessEnvironment(env);
99 serverProc.start(program: findExecutable(executableName: "restart_server", paths: {
100 QCoreApplication::applicationDirPath() + "/../server/"
101 }), arguments: QStringList());
102 QVERIFY(serverProc.waitForStarted());
103
104 // wait for server start
105 QTest::qWait(ms: 200);
106
107 qDebug() << "Starting client process";
108 QProcess clientProc;
109 clientProc.setProcessChannelMode(QProcess::ForwardedChannels);
110 clientProc.setProcessEnvironment(env);
111 clientProc.start(program: findExecutable(executableName: "restart_client", paths: {
112 QCoreApplication::applicationDirPath() + "/../client/"
113 }), arguments: QStringList());
114 QVERIFY(clientProc.waitForStarted());
115
116 if (serverRestart) {
117 env.insert(name: "RunMode", value: QVariant::fromValue(value: Baseline).toString()); // Don't include ServerRestart environment variable this time
118 qDebug() << "Waiting for server exit";
119 QVERIFY(serverProc.waitForFinished());
120 if (runMode == ServerRestartFatal)
121 QVERIFY(serverProc.exitCode() != 0);
122 else
123 QCOMPARE(serverProc.exitCode(), 0);
124 qDebug() << "Restarting server";
125 serverProc.setProcessEnvironment(env);
126 serverProc.start(program: findExecutable(executableName: "restart_server", paths: {
127 QCoreApplication::applicationDirPath() + "/../server/"
128 }), arguments: QStringList());
129 QVERIFY(serverProc.waitForStarted());
130 }
131
132 QVERIFY(clientProc.waitForFinished());
133 QVERIFY(serverProc.waitForFinished());
134
135 QCOMPARE(serverProc.exitCode(), 0);
136 QCOMPARE(clientProc.exitCode(), 0);
137 }
138};
139
140QTEST_MAIN(tst_Restart)
141
142#include "tst_restart.moc"
143

source code of qtremoteobjects/tests/auto/restart/tst/tst_restart.cpp