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 <thiago.macieira@intel.com>
This commit is contained in:
parent
3902fb0438
commit
c580a7b0fa
@ -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
|
||||
|
||||
\a ms must be greater than 0.
|
||||
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 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)
|
||||
|
@ -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)
|
||||
|
@ -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]
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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.");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user