From e265890c9519e142c37b95924513f66f32b06164 Mon Sep 17 00:00:00 2001 From: Richard Weickelt Date: Sat, 14 Apr 2018 16:06:35 +0200 Subject: [PATCH] Use precise timeout timers --- src/qst/processprobe.cpp | 30 ++++++++++++++++++++++++++++-- src/qst/rochostcontroller.cpp | 1 + src/qst/stpsocket.cpp | 1 + src/qst/testcase.cpp | 6 +++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/qst/processprobe.cpp b/src/qst/processprobe.cpp index 549abe5..3813176 100644 --- a/src/qst/processprobe.cpp +++ b/src/qst/processprobe.cpp @@ -27,6 +27,7 @@ #include "testcase.h" #include #include +#include #include ProcessProbe::ProcessProbe(QObject *parent) : Component(parent) @@ -101,12 +102,37 @@ void ProcessProbe::terminate() bool ProcessProbe::waitForStarted(int milliseconds) { - return m_process.waitForStarted(milliseconds); + if (m_process.state() != QProcess::Starting) + { + return true; + } + + QEventLoop loop; + QTimer timer; + timer.setSingleShot(true); + timer.setTimerType(Qt::PreciseTimer); + timer.start(milliseconds); + connect(&m_process, &QProcess::started, &loop, [&loop](){ loop.exit(0); }); + connect(&timer, &QTimer::timeout, &loop, [&loop]{ loop.exit(1); }); + return loop.exec() == 0; } bool ProcessProbe::waitForFinished(int milliseconds) { - return m_process.waitForFinished(milliseconds); + if (m_process.state() != QProcess::Running) + { + return false; + } + + QEventLoop loop; + QTimer timer; + timer.setSingleShot(true); + timer.setTimerType(Qt::PreciseTimer); + timer.start(milliseconds); + connect(&m_process, static_cast(&QProcess::finished), + &loop, [&loop](int){ loop.exit(0); }); + connect(&timer, &QTimer::timeout, &loop, [&loop]{ loop.exit(1); }); + return loop.exec() == 0; } void ProcessProbe::onProcessErrorOccurred(QProcess::ProcessError error) diff --git a/src/qst/rochostcontroller.cpp b/src/qst/rochostcontroller.cpp index 8fe9b75..1b27f21 100644 --- a/src/qst/rochostcontroller.cpp +++ b/src/qst/rochostcontroller.cpp @@ -51,6 +51,7 @@ RocHostController::RocHostController(const QString& port) : QObject() &m_pingTimer, &QTimer::timeout, this, &RocHostController::onPingTimerTick); m_pingTimer.setSingleShot(false); + m_pingTimer.setTimerType(Qt::PreciseTimer); } diff --git a/src/qst/stpsocket.cpp b/src/qst/stpsocket.cpp index e07e036..33a818d 100644 --- a/src/qst/stpsocket.cpp +++ b/src/qst/stpsocket.cpp @@ -156,6 +156,7 @@ bool StpSocket::waitForReadyRead(qint32 milliseconds) QTimer timeoutTimer; timeoutTimer.setSingleShot(true); + timeoutTimer.setTimerType(Qt::PreciseTimer); timeoutTimer.start(milliseconds); while (timeoutTimer.isActive() && (m_rxMessageQueue.length() == 0)) diff --git a/src/qst/testcase.cpp b/src/qst/testcase.cpp index 047c3c4..f0d2ac1 100644 --- a/src/qst/testcase.cpp +++ b/src/qst/testcase.cpp @@ -335,7 +335,11 @@ void Testcase::waitMilliseconds(int milliseconds, const QString& file, int line) m_callerFile = file; m_callerLine = line; QEventLoop eventLoop; - QTimer::singleShot(milliseconds, &eventLoop, &QEventLoop::quit); + QTimer timer; + timer.setSingleShot(true); + timer.setTimerType(Qt::PreciseTimer); + timer.connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit); + timer.start(milliseconds); eventLoop.exec(); }