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"
#ifdef Q_OS_WIN
#include <qt_windows.h>
#endif
#include <thread>
QT_BEGIN_NAMESPACE
@ -17,9 +15,11 @@ QT_BEGIN_NAMESPACE
\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.
\note Starting from Qt 6.7, this function is implemented using
\c {std::this_thread::sleep_for}, so the accuracy of time spent depends
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:
\snippet code/src_qtestlib_qtestcase.cpp 23
@ -29,13 +29,7 @@ QT_BEGIN_NAMESPACE
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
std::this_thread::sleep_for(std::chrono::milliseconds{ms});
}
/*! \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
LIBRARIES
Qt::Test
Qt::CorePrivate
)
## Scopes:

View File

@ -6,6 +6,15 @@
#include <QtCore/QElapsedTimer>
#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
{
Q_OBJECT
@ -21,13 +30,13 @@ void tst_Sleep::sleep()
t.start();
QTest::qSleep(100);
QVERIFY(t.elapsed() > 90);
QCOMPARE_GE(t.durationElapsed(), 90ms);
QTest::qSleep(1000);
QVERIFY(t.elapsed() > 1000);
QCOMPARE_GE(t.durationElapsed(), 1s);
QTest::qSleep(1000 * 10); // 10 seconds
QVERIFY(t.elapsed() > 1000 * 10);
QCOMPARE_GE(t.durationElapsed(), 10s);
}
void tst_Sleep::wait()
@ -35,17 +44,18 @@ void tst_Sleep::wait()
QElapsedTimer t;
t.start();
t.start();
QTest::qWait(1);
QVERIFY(t.elapsed() >= 1);
QCOMPARE_GE(t.durationElapsed(), 1ms);
QTest::qWait(10);
QVERIFY(t.elapsed() >= 11);
QCOMPARE_GE(t.durationElapsed(), 11ms);
QTest::qWait(100);
QVERIFY(t.elapsed() >= 111);
QCOMPARE_GE(t.durationElapsed(), 111ms);
QTest::qWait(1000);
QVERIFY(t.elapsed() >= 1111);
QCOMPARE_GE(t.durationElapsed(), 1111ms);
}
QTEST_MAIN(tst_Sleep)