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>
bb10
Jarek Kobus 2020-09-10 12:38:53 +02:00
parent 44920e7fb2
commit 6ca50b79bc
1 changed files with 23 additions and 11 deletions

View File

@ -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()