summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Samir <[email protected]>2023-02-19 21:44:55 +0200
committerAhmad Samir <[email protected]>2023-06-30 02:24:53 +0300
commitc580a7b0fa09473fb6afaf3416c858b71171ad48 (patch)
tree60cfa0e30ee1215da7a231f2d2382a999f2d6c28
parent3902fb0438d946a466b67f2b82ba8779c731e55d (diff)
QTest: add qSleep(std::chrono::milliseconds) overload
Using chrono means one can write 10s instead of 10'000. [ChangeLog][QTest] Added qSleep(std::chrono::milliseconds) overload. Change-Id: Iac1b12a3fc3f692b557e2d459e6f3bc565f20e93 Reviewed-by: Thiago Macieira <[email protected]>
-rw-r--r--src/corelib/kernel/qtestsupport_core.cpp36
-rw-r--r--src/corelib/kernel/qtestsupport_core.h1
-rw-r--r--src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp5
-rw-r--r--tests/auto/testlib/selftests/sleep/tst_sleep.cpp14
-rw-r--r--tests/auto/testlib/selftests/watchdog/tst_watchdog.cpp5
5 files changed, 44 insertions, 17 deletions
diff --git a/src/corelib/kernel/qtestsupport_core.cpp b/src/corelib/kernel/qtestsupport_core.cpp
index bc461114d89..a6221444180 100644
--- a/src/corelib/kernel/qtestsupport_core.cpp
+++ b/src/corelib/kernel/qtestsupport_core.cpp
@@ -5,15 +5,35 @@
#include <thread>
+using namespace std::chrono_literals;
+
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.
+ \overload
+
+ Sleeps for \a ms milliseconds, blocking execution of the test.
+
+ Equivalent to calling:
+ \code
+ QTest::qSleep(std::chrono::milliseconds{ms});
+ \endcode
+*/
+void QTest::qSleep(int ms)
+{
+ QTest::qSleep(std::chrono::milliseconds{ms});
+}
+
+/*!
+ \since 6.7
+
+ Sleeps for \a msecs, blocking execution of the test.
+
+ This method will not do any event processing and will 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.
+ \a msecs must be greater than 0ms.
\note Starting from Qt 6.7, this function is implemented using
\c {std::this_thread::sleep_for}, so the accuracy of time spent depends
@@ -26,10 +46,10 @@ QT_BEGIN_NAMESPACE
\sa {QTest::qWait()}
*/
-Q_CORE_EXPORT void QTest::qSleep(int ms)
+void QTest::qSleep(std::chrono::milliseconds msecs)
{
- Q_ASSERT(ms > 0);
- std::this_thread::sleep_for(std::chrono::milliseconds{ms});
+ Q_ASSERT(msecs > 0ms);
+ std::this_thread::sleep_for(msecs);
}
/*! \fn template <typename Functor> bool QTest::qWaitFor(Functor predicate, int timeout)
diff --git a/src/corelib/kernel/qtestsupport_core.h b/src/corelib/kernel/qtestsupport_core.h
index c9505196a91..f3aa2299104 100644
--- a/src/corelib/kernel/qtestsupport_core.h
+++ b/src/corelib/kernel/qtestsupport_core.h
@@ -13,6 +13,7 @@ QT_BEGIN_NAMESPACE
namespace QTest {
Q_CORE_EXPORT void qSleep(int ms);
+Q_CORE_EXPORT void qSleep(std::chrono::milliseconds msecs);
template <typename Functor>
[[nodiscard]] static bool qWaitFor(Functor predicate, int timeout = 5000)
diff --git a/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp
index 532b26b4f18..4716d06e55c 100644
--- a/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp
+++ b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp
@@ -170,10 +170,11 @@ void MyTestClass::cleanup()
}
//! [22]
-void mySleep()
+void quarterSecondSleep()
{
//! [23]
-QTest::qSleep(250);
+using namespace std::chrono_literals;
+QTest::qSleep(250ms);
//! [23]
}
diff --git a/tests/auto/testlib/selftests/sleep/tst_sleep.cpp b/tests/auto/testlib/selftests/sleep/tst_sleep.cpp
index c259b2e4756..03d157afde3 100644
--- a/tests/auto/testlib/selftests/sleep/tst_sleep.cpp
+++ b/tests/auto/testlib/selftests/sleep/tst_sleep.cpp
@@ -26,17 +26,21 @@ private slots:
void tst_Sleep::sleep()
{
+ // Subtracting 10ms as a margin for error
+ static constexpr auto MarginForError = 10ms;
+
QElapsedTimer t;
t.start();
+ // Test qSleep(int) overload, too
QTest::qSleep(100);
- QCOMPARE_GE(t.durationElapsed(), 90ms);
+ QCOMPARE_GT(t.durationElapsed(), 100ms - MarginForError);
- QTest::qSleep(1000);
- QCOMPARE_GE(t.durationElapsed(), 1s);
+ QTest::qSleep(1s);
+ QCOMPARE_GT(t.durationElapsed(), 1s - MarginForError);
- QTest::qSleep(1000 * 10); // 10 seconds
- QCOMPARE_GE(t.durationElapsed(), 10s);
+ QTest::qSleep(10s);
+ QCOMPARE_GT(t.durationElapsed(), 10s - MarginForError);
}
void tst_Sleep::wait()
diff --git a/tests/auto/testlib/selftests/watchdog/tst_watchdog.cpp b/tests/auto/testlib/selftests/watchdog/tst_watchdog.cpp
index dc5cde236b4..9503e388749 100644
--- a/tests/auto/testlib/selftests/watchdog/tst_watchdog.cpp
+++ b/tests/auto/testlib/selftests/watchdog/tst_watchdog.cpp
@@ -3,6 +3,8 @@
#include <QTest>
+using namespace std::chrono_literals;
+
class tst_Watchdog : public QObject
{
Q_OBJECT
@@ -13,10 +15,9 @@ private slots:
void tst_Watchdog::delay() const
{
bool ok = false;
- const int fiveMinutes = 5 * 60 * 1000;
// Use the same env.var as the watch-dog and add a little to it:
const int timeout = qEnvironmentVariableIntValue("QTEST_FUNCTION_TIMEOUT", &ok);
- QTest::qSleep(5000 + (ok && timeout > 0 ? timeout : fiveMinutes));
+ QTest::qSleep(5s + (ok && timeout > 0 ? timeout * 1ms : 5min));
// The watchdog timer should have interrupted us by now.
QFAIL("ERROR: this function should be interrupted.");
}