QTest: port qSleep() to std::this_thread::sleep_for

As requested in code review. Big improvement, code-wise.

tst_Sleep::wait() was failing on the CI, so be more accurate by using
QElapsedTimer::durationElapsed(), which returns nanoseconds.

Change-Id: I5bed6d6bd768adfdecab2475e6cbe245c20aabd7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 8ace5b26aa7d0b0e800655089c6405d67dbaca12)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ahmad Samir 2023-06-04 02:14:23 +03:00 committed by Qt Cherry-pick Bot
parent 50150e3b4e
commit 50dbf86104
3 changed files with 25 additions and 20 deletions

View File

@ -3,9 +3,7 @@
#include "qtestsupport_core.h" #include "qtestsupport_core.h"
#ifdef Q_OS_WIN #include <thread>
#include <qt_windows.h>
#endif
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -17,9 +15,11 @@ QT_BEGIN_NAMESPACE
\a ms must be greater than 0. \a ms must be greater than 0.
\b {Note:} The qSleep() function calls either \c nanosleep() on \note Starting from Qt 6.7, this function is implemented using
unix or \c Sleep() on windows, so the accuracy of time spent in \c {std::this_thread::sleep_for}, so the accuracy of time spent depends
qSleep() depends on the operating system. on the Standard Library implementation. Before Qt 6.7 this function called
either \c nanosleep() on Unix or \c Sleep() on Windows, so the accuracy of
time spent in this function depended on the operating system.
Example: Example:
\snippet code/src_qtestlib_qtestcase.cpp 23 \snippet code/src_qtestlib_qtestcase.cpp 23
@ -29,13 +29,7 @@ QT_BEGIN_NAMESPACE
Q_CORE_EXPORT void QTest::qSleep(int ms) Q_CORE_EXPORT void QTest::qSleep(int ms)
{ {
Q_ASSERT(ms > 0); Q_ASSERT(ms > 0);
std::this_thread::sleep_for(std::chrono::milliseconds{ms});
#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) /*! \fn template <typename Functor> bool QTest::qWaitFor(Functor predicate, int timeout)

View File

@ -12,6 +12,7 @@ qt_internal_add_executable(sleep
tst_sleep.cpp tst_sleep.cpp
LIBRARIES LIBRARIES
Qt::Test Qt::Test
Qt::CorePrivate
) )
## Scopes: ## Scopes:

View File

@ -6,6 +6,15 @@
#include <QtCore/QElapsedTimer> #include <QtCore/QElapsedTimer>
#include <QTest> #include <QTest>
#ifdef Q_OS_UNIX
#include <QtCore/private/qcore_unix_p.h>
#include <QtCore/qsystemdetection.h>
#include <time.h>
#endif
using namespace std::chrono_literals;
class tst_Sleep: public QObject class tst_Sleep: public QObject
{ {
Q_OBJECT Q_OBJECT
@ -21,13 +30,13 @@ void tst_Sleep::sleep()
t.start(); t.start();
QTest::qSleep(100); QTest::qSleep(100);
QVERIFY(t.elapsed() > 90); QCOMPARE_GE(t.durationElapsed(), 90ms);
QTest::qSleep(1000); QTest::qSleep(1000);
QVERIFY(t.elapsed() > 1000); QCOMPARE_GE(t.durationElapsed(), 1s);
QTest::qSleep(1000 * 10); // 10 seconds QTest::qSleep(1000 * 10); // 10 seconds
QVERIFY(t.elapsed() > 1000 * 10); QCOMPARE_GE(t.durationElapsed(), 10s);
} }
void tst_Sleep::wait() void tst_Sleep::wait()
@ -35,17 +44,18 @@ void tst_Sleep::wait()
QElapsedTimer t; QElapsedTimer t;
t.start(); t.start();
t.start();
QTest::qWait(1); QTest::qWait(1);
QVERIFY(t.elapsed() >= 1); QCOMPARE_GE(t.durationElapsed(), 1ms);
QTest::qWait(10); QTest::qWait(10);
QVERIFY(t.elapsed() >= 11); QCOMPARE_GE(t.durationElapsed(), 11ms);
QTest::qWait(100); QTest::qWait(100);
QVERIFY(t.elapsed() >= 111); QCOMPARE_GE(t.durationElapsed(), 111ms);
QTest::qWait(1000); QTest::qWait(1000);
QVERIFY(t.elapsed() >= 1111); QCOMPARE_GE(t.durationElapsed(), 1111ms);
} }
QTEST_MAIN(tst_Sleep) QTEST_MAIN(tst_Sleep)