diff --git a/src/corelib/kernel/qtestsupport_core.cpp b/src/corelib/kernel/qtestsupport_core.cpp index 3fa7f346be9..bc461114d89 100644 --- a/src/corelib/kernel/qtestsupport_core.cpp +++ b/src/corelib/kernel/qtestsupport_core.cpp @@ -3,9 +3,7 @@ #include "qtestsupport_core.h" -#ifdef Q_OS_WIN -#include -#endif +#include 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 bool QTest::qWaitFor(Functor predicate, int timeout) diff --git a/tests/auto/testlib/selftests/sleep/CMakeLists.txt b/tests/auto/testlib/selftests/sleep/CMakeLists.txt index c7d04abc89f..dcd6ab690ba 100644 --- a/tests/auto/testlib/selftests/sleep/CMakeLists.txt +++ b/tests/auto/testlib/selftests/sleep/CMakeLists.txt @@ -12,6 +12,7 @@ qt_internal_add_executable(sleep tst_sleep.cpp LIBRARIES Qt::Test + Qt::CorePrivate ) ## Scopes: diff --git a/tests/auto/testlib/selftests/sleep/tst_sleep.cpp b/tests/auto/testlib/selftests/sleep/tst_sleep.cpp index ebefbf44d62..c259b2e4756 100644 --- a/tests/auto/testlib/selftests/sleep/tst_sleep.cpp +++ b/tests/auto/testlib/selftests/sleep/tst_sleep.cpp @@ -6,6 +6,15 @@ #include #include +#ifdef Q_OS_UNIX +#include +#include + +#include +#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)