summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@digia.com>2014-03-12 11:20:37 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-03-17 16:27:55 +0100
commita1a2a038db4c67add9350edf900583323c9cff5b (patch)
tree557f4239c9535887f586c234e65aefae4dee46cf /src
parent5be81925d7be19dd0f1022c3cfaa9c88624b1f08 (diff)
Warn if the time taken by QTest::qSleep() is more than 50% off
Like this we can find out more easily if autotests fail because of flaky timing. Change-Id: I57b10f5fc4908bed7d00990641b2ddfd4ea84a11 Reviewed-by: Jan Arve Sæther <jan-arve.saether@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/testlib/qtestcase.cpp11
1 files changed, 11 insertions, 0 deletions
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