From 8b9a02537300507d21d58ac7d3db4fe1a2f2fe62 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Mon, 24 Feb 2020 17:23:57 +0100 Subject: Make QDeadlineTimer test more resilient against VM starvation Flaky fails in this test suggest that the VM on which the test is executed does not get CPU resources allocated for enough time to make this test pass. This change makes the test more resilient by taking the measurements as quickly as possible. In addition, use a sanity-check based on std::chrono APIs to abort the test completely if we see that the clock has advanced too far to make the following tests meaningful. Change-Id: Ie6ac4ffb52f20e7774014f8222c9cd8f54d8a263 Fixes: QTBUG-64517 Reviewed-by: Friedemann Kleint --- .../kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp | 23 ++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'tests/auto') diff --git a/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp b/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp index 4e105d7dc7..35c5e7cb75 100644 --- a/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp +++ b/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp @@ -580,11 +580,26 @@ void tst_QDeadlineTimer::stdchrono() auto now = QDeadlineTimer::current(timerType); QTest::qSleep(minResolution); + auto sampling_start = steady_clock::now(); + auto steady_deadline = now.deadline(); + auto system_deadline = now.deadline(); auto steady_after = steady_clock::now(); auto system_after = system_clock::now(); + auto sampling_end = steady_clock::now(); + + auto sampling_diff = duration_cast(sampling_end - sampling_start).count(); + if (sampling_diff > minResolution/2) { + qWarning() << "Sampling clock took" << sampling_diff << "ms"; + QSKIP("Sampling clock took too long, aborting test", Abort); + } + auto total_diff = duration_cast(steady_after - steady_before).count(); + if (total_diff >= 3*minResolution) { + qWarning() << "Measurement took" << total_diff << "ms"; + QSKIP("Measurement took too long, aborting test", Abort); + } { - auto diff = duration_cast(steady_after - now.deadline()); + auto diff = duration_cast(steady_after - steady_deadline); QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count()))); QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count()))); QDeadlineTimer dt_after(steady_after, timerType); @@ -592,7 +607,7 @@ void tst_QDeadlineTimer::stdchrono() ("now = " + QLocale().toString(now.deadlineNSecs()) + "; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1()); - diff = duration_cast(now.deadline() - steady_before); + diff = duration_cast(steady_deadline - steady_before); QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count()))); QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count()))); QDeadlineTimer dt_before(steady_before, timerType); @@ -601,7 +616,7 @@ void tst_QDeadlineTimer::stdchrono() "; before = " + QLocale().toString(dt_before.deadlineNSecs())).toLatin1()); } { - auto diff = duration_cast(system_after - now.deadline()); + auto diff = duration_cast(system_after - system_deadline); QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count()))); QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count()))); QDeadlineTimer dt_after(system_after, timerType); @@ -609,7 +624,7 @@ void tst_QDeadlineTimer::stdchrono() ("now = " + QLocale().toString(now.deadlineNSecs()) + "; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1()); - diff = duration_cast(now.deadline() - system_before); + diff = duration_cast(system_deadline - system_before); QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count()))); QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count()))); QDeadlineTimer dt_before(system_before, timerType); -- cgit v1.2.3