From a1a2a038db4c67add9350edf900583323c9cff5b Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 12 Mar 2014 11:20:37 +0100 Subject: Warn if the time taken by QTest::qSleep() is more than 50% off MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Like this we can find out more easily if autotests fail because of flaky timing. Change-Id: I57b10f5fc4908bed7d00990641b2ddfd4ea84a11 Reviewed-by: Jan Arve Sæther Reviewed-by: Thiago Macieira --- src/testlib/qtestcase.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 10bf200f4f..7b225ef5b4 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2863,6 +2863,8 @@ bool QTest::currentTestFailed() void QTest::qSleep(int ms) { QTEST_ASSERT(ms > 0); + QElapsedTimer timer; + timer.start(); #if defined(Q_OS_WINRT) WaitForSingleObjectEx(GetCurrentThread(), ms, true); @@ -2872,6 +2874,15 @@ void QTest::qSleep(int ms) struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 }; nanosleep(&ts, NULL); #endif + // Warn if the elapsed time was more than 50% longer or more than 10% shorter than the + // requested time. + qint64 requested = 1000000 * (qint64)ms; + qint64 diff = timer.nsecsElapsed() - requested; + if (diff * 2 > requested || diff * 10 < -requested) { + QTestLog::warn(qPrintable( + QString::fromLatin1("QTest::qSleep() should have taken %1ns, but actually took %2ns!") + .arg(requested).arg(diff + requested)), __FILE__, __LINE__); + } } /*! \internal -- cgit v1.2.3