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>
bb10
Ulf Hermann 2014-03-12 11:20:37 +01:00 committed by The Qt Project
parent 5be81925d7
commit a1a2a038db
1 changed files with 11 additions and 0 deletions

View File

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