summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2020-02-27 09:53:49 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2020-02-27 10:36:57 +0100
commit715468df40e4ce97da04f327b6e34d535ff9b97d (patch)
treeb57d6346e5a3d916a74cc8c08e327cd8623c532c /tests/auto/corelib
parent529cfe4e22cc02dc7c29f653e8ff23656aa16ff9 (diff)
Fix flaky QElapsedTimer::elapsed test case
Much of this test case was testing that the machine it runs on didn't take more than an expected amount of time, which is an assumption that won't hold in a virtual environment where the hypervisor might decide to not allocate any CPU time to the machine at certain times. Instead, take the samples that we want to compare with once, then use them as reference for further comparisons. Also, split the test in two, with the comparison operators and msecsTo test moved into a separate test function. Change-Id: I7db12b8e02552f4d63af933c1b0fee9d62b591eb Fixes: QTBUG-58713 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r--tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp47
1 files changed, 30 insertions, 17 deletions
diff --git a/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp b/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp
index 4ee3ca361f..bfc4f2ca36 100644
--- a/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp
+++ b/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp
@@ -48,6 +48,7 @@ private Q_SLOTS:
void validity();
void basics();
void elapsed();
+ void msecsTo();
};
void tst_QElapsedTimer::statics()
@@ -108,30 +109,42 @@ void tst_QElapsedTimer::elapsed()
t1.start();
QTest::qSleep(2*minResolution);
- QElapsedTimer t2;
- t2.start();
-
- QVERIFY(t1 != t2);
- QVERIFY(!(t1 == t2));
- QVERIFY(t1 < t2);
- QVERIFY(t1.msecsTo(t2) > 0);
- QVERIFY(t1.nsecsElapsed() > 0);
- QVERIFY(t1.elapsed() > 0);
+ auto nsecs = t1.nsecsElapsed();
+ auto msecs = t1.elapsed();
+ QVERIFY(nsecs > 0);
+ QVERIFY(msecs > 0);
// the number of elapsed nanoseconds and milliseconds should match
- QVERIFY(t1.nsecsElapsed() - t1.elapsed() * 1000000 < 1000000);
+ QVERIFY(nsecs - msecs * 1000000 < 1000000);
+
+ if (msecs > 8 * minResolution)
+ QSKIP("Sampling timer took too long, aborting test");
+
QVERIFY(t1.hasExpired(minResolution));
QVERIFY(!t1.hasExpired(8*minResolution));
- QVERIFY(!t2.hasExpired(minResolution));
-
QVERIFY(!t1.hasExpired(-1));
- QVERIFY(!t2.hasExpired(-1));
qint64 elapsed = t1.restart();
- QVERIFY(elapsed > minResolution);
- QVERIFY(elapsed < 3*minResolution);
- qint64 diff = t2.msecsTo(t1);
- QVERIFY(diff < minResolution);
+ QVERIFY(elapsed >= msecs);
+ QVERIFY(elapsed < msecs + 3*minResolution);
+}
+
+void tst_QElapsedTimer::msecsTo()
+{
+ QElapsedTimer t1;
+ t1.start();
+ QTest::qSleep(minResolution);
+ QElapsedTimer t2;
+ t2.start();
+
+ QVERIFY(t1 != t2);
+ QVERIFY(!(t1 == t2));
+ QVERIFY(t1 < t2);
+
+ auto diff = t1.msecsTo(t2);
+ QVERIFY2(diff > 0, QString("difference t1 and t2 is %1").arg(diff).toLatin1());
+ diff = t2.msecsTo(t1);
+ QVERIFY2(diff < 0, QString("difference t2 and t1 is %1").arg(diff).toLatin1());
}
QTEST_MAIN(tst_QElapsedTimer);