Skip to content

Commit

Permalink
Use precise timeout timers
Browse files Browse the repository at this point in the history
  • Loading branch information
rweickelt committed Apr 14, 2018
1 parent 6ace1e1 commit e265890
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/qst/processprobe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "testcase.h"
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QEventLoop>
#include <QtQml/QQmlEngine>

ProcessProbe::ProcessProbe(QObject *parent) : Component(parent)
Expand Down Expand Up @@ -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<void (QProcess::*)(int)>(&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)
Expand Down
1 change: 1 addition & 0 deletions src/qst/rochostcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
1 change: 1 addition & 0 deletions src/qst/stpsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion src/qst/testcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit e265890

Please sign in to comment.