From 6ca50b79bc736415e6e76b1dff2956eaca9f5236 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Thu, 10 Sep 2020 12:38:53 +0200 Subject: [PATCH] Fix tst_QElapsedTimer::elapsed() flaky test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Edward Welbourne Reviewed-by: Qt CI Bot --- .../qelapsedtimer/tst_qelapsedtimer.cpp | 34 +++++++++++++------ 1 file 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()