Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ed13578

Browse filesBrowse files
committed
added improved support for restart commands
1 parent 65b16cb commit ed13578
Copy full SHA for ed13578

File tree

Expand file treeCollapse file tree

2 files changed

+52
-19
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+52
-19
lines changed

‎src/service/servicecontrol.cpp

Copy file name to clipboardExpand all lines: src/service/servicecontrol.cpp
+29-1Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include "servicecontrol_p.h"
33
#include "logging_p.h"
44
#include "service_p.h"
5+
6+
#include <QtCore/QTimer>
7+
58
using namespace QtService;
69

710
QStringList ServiceControl::listBackends()
@@ -131,13 +134,38 @@ bool ServiceControl::stop()
131134

132135
bool ServiceControl::restart()
133136
{
137+
// blocking services can simply call stop and start
134138
if(blocking() == BlockMode::Blocking) {
135139
auto ok = stop();
136140
if(ok)
137141
ok = start();
138142
return ok;
143+
// other types can still simulate a (non)-blocking start/stop, if the have status reports
144+
} else if(supportFlags().testFlag(SupportFlag::Status)) {
145+
if(!stop())
146+
return false;
147+
148+
// check every second, until the service stopped or errored, then start again if successfully
149+
auto timer = new QTimer{this};
150+
connect(timer, &QTimer::timeout,
151+
this, [this, timer]() {
152+
switch(status()) {
153+
case Status::Stopped:
154+
timer->deleteLater();
155+
start(); //ignore result as error messages are set by start itself
156+
break;
157+
case Status::Errored:
158+
timer->deleteLater();
159+
break;
160+
// ignore all other cases
161+
default:
162+
break;
163+
}
164+
});
165+
timer->start(std::chrono::seconds{1});
166+
return true;
139167
} else {
140-
setError(tr("Operation restart is supported for non-blocking service controls"));
168+
setError(tr("Operation restart is not supported for non-blocking service controls without status information"));
141169
return false;
142170
}
143171
}

‎tests/auto/service/TestBaseLib/basicservicetest.cpp

Copy file name to clipboardExpand all lines: tests/auto/service/TestBaseLib/basicservicetest.cpp
+23-18Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void BasicServiceTest::testStart()
5050

5151
socket = new QLocalSocket(this);
5252
socket->connectToServer(QStringLiteral("__qtservice_testservice"));
53-
QVERIFY(socket->waitForConnected(30000));
53+
QVERIFY2(socket->waitForConnected(30000), qUtf8Printable(socket->errorString()));
5454
stream.setDevice(socket);
5555

5656
QByteArray msg;
@@ -106,35 +106,43 @@ void BasicServiceTest::testRestart()
106106
{
107107
TEST_STATUS(ServiceControl::Status::Running);
108108
resetSettings();
109-
if(control->blocking() != ServiceControl::BlockMode::Blocking)
110-
return;
111-
testFeature(static_cast<ServiceControl::SupportFlag>(static_cast<int>(ServiceControl::SupportFlag::Start |
112-
ServiceControl::SupportFlag::Stop)));
109+
110+
// run test for blocking backends or backends that report status - but those must support start/stop
111+
if(control->blocking() != ServiceControl::BlockMode::Blocking &&
112+
!control->supportFlags().testFlag(ServiceControl::SupportFlag::Status)) {
113+
QEXPECT_FAIL("", "feature restart not supported by backend", Abort);
114+
} else {
115+
QVERIFY(control->supportFlags().testFlag(ServiceControl::SupportFlag::Start));
116+
QVERIFY(control->supportFlags().testFlag(ServiceControl::SupportFlag::Stop));
117+
}
113118

114119
QVERIFY2(control->restart(), qUtf8Printable(control->error()));
115-
QThread::sleep(10);
116-
// blocking should only return after the server started, but for non blocking this may not be the case...
117-
if(control->blocking() != ServiceControl::BlockMode::Blocking)
118-
QThread::sleep(3);
119120

120121
QByteArray msg;
121122
#ifndef Q_OS_WIN
122123
READ_LOOP(msg);
123124
QCOMPARE(msg, QByteArray("stopping"));
124125
msg = {};
125126
#endif
126-
QVERIFY(socket->waitForDisconnected(5000));
127+
QVERIFY2(socket->waitForDisconnected(5000), qUtf8Printable(socket->errorString()));
127128
socket->deleteLater();
128129

130+
// blocking should only return after the server started, but for non blocking this may not be the case...
131+
if(control->blocking() != ServiceControl::BlockMode::Blocking) {
132+
for(auto i = 0; i < 10; ++i) {
133+
QThread::msleep(500);
134+
QCoreApplication::processEvents();
135+
}
136+
}
137+
TEST_STATUS(ServiceControl::Status::Running);
138+
129139
socket = new QLocalSocket(this);
130140
socket->connectToServer(QStringLiteral("__qtservice_testservice"));
131-
QVERIFY(socket->waitForConnected(30000));
141+
QVERIFY2(socket->waitForConnected(30000), qUtf8Printable(socket->errorString()));
132142
stream.setDevice(socket);
133143

134144
READ_LOOP(msg);
135145
QCOMPARE(msg, QByteArray("started"));
136-
137-
TEST_STATUS(ServiceControl::Status::Running);
138146
}
139147

140148
void BasicServiceTest::testCustom()
@@ -156,7 +164,7 @@ void BasicServiceTest::testStop()
156164
READ_LOOP(msg);
157165
QCOMPARE(msg, QByteArray("stopping"));
158166
#endif
159-
QVERIFY(socket->waitForDisconnected(5000));
167+
QVERIFY2(socket->waitForDisconnected(5000), qUtf8Printable(socket->errorString()));
160168

161169
TEST_STATUS(ServiceControl::Status::Stopped);
162170
}
@@ -273,10 +281,7 @@ void BasicServiceTest::performSocketTest()
273281

274282
auto tcpSocket = new QTcpSocket(this);
275283
tcpSocket->connectToHost(QStringLiteral("127.0.0.1"), 15843);
276-
while(control->status() == ServiceControl::Status::Starting)
277-
QThread::msleep(500);
278-
QVERIFY(tcpSocket->waitForConnected(5000));
279-
284+
QVERIFY2(tcpSocket->waitForConnected(5000), qUtf8Printable(socket->errorString()));
280285
TEST_STATUS(ServiceControl::Status::Running);
281286

282287
QByteArray msg = "hello world";

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.