diff options
author | Jarek Kobus <jaroslaw.kobus@qt.io> | 2020-09-10 12:38:53 +0200 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@qt.io> | 2020-09-15 21:07:00 +0200 |
commit | 6ca50b79bc736415e6e76b1dff2956eaca9f5236 (patch) | |
tree | ce1034988c2231156d9c61841a9a55c082c39ec3 | |
parent | 44920e7fb272f49423c925555e8ae3bc38ffd8a4 (diff) |
Fix tst_QElapsedTimer::elapsed() flaky test
Instead of using imprecise QTest::qSleep() to estimate the
elapsed time, we trigger a single shot PreciseTimer and
gather all the data in lambda. We wait for lambda to be
executed - we give it twice as much time as is in theory
needed. Afterwards we verify all the data collected in lambda.
Task-number: QTBUG-82903
Change-Id: I0147b7cd2aaf4bf58a216caff167d2db8712541a
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r-- | tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp b/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp index bfc4f2ca36..d0b6992fce 100644 --- a/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp +++ b/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp @@ -105,28 +105,40 @@ void tst_QElapsedTimer::basics() void tst_QElapsedTimer::elapsed() { + qint64 nsecs = 0; + qint64 msecs = 0; + bool expired1 = false; + bool expired8 = false; + bool expiredInf = false; + qint64 elapsed = 0; + bool timerExecuted = false; + QElapsedTimer t1; t1.start(); - QTest::qSleep(2*minResolution); + QTimer::singleShot(2 * minResolution, Qt::PreciseTimer, [&](){ + nsecs = t1.nsecsElapsed(); + msecs = t1.elapsed(); + expired1 = t1.hasExpired(minResolution); + expired8 = t1.hasExpired(8 * minResolution); + expiredInf = t1.hasExpired(-1); + elapsed = t1.restart(); + timerExecuted = true; + }); + + QTRY_VERIFY_WITH_TIMEOUT(timerExecuted, 4 * minResolution); - auto nsecs = t1.nsecsElapsed(); - auto msecs = t1.elapsed(); QVERIFY(nsecs > 0); QVERIFY(msecs > 0); // the number of elapsed nanoseconds and milliseconds should match QVERIFY(nsecs - msecs * 1000000 < 1000000); - if (msecs > 8 * minResolution) - QSKIP("Sampling timer took too long, aborting test"); + QVERIFY(expired1); + QVERIFY(!expired8); + QVERIFY(!expiredInf); - QVERIFY(t1.hasExpired(minResolution)); - QVERIFY(!t1.hasExpired(8*minResolution)); - QVERIFY(!t1.hasExpired(-1)); - - qint64 elapsed = t1.restart(); QVERIFY(elapsed >= msecs); - QVERIFY(elapsed < msecs + 3*minResolution); + QVERIFY(elapsed < msecs + 3 * minResolution); } void tst_QElapsedTimer::msecsTo() |