Inline QTest::qSleep()

Simply use QThread::msleep() to implement it.

Change-Id: I37c255fc70951715edc489d9f67669b01af380b1
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Lars Knoll 2020-09-03 11:18:09 +02:00
parent 75d1d2a913
commit a0e0b51001
4 changed files with 8 additions and 45 deletions

View File

@ -45,18 +45,6 @@
QT_BEGIN_NAMESPACE
Q_CORE_EXPORT void QTestPrivate::qSleep(int ms)
{
Q_ASSERT(ms > 0);
#if defined(Q_OS_WIN)
Sleep(uint(ms));
#else
struct timespec ts = { time_t(ms / 1000), (ms % 1000) * 1000 * 1000 };
nanosleep(&ts, nullptr);
#endif
}
/*! \fn template <typename Functor> bool QTest::qWaitFor(Functor predicate, int timeout)
Waits for \a timeout milliseconds or until the \a predicate returns true.
@ -88,25 +76,5 @@ Q_CORE_EXPORT void QTestPrivate::qSleep(int ms)
\sa QTest::qSleep(), QSignalSpy::wait()
*/
Q_CORE_EXPORT void QTest::qWait(int ms)
{
// Ideally this method would be implemented in terms of qWaitFor, with
// a predicate that always returns false, but due to a compiler bug in
// GCC 6 we can't do that.
Q_ASSERT(QCoreApplication::instance());
QDeadlineTimer timer(ms, Qt::PreciseTimer);
int remaining = ms;
do {
QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
remaining = timer.remainingTime();
if (remaining <= 0)
break;
QTestPrivate::qSleep(qMin(10, remaining));
remaining = timer.remainingTime();
} while (remaining > 0);
}
QT_END_NAMESPACE

View File

@ -42,13 +42,10 @@
#include <QtCore/qcoreapplication.h>
#include <QtCore/qdeadlinetimer.h>
#include <QtCore/qthread.h>
QT_BEGIN_NAMESPACE
namespace QTestPrivate {
Q_CORE_EXPORT void qSleep(int ms);
}
namespace QTest {
template <typename Functor>
@ -78,7 +75,7 @@ Q_REQUIRED_RESULT static bool qWaitFor(Functor predicate, int timeout = 5000)
remaining = int(deadline.remainingTime());
if (remaining > 0)
QTestPrivate::qSleep(qMin(10, remaining));
QThread::msleep(qMin(10, remaining));
if (predicate())
return true;
@ -89,7 +86,10 @@ Q_REQUIRED_RESULT static bool qWaitFor(Functor predicate, int timeout = 5000)
return predicate(); // Last chance
}
Q_CORE_EXPORT void qWait(int ms);
inline void qWait(int ms)
{
(void)qWaitFor([]() { return false; }, ms);
}
} // namespace QTest

View File

@ -2491,12 +2491,6 @@ bool QTest::currentTestFailed()
\sa {QTest::qWait()}
*/
void QTest::qSleep(int ms)
{
// ### Qt 6, move to QtCore or remove altogether
QTEST_ASSERT(ms > 0);
QTestPrivate::qSleep(ms);
}
/*! \internal
*/

View File

@ -48,6 +48,7 @@
#include <QtCore/qmetaobject.h>
#include <QtCore/qsharedpointer.h>
#include <QtCore/qtemporarydir.h>
#include <QtCore/qthread.h>
#include <string.h>
@ -335,7 +336,7 @@ namespace QTest
char *val1, char *val2,
const char *actual, const char *expected,
const char *file, int line);
Q_TESTLIB_EXPORT void qSleep(int ms);
inline void qSleep(int ms) { QThread::msleep(ms); }
Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
template <typename T>