diff options
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/thread/qmutex/tst_qmutex.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp b/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp index 7e3e720739..e51f8c9a23 100644 --- a/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp +++ b/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp @@ -57,6 +57,8 @@ private slots: void stressTest(); void tryLockRace(); void tryLockDeadlock(); + void tryLockNegative_data(); + void tryLockNegative(); void moreStress(); }; @@ -579,6 +581,55 @@ void tst_QMutex::tryLockDeadlock() QCOMPARE(tryLockDeadlockFailureCount, 0); } +void tst_QMutex::tryLockNegative_data() +{ + QTest::addColumn<int>("timeout"); + QTest::newRow("-1") << -1; + QTest::newRow("-2") << -2; + QTest::newRow("INT_MIN/2") << INT_MIN/2; + QTest::newRow("INT_MIN") << INT_MIN; +} + +void tst_QMutex::tryLockNegative() +{ + // the documentation says tryLock() with a negative number is the same as lock() + struct TrylockThread : QThread { + TrylockThread(QMutex &mut, int timeout) + : mut(mut), timeout(timeout), tryLockResult(-1) + {} + QMutex &mut; + int timeout; + int tryLockResult; + void run() { + tryLockResult = mut.tryLock(timeout); + mut.unlock(); + } + }; + + QFETCH(int, timeout); + + QMutex mutex; + TrylockThread thr(mutex, timeout); + QSignalSpy spy(&thr, SIGNAL(started())); + mutex.lock(); + thr.start(); + + // the thread should have stopped in tryLock(), waiting for us to unlock + // the mutex. The following test can be falsely positive due to timing: + // tryLock may still fail but hasn't failed yet. But it certainly cannot be + // a false negative: if wait() returns, tryLock failed. + QVERIFY(!thr.wait(200)); + + // after we unlock the mutex, the thread should succeed in locking, then + // unlock and exit. Do this before more tests to avoid deadlocking due to + // ~QThread waiting forever on a thread that won't exit. + mutex.unlock(); + + QCOMPARE(spy.count(), 1); + QVERIFY(thr.wait()); + QCOMPARE(thr.tryLockResult, 1); +} + class MoreStressTestThread : public QThread { |