From 1abea5f5f13b4b8ec2a1c282e643b791cea12f30 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Sat, 12 Sep 2020 13:58:03 +0200 Subject: [PATCH] Partially revert "Inline QTest::qSleep()" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change partially reverts change a0e0b51001edfc1c7aea113c472ce995efa833fd. Replacing the QTest specific sleep function with QThread::msleep() was not a good idea. The reason is that QThread::msleep() will force the thread to sleep to x mseconds, even if a signal woke the thread in the meantime. This would cause qWaitFor() to not call processEvents(), in some cases, leading to flakyness and test failures in tests that rely on timing, such as the animation tests in Qt Qml. Change-Id: I0ad132cdf32be5813b2e73552d772251fe1d7f89 Reviewed-by: Volker Hilsheimer Reviewed-by: Tor Arne Vestbø --- src/corelib/kernel/qtestsupport_core.cpp | 29 ++++++++++++++++++++++++ src/corelib/kernel/qtestsupport_core.h | 4 +++- src/testlib/qtestcase.cpp | 18 --------------- src/testlib/qtestcase.h | 1 - 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/corelib/kernel/qtestsupport_core.cpp b/src/corelib/kernel/qtestsupport_core.cpp index e6e3dfbd69b..02dd5ad04d7 100644 --- a/src/corelib/kernel/qtestsupport_core.cpp +++ b/src/corelib/kernel/qtestsupport_core.cpp @@ -45,6 +45,35 @@ QT_BEGIN_NAMESPACE +/*! + Sleeps for \a ms milliseconds, blocking execution of the + test. qSleep() will not do any event processing and leave your test + unresponsive. Network communication might time out while + sleeping. Use \l {QTest::qWait()} to do non-blocking sleeping. + + \a ms must be greater than 0. + + \b {Note:} The qSleep() function calls either \c nanosleep() on + unix or \c Sleep() on windows, so the accuracy of time spent in + qSleep() depends on the operating system. + + Example: + \snippet code/src_qtestlib_qtestcase.cpp 23 + + \sa {QTest::qWait()} +*/ +Q_CORE_EXPORT void QTest::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 bool QTest::qWaitFor(Functor predicate, int timeout) Waits for \a timeout milliseconds or until the \a predicate returns true. diff --git a/src/corelib/kernel/qtestsupport_core.h b/src/corelib/kernel/qtestsupport_core.h index 03430d7680f..2abe70bf993 100644 --- a/src/corelib/kernel/qtestsupport_core.h +++ b/src/corelib/kernel/qtestsupport_core.h @@ -48,6 +48,8 @@ QT_BEGIN_NAMESPACE namespace QTest { +Q_CORE_EXPORT void qSleep(int ms); + template Q_REQUIRED_RESULT static bool qWaitFor(Functor predicate, int timeout = 5000) { @@ -75,7 +77,7 @@ Q_REQUIRED_RESULT static bool qWaitFor(Functor predicate, int timeout = 5000) remaining = int(deadline.remainingTime()); if (remaining > 0) - QThread::msleep(qMin(10, remaining)); + qSleep(qMin(10, remaining)); if (predicate()) return true; diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index c2b33019b27..0202915e678 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2474,24 +2474,6 @@ bool QTest::currentTestFailed() return QTestResult::currentTestFailed(); } -/*! - Sleeps for \a ms milliseconds, blocking execution of the - test. qSleep() will not do any event processing and leave your test - unresponsive. Network communication might time out while - sleeping. Use \l {QTest::qWait()} to do non-blocking sleeping. - - \a ms must be greater than 0. - - \b {Note:} The qSleep() function calls either \c nanosleep() on - unix or \c Sleep() on windows, so the accuracy of time spent in - qSleep() depends on the operating system. - - Example: - \snippet code/src_qtestlib_qtestcase.cpp 23 - - \sa {QTest::qWait()} -*/ - /*! \internal */ QObject *QTest::testObject() diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 6fe962277a5..e6a2b01c958 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -336,7 +336,6 @@ namespace QTest char *val1, char *val2, const char *actual, const char *expected, const char *file, int line); - inline void qSleep(int ms) { QThread::msleep(ms); } Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name); template