summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorRohan McGovern <rohan.mcgovern@nokia.com>2012-02-06 04:17:08 (GMT)
committerQt by Nokia <qt-info@nokia.com>2012-02-07 07:17:34 (GMT)
commit9a2573dc13b3e8df6cd15bef64370ea407480fc7 (patch)
tree5d6187b25846bb51cb5e2b5ccbe25fa15fc35663 /tests/auto
parentc85faef67b6a7e8fcedb4ce800282d41f5b79ec1 (diff)
downloadQt-9a2573dc13b3e8df6cd15bef64370ea407480fc7.zip
Qt-9a2573dc13b3e8df6cd15bef64370ea407480fc7.tar.gz
Qt-9a2573dc13b3e8df6cd15bef64370ea407480fc7.tar.bz2
Improved stability of tst_qsemaphore (especially on mac)
Timers are not entirely precise; if we ask for a timeout of 10000 milliseconds, we might time out in 9999 instead. Also, we know the expected elapsed time in each case, so do a fuzzy comparison against that time. Previously the test was verifying that the elapsed time was greater than or equal to the timeout in the case where a timeout was expected, which means the test would not detect bugs which incorrectly caused the timeout to occur later than it should. Change-Id: Ic054013c7d53af8a87a89e9d9c4acbce68ba575e Reviewed-by: Jason McDonald <jason.mcdonald@nokia.com> Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/qsemaphore/tst_qsemaphore.cpp40
1 files changed, 26 insertions, 14 deletions
diff --git a/tests/auto/qsemaphore/tst_qsemaphore.cpp b/tests/auto/qsemaphore/tst_qsemaphore.cpp
index 29cda25..68c6cde 100644
--- a/tests/auto/qsemaphore/tst_qsemaphore.cpp
+++ b/tests/auto/qsemaphore/tst_qsemaphore.cpp
@@ -241,11 +241,21 @@ void tst_QSemaphore::tryAcquireWithTimeout()
{
QFETCH(int, timeout);
+ // timers are not guaranteed to be accurate down to the last millisecond,
+ // so we permit the elapsed times to be up to this far from the expected value.
+ int fuzz = 10;
+
QSemaphore semaphore;
QTime time;
-#define QVERIFYGE(a,b) {int e = a; if (a<b) qDebug() << #a << "=" << e << " !>= " << #b << "=" << b; QVERIFY(e>=b);}
-#define QVERIFYLE(a,b) {int e = a; if (b<a) qDebug() << #a << "=" << e << " !<= " << #b << "=" << b; QVERIFY(e<=b);}
+#define FUZZYCOMPARE(a,e) \
+ do { \
+ int a1 = a; \
+ int e1 = e; \
+ QVERIFY2(qAbs(a1-e1) < fuzz, \
+ qPrintable(QString("(%1=%2) is more than %3 milliseconds different from (%4=%5)") \
+ .arg(#a).arg(a1).arg(fuzz).arg(#e).arg(e1))); \
+ } while (0)
QCOMPARE(semaphore.available(), 0);
@@ -253,70 +263,72 @@ void tst_QSemaphore::tryAcquireWithTimeout()
QCOMPARE(semaphore.available(), 1);
time.start();
QVERIFY(!semaphore.tryAcquire(2, timeout));
- QVERIFYGE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 1);
semaphore.release();
QCOMPARE(semaphore.available(), 2);
time.start();
QVERIFY(!semaphore.tryAcquire(3, timeout));
- QVERIFYGE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 2);
semaphore.release(10);
QCOMPARE(semaphore.available(), 12);
time.start();
QVERIFY(!semaphore.tryAcquire(100, timeout));
- QVERIFYGE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 12);
semaphore.release(10);
QCOMPARE(semaphore.available(), 22);
time.start();
QVERIFY(!semaphore.tryAcquire(100, timeout));
- QVERIFYGE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 22);
time.start();
QVERIFY(semaphore.tryAcquire(1, timeout));
- QVERIFYLE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), 0);
QCOMPARE(semaphore.available(), 21);
time.start();
QVERIFY(semaphore.tryAcquire(1, timeout));
- QVERIFYLE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), 0);
QCOMPARE(semaphore.available(), 20);
time.start();
QVERIFY(semaphore.tryAcquire(10, timeout));
- QVERIFYLE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), 0);
QCOMPARE(semaphore.available(), 10);
time.start();
QVERIFY(semaphore.tryAcquire(10, timeout));
- QVERIFYLE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), 0);
QCOMPARE(semaphore.available(), 0);
// should not be able to acquire more
time.start();
QVERIFY(!semaphore.tryAcquire(1, timeout));
- QVERIFYGE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 0);
time.start();
QVERIFY(!semaphore.tryAcquire(1, timeout));
- QVERIFYGE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 0);
time.start();
QVERIFY(!semaphore.tryAcquire(10, timeout));
- QVERIFYGE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 0);
time.start();
QVERIFY(!semaphore.tryAcquire(10, timeout));
- QVERIFYGE(time.elapsed(), timeout);
+ FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 0);
+
+#undef FUZZYCOMPARE
}
void tst_QSemaphore::tryAcquireWithTimeoutStarvation()