Try again to make QDeadlineTimer test robust against context switches
Instead of comparing to absolute values, compare the result from QDeadlineTimer with the reference clock types from std::chrono. Pass the test as long as we are within 10% of that reference. In addition, handle the case where QTest::qSleep sleeps for more than 10% longer or shorter than what is requested, and if so, abort the test. Change-Id: If8b77aea55a8c5c53e96427b2fff2f78281d0f82 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
08d5059320
commit
4a1de178c9
@ -573,12 +573,32 @@ void tst_QDeadlineTimer::stdchrono()
|
|||||||
|
|
||||||
QCOMPARE(deadline.remainingTimeAsDuration(), nanoseconds::zero());
|
QCOMPARE(deadline.remainingTimeAsDuration(), nanoseconds::zero());
|
||||||
|
|
||||||
|
/*
|
||||||
|
Call QTest::qSleep, and return true if the time actually slept is
|
||||||
|
within \a deviationPercent percent of the requested sleep time.
|
||||||
|
Otherwise, return false, in which case the test should to abort.
|
||||||
|
*/
|
||||||
|
auto sleepHelper = [](int ms, int deviationPercent = 10) -> bool {
|
||||||
|
auto before = steady_clock::now();
|
||||||
|
QTest::qSleep(ms);
|
||||||
|
auto after = steady_clock::now();
|
||||||
|
auto diff = duration_cast<milliseconds>(after - before).count();
|
||||||
|
bool inRange = qAbs(diff - ms) < ms * deviationPercent/100.0;
|
||||||
|
if (!inRange)
|
||||||
|
qWarning() << "sleeping" << diff << "instead of" << ms << inRange;
|
||||||
|
return inRange;
|
||||||
|
};
|
||||||
|
|
||||||
auto steady_before = steady_clock::now();
|
auto steady_before = steady_clock::now();
|
||||||
auto system_before = system_clock::now();
|
auto system_before = system_clock::now();
|
||||||
|
|
||||||
QTest::qSleep(minResolution);
|
if (!sleepHelper(minResolution))
|
||||||
|
QSKIP("Slept too long");
|
||||||
auto now = QDeadlineTimer::current(timerType);
|
auto now = QDeadlineTimer::current(timerType);
|
||||||
QTest::qSleep(minResolution);
|
auto steady_reference = steady_clock::now();
|
||||||
|
auto system_reference = system_clock::now();
|
||||||
|
if (!sleepHelper(minResolution))
|
||||||
|
QSKIP("Slept too long");
|
||||||
|
|
||||||
auto sampling_start = steady_clock::now();
|
auto sampling_start = steady_clock::now();
|
||||||
auto steady_deadline = now.deadline<steady_clock>();
|
auto steady_deadline = now.deadline<steady_clock>();
|
||||||
@ -599,35 +619,33 @@ void tst_QDeadlineTimer::stdchrono()
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto diff = duration_cast<milliseconds>(steady_after - steady_deadline);
|
auto reference = duration_cast<milliseconds>(steady_after - steady_reference).count();
|
||||||
QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count())));
|
auto diff = duration_cast<milliseconds>(steady_after - steady_deadline).count();
|
||||||
QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count())));
|
QVERIFY2(diff > reference * 0.9 && diff < reference*1.1, QByteArray::number(qint64(diff)));
|
||||||
QDeadlineTimer dt_after(steady_after, timerType);
|
QDeadlineTimer dt_after(steady_after, timerType);
|
||||||
QVERIFY2(now < dt_after,
|
QVERIFY2(now < dt_after,
|
||||||
("now = " + QLocale().toString(now.deadlineNSecs()) +
|
("now = " + QLocale().toString(now.deadlineNSecs()) +
|
||||||
"; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1());
|
"; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1());
|
||||||
|
|
||||||
diff = duration_cast<milliseconds>(steady_deadline - steady_before);
|
reference = duration_cast<milliseconds>(steady_reference - steady_before).count();
|
||||||
QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count())));
|
diff = duration_cast<milliseconds>(steady_deadline - steady_before).count();
|
||||||
QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count())));
|
QVERIFY2(diff > reference * 0.9 && diff < reference*1.1, QByteArray::number(qint64(diff)));
|
||||||
QDeadlineTimer dt_before(steady_before, timerType);
|
QDeadlineTimer dt_before(steady_before, timerType);
|
||||||
QVERIFY2(now > dt_before,
|
QVERIFY2(now > dt_before,
|
||||||
("now = " + QLocale().toString(now.deadlineNSecs()) +
|
("now = " + QLocale().toString(now.deadlineNSecs()) +
|
||||||
"; before = " + QLocale().toString(dt_before.deadlineNSecs())).toLatin1());
|
"; before = " + QLocale().toString(dt_before.deadlineNSecs())).toLatin1());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
auto diff = duration_cast<milliseconds>(system_after - system_deadline);
|
auto reference = duration_cast<milliseconds>(system_after - system_reference).count();
|
||||||
QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count())));
|
auto diff = duration_cast<milliseconds>(system_after - system_deadline).count();
|
||||||
QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count())));
|
QVERIFY2(diff > reference * 0.9 && diff < reference*1.1, QByteArray::number(qint64(diff))); QDeadlineTimer dt_after(system_after, timerType);
|
||||||
QDeadlineTimer dt_after(system_after, timerType);
|
|
||||||
QVERIFY2(now < dt_after,
|
QVERIFY2(now < dt_after,
|
||||||
("now = " + QLocale().toString(now.deadlineNSecs()) +
|
("now = " + QLocale().toString(now.deadlineNSecs()) +
|
||||||
"; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1());
|
"; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1());
|
||||||
|
|
||||||
diff = duration_cast<milliseconds>(system_deadline - system_before);
|
reference = duration_cast<milliseconds>(system_reference - system_before).count();
|
||||||
QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count())));
|
diff = duration_cast<milliseconds>(steady_deadline - steady_before).count();
|
||||||
QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count())));
|
QVERIFY2(diff > reference * 0.9 && diff < reference*1.1, QByteArray::number(qint64(diff))); QDeadlineTimer dt_before(system_before, timerType);
|
||||||
QDeadlineTimer dt_before(system_before, timerType);
|
|
||||||
QVERIFY2(now > dt_before,
|
QVERIFY2(now > dt_before,
|
||||||
("now = " + QLocale().toString(now.deadlineNSecs()) +
|
("now = " + QLocale().toString(now.deadlineNSecs()) +
|
||||||
"; before = " + QLocale().toString(dt_before.deadlineNSecs())).toLatin1());
|
"; before = " + QLocale().toString(dt_before.deadlineNSecs())).toLatin1());
|
||||||
|
Loading…
Reference in New Issue
Block a user