tst_QMutex: use the new QCOMPARE_* macros

Some machine in CI is failing some of these sometimes and
I would like to know by how much.

Change-Id: I88b41d5cde81419f7c11f7038101962630eb31ef
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit 8d6d7428f49d91600977ef5fbe01ed2117424e71)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Mårten Nordheim 2024-03-01 14:53:39 +01:00 committed by Qt Cherry-pick Bot
parent 65406e0be7
commit 0119a81ea4

View File

@ -97,19 +97,19 @@ void tst_QMutex::tryLock_non_recursive()
QElapsedTimer timer; QElapsedTimer timer;
timer.start(); timer.start();
QVERIFY(!normalMutex.tryLock(waitTime)); QVERIFY(!normalMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() >= waitTime - systemTimersResolution); QCOMPARE_GE(timer.elapsed(), waitTime - systemTimersResolution);
testsTurn.release(); testsTurn.release();
// TEST 4: thread can acquire lock, timeout = waitTime // TEST 4: thread can acquire lock, timeout = waitTime
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(normalMutex.tryLock(waitTime)); QVERIFY(normalMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() <= waitTime + systemTimersResolution); QCOMPARE_LE(timer.elapsed(), waitTime + systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
timer.start(); timer.start();
// it's non-recursive, so the following lock needs to fail // it's non-recursive, so the following lock needs to fail
QVERIFY(!normalMutex.tryLock(waitTime)); QVERIFY(!normalMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() >= waitTime - systemTimersResolution); QCOMPARE_GE(timer.elapsed(), waitTime - systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(1, 0)); QVERIFY(lockCount.testAndSetRelaxed(1, 0));
normalMutex.unlock(); normalMutex.unlock();
testsTurn.release(); testsTurn.release();
@ -123,7 +123,7 @@ void tst_QMutex::tryLock_non_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(normalMutex.tryLock(0)); QVERIFY(normalMutex.tryLock(0));
QVERIFY(timer.elapsed() < waitTime + systemTimersResolution); QCOMPARE_LT(timer.elapsed(), waitTime + systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(!normalMutex.tryLock(0)); QVERIFY(!normalMutex.tryLock(0));
QVERIFY(lockCount.testAndSetRelaxed(1, 0)); QVERIFY(lockCount.testAndSetRelaxed(1, 0));
@ -134,7 +134,7 @@ void tst_QMutex::tryLock_non_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(normalMutex.tryLock(3000)); QVERIFY(normalMutex.tryLock(3000));
QVERIFY(timer.elapsed() < 3000 + systemTimersResolution); QCOMPARE_LT(timer.elapsed(), 3000 + systemTimersResolution);
normalMutex.unlock(); normalMutex.unlock();
testsTurn.release(); testsTurn.release();
@ -222,19 +222,19 @@ void tst_QMutex::try_lock_for_non_recursive()
QElapsedTimer timer; QElapsedTimer timer;
timer.start(); timer.start();
QVERIFY(!normalMutex.try_lock_for(waitTimeAsDuration)); QVERIFY(!normalMutex.try_lock_for(waitTimeAsDuration));
QVERIFY(timer.elapsed() >= waitTime - systemTimersResolution); QCOMPARE_GE(timer.elapsed(), waitTime - systemTimersResolution);
testsTurn.release(); testsTurn.release();
// TEST 4: thread can acquire lock, timeout = waitTime // TEST 4: thread can acquire lock, timeout = waitTime
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(normalMutex.try_lock_for(waitTimeAsDuration)); QVERIFY(normalMutex.try_lock_for(waitTimeAsDuration));
QVERIFY(timer.elapsed() <= waitTime + systemTimersResolution); QCOMPARE_LE(timer.elapsed(), waitTime + systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
timer.start(); timer.start();
// it's non-recursive, so the following lock needs to fail // it's non-recursive, so the following lock needs to fail
QVERIFY(!normalMutex.try_lock_for(waitTimeAsDuration)); QVERIFY(!normalMutex.try_lock_for(waitTimeAsDuration));
QVERIFY(timer.elapsed() >= waitTime - systemTimersResolution); QCOMPARE_GE(timer.elapsed(), waitTime - systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(1, 0)); QVERIFY(lockCount.testAndSetRelaxed(1, 0));
normalMutex.unlock(); normalMutex.unlock();
testsTurn.release(); testsTurn.release();
@ -248,7 +248,7 @@ void tst_QMutex::try_lock_for_non_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(normalMutex.try_lock_for(std::chrono::milliseconds::zero())); QVERIFY(normalMutex.try_lock_for(std::chrono::milliseconds::zero()));
QVERIFY(timer.elapsed() < waitTime + systemTimersResolution); QCOMPARE_LT(timer.elapsed(), waitTime + systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(!normalMutex.try_lock_for(std::chrono::milliseconds::zero())); QVERIFY(!normalMutex.try_lock_for(std::chrono::milliseconds::zero()));
QVERIFY(lockCount.testAndSetRelaxed(1, 0)); QVERIFY(lockCount.testAndSetRelaxed(1, 0));
@ -259,7 +259,7 @@ void tst_QMutex::try_lock_for_non_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(normalMutex.try_lock_for(std::chrono::milliseconds(3000))); QVERIFY(normalMutex.try_lock_for(std::chrono::milliseconds(3000)));
QVERIFY(timer.elapsed() < 3000 + systemTimersResolution); QCOMPARE_LT(timer.elapsed(), 3000 + systemTimersResolution);
normalMutex.unlock(); normalMutex.unlock();
testsTurn.release(); testsTurn.release();
@ -347,19 +347,19 @@ void tst_QMutex::try_lock_until_non_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
auto endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration; auto endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration;
QVERIFY(!normalMutex.try_lock_until(endTimePoint)); QVERIFY(!normalMutex.try_lock_until(endTimePoint));
QVERIFY(std::chrono::steady_clock::now() >= endTimePoint - systemTimersResolutionAsDuration); QCOMPARE_GE(std::chrono::steady_clock::now(), endTimePoint - systemTimersResolutionAsDuration);
testsTurn.release(); testsTurn.release();
// TEST 4: thread can acquire lock, timeout = waitTime // TEST 4: thread can acquire lock, timeout = waitTime
threadsTurn.acquire(); threadsTurn.acquire();
endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration; endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration;
QVERIFY(normalMutex.try_lock_until(endTimePoint)); QVERIFY(normalMutex.try_lock_until(endTimePoint));
QVERIFY(std::chrono::steady_clock::now() <= endTimePoint + systemTimersResolutionAsDuration); QCOMPARE_LE(std::chrono::steady_clock::now(), endTimePoint + systemTimersResolutionAsDuration);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration; endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration;
// it's non-recursive, so the following lock needs to fail // it's non-recursive, so the following lock needs to fail
QVERIFY(!normalMutex.try_lock_until(endTimePoint)); QVERIFY(!normalMutex.try_lock_until(endTimePoint));
QVERIFY(std::chrono::steady_clock::now() >= endTimePoint - systemTimersResolutionAsDuration); QCOMPARE_GE(std::chrono::steady_clock::now(), endTimePoint - systemTimersResolutionAsDuration);
QVERIFY(lockCount.testAndSetRelaxed(1, 0)); QVERIFY(lockCount.testAndSetRelaxed(1, 0));
normalMutex.unlock(); normalMutex.unlock();
testsTurn.release(); testsTurn.release();
@ -373,7 +373,7 @@ void tst_QMutex::try_lock_until_non_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration; endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration;
QVERIFY(normalMutex.try_lock_until(std::chrono::steady_clock::now())); QVERIFY(normalMutex.try_lock_until(std::chrono::steady_clock::now()));
QVERIFY(std::chrono::steady_clock::now() < endTimePoint + systemTimersResolutionAsDuration); QCOMPARE_LT(std::chrono::steady_clock::now(), endTimePoint + systemTimersResolutionAsDuration);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(!normalMutex.try_lock_until(std::chrono::steady_clock::now())); QVERIFY(!normalMutex.try_lock_until(std::chrono::steady_clock::now()));
QVERIFY(lockCount.testAndSetRelaxed(1, 0)); QVERIFY(lockCount.testAndSetRelaxed(1, 0));
@ -384,7 +384,7 @@ void tst_QMutex::try_lock_until_non_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
endTimePoint = std::chrono::steady_clock::now() + std::chrono::milliseconds(3000); endTimePoint = std::chrono::steady_clock::now() + std::chrono::milliseconds(3000);
QVERIFY(normalMutex.try_lock_until(endTimePoint)); QVERIFY(normalMutex.try_lock_until(endTimePoint));
QVERIFY(std::chrono::steady_clock::now() < endTimePoint + systemTimersResolutionAsDuration); QCOMPARE_LT(std::chrono::steady_clock::now(), endTimePoint + systemTimersResolutionAsDuration);
normalMutex.unlock(); normalMutex.unlock();
testsTurn.release(); testsTurn.release();
@ -472,14 +472,14 @@ void tst_QMutex::tryLock_recursive()
QElapsedTimer timer; QElapsedTimer timer;
timer.start(); timer.start();
QVERIFY(!recursiveMutex.tryLock(waitTime)); QVERIFY(!recursiveMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() >= waitTime - systemTimersResolution); QCOMPARE_GE(timer.elapsed(), waitTime - systemTimersResolution);
QVERIFY(!recursiveMutex.tryLock(0)); QVERIFY(!recursiveMutex.tryLock(0));
testsTurn.release(); testsTurn.release();
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(recursiveMutex.tryLock(waitTime)); QVERIFY(recursiveMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() <= waitTime + systemTimersResolution); QCOMPARE_LE(timer.elapsed(), waitTime + systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(recursiveMutex.tryLock(waitTime)); QVERIFY(recursiveMutex.tryLock(waitTime));
QVERIFY(lockCount.testAndSetRelaxed(1, 2)); QVERIFY(lockCount.testAndSetRelaxed(1, 2));
@ -497,7 +497,7 @@ void tst_QMutex::tryLock_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(recursiveMutex.tryLock(0)); QVERIFY(recursiveMutex.tryLock(0));
QVERIFY(timer.elapsed() < waitTime + systemTimersResolution); QCOMPARE_LT(timer.elapsed(), waitTime + systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(recursiveMutex.tryLock(0)); QVERIFY(recursiveMutex.tryLock(0));
QVERIFY(lockCount.testAndSetRelaxed(1, 2)); QVERIFY(lockCount.testAndSetRelaxed(1, 2));
@ -596,14 +596,14 @@ void tst_QMutex::try_lock_for_recursive()
QElapsedTimer timer; QElapsedTimer timer;
timer.start(); timer.start();
QVERIFY(!recursiveMutex.try_lock_for(waitTimeAsDuration)); QVERIFY(!recursiveMutex.try_lock_for(waitTimeAsDuration));
QVERIFY(timer.elapsed() >= waitTime - systemTimersResolution); QCOMPARE_GE(timer.elapsed(), waitTime - systemTimersResolution);
QVERIFY(!recursiveMutex.try_lock_for(std::chrono::milliseconds::zero())); QVERIFY(!recursiveMutex.try_lock_for(std::chrono::milliseconds::zero()));
testsTurn.release(); testsTurn.release();
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(recursiveMutex.try_lock_for(waitTimeAsDuration)); QVERIFY(recursiveMutex.try_lock_for(waitTimeAsDuration));
QVERIFY(timer.elapsed() <= waitTime + systemTimersResolution); QCOMPARE_LE(timer.elapsed(), waitTime + systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(recursiveMutex.try_lock_for(waitTimeAsDuration)); QVERIFY(recursiveMutex.try_lock_for(waitTimeAsDuration));
QVERIFY(lockCount.testAndSetRelaxed(1, 2)); QVERIFY(lockCount.testAndSetRelaxed(1, 2));
@ -621,7 +621,7 @@ void tst_QMutex::try_lock_for_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
timer.start(); timer.start();
QVERIFY(recursiveMutex.try_lock_for(std::chrono::milliseconds::zero())); QVERIFY(recursiveMutex.try_lock_for(std::chrono::milliseconds::zero()));
QVERIFY(timer.elapsed() < waitTime + systemTimersResolution); QCOMPARE_LT(timer.elapsed(), waitTime + systemTimersResolution);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(recursiveMutex.try_lock_for(std::chrono::milliseconds::zero())); QVERIFY(recursiveMutex.try_lock_for(std::chrono::milliseconds::zero()));
QVERIFY(lockCount.testAndSetRelaxed(1, 2)); QVERIFY(lockCount.testAndSetRelaxed(1, 2));
@ -720,14 +720,14 @@ void tst_QMutex::try_lock_until_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
auto endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration; auto endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration;
QVERIFY(!recursiveMutex.try_lock_until(endTimePoint)); QVERIFY(!recursiveMutex.try_lock_until(endTimePoint));
QVERIFY(std::chrono::steady_clock::now() >= endTimePoint - systemTimersResolutionAsDuration); QCOMPARE_GE(std::chrono::steady_clock::now(), endTimePoint - systemTimersResolutionAsDuration);
QVERIFY(!recursiveMutex.try_lock()); QVERIFY(!recursiveMutex.try_lock());
testsTurn.release(); testsTurn.release();
threadsTurn.acquire(); threadsTurn.acquire();
endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration; endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration;
QVERIFY(recursiveMutex.try_lock_until(endTimePoint)); QVERIFY(recursiveMutex.try_lock_until(endTimePoint));
QVERIFY(std::chrono::steady_clock::now() <= endTimePoint + systemTimersResolutionAsDuration); QCOMPARE_LE(std::chrono::steady_clock::now(), endTimePoint + systemTimersResolutionAsDuration);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration; endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration;
QVERIFY(recursiveMutex.try_lock_until(endTimePoint)); QVERIFY(recursiveMutex.try_lock_until(endTimePoint));
@ -746,7 +746,7 @@ void tst_QMutex::try_lock_until_recursive()
threadsTurn.acquire(); threadsTurn.acquire();
endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration; endTimePoint = std::chrono::steady_clock::now() + waitTimeAsDuration;
QVERIFY(recursiveMutex.try_lock_until(std::chrono::steady_clock::now())); QVERIFY(recursiveMutex.try_lock_until(std::chrono::steady_clock::now()));
QVERIFY(std::chrono::steady_clock::now() <= endTimePoint + systemTimersResolutionAsDuration); QCOMPARE_LE(std::chrono::steady_clock::now(), endTimePoint + systemTimersResolutionAsDuration);
QVERIFY(lockCount.testAndSetRelaxed(0, 1)); QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(recursiveMutex.try_lock_until(std::chrono::steady_clock::now())); QVERIFY(recursiveMutex.try_lock_until(std::chrono::steady_clock::now()));
QVERIFY(lockCount.testAndSetRelaxed(1, 2)); QVERIFY(lockCount.testAndSetRelaxed(1, 2));