summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/thread
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@qt.io>2017-11-30 08:28:20 +0100
committerLiang Qi <liang.qi@qt.io>2017-11-30 09:16:58 +0100
commit87204c856ae3d8495c41af998ac196a12fe1c779 (patch)
tree5af64eaa3acde506d19fe4ab1c88b028a2197adf /tests/auto/corelib/thread
parent498ef7f9d77fca75bcba2f657aaac38c8dc78aea (diff)
parentd0812cbdabcff4d388334c499c23992cce98d4da (diff)
Merge remote-tracking branch 'origin/5.10' into dev
Conflicts: src/corelib/thread/qsemaphore.cpp tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp Change-Id: Id35b535e88df63fdfe4007ea92ed4a39c4b6d707
Diffstat (limited to 'tests/auto/corelib/thread')
-rw-r--r--tests/auto/corelib/thread/qsemaphore/tst_qsemaphore.cpp63
-rw-r--r--tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp68
2 files changed, 131 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qsemaphore/tst_qsemaphore.cpp b/tests/auto/corelib/thread/qsemaphore/tst_qsemaphore.cpp
index ba470a77c9..a67ecc2471 100644
--- a/tests/auto/corelib/thread/qsemaphore/tst_qsemaphore.cpp
+++ b/tests/auto/corelib/thread/qsemaphore/tst_qsemaphore.cpp
@@ -41,6 +41,8 @@ private slots:
void tryAcquireWithTimeout_data();
void tryAcquireWithTimeout();
void tryAcquireWithTimeoutStarvation();
+ void tryAcquireWithTimeoutForever_data();
+ void tryAcquireWithTimeoutForever();
void producerConsumer();
void raii();
};
@@ -156,21 +158,25 @@ void tst_QSemaphore::tryAcquire()
semaphore.release();
QCOMPARE(semaphore.available(), 1);
QVERIFY(!semaphore.tryAcquire(2));
+ QVERIFY(!semaphore.tryAcquire(2, 0));
QCOMPARE(semaphore.available(), 1);
semaphore.release();
QCOMPARE(semaphore.available(), 2);
QVERIFY(!semaphore.tryAcquire(3));
+ QVERIFY(!semaphore.tryAcquire(3, 0));
QCOMPARE(semaphore.available(), 2);
semaphore.release(10);
QCOMPARE(semaphore.available(), 12);
QVERIFY(!semaphore.tryAcquire(100));
+ QVERIFY(!semaphore.tryAcquire(100, 0));
QCOMPARE(semaphore.available(), 12);
semaphore.release(10);
QCOMPARE(semaphore.available(), 22);
QVERIFY(!semaphore.tryAcquire(100));
+ QVERIFY(!semaphore.tryAcquire(100, 0));
QCOMPARE(semaphore.available(), 22);
QVERIFY(semaphore.tryAcquire());
@@ -179,23 +185,38 @@ void tst_QSemaphore::tryAcquire()
QVERIFY(semaphore.tryAcquire());
QCOMPARE(semaphore.available(), 20);
+ semaphore.release(2);
+ QVERIFY(semaphore.tryAcquire(1, 0));
+ QCOMPARE(semaphore.available(), 21);
+
+ QVERIFY(semaphore.tryAcquire(1, 0));
+ QCOMPARE(semaphore.available(), 20);
+
QVERIFY(semaphore.tryAcquire(10));
QCOMPARE(semaphore.available(), 10);
+ semaphore.release(10);
+ QVERIFY(semaphore.tryAcquire(10, 0));
+ QCOMPARE(semaphore.available(), 10);
+
QVERIFY(semaphore.tryAcquire(10));
QCOMPARE(semaphore.available(), 0);
// should not be able to acquire more
QVERIFY(!semaphore.tryAcquire());
+ QVERIFY(!semaphore.tryAcquire(1, 0));
QCOMPARE(semaphore.available(), 0);
QVERIFY(!semaphore.tryAcquire());
+ QVERIFY(!semaphore.tryAcquire(1, 0));
QCOMPARE(semaphore.available(), 0);
QVERIFY(!semaphore.tryAcquire(10));
+ QVERIFY(!semaphore.tryAcquire(10, 0));
QCOMPARE(semaphore.available(), 0);
QVERIFY(!semaphore.tryAcquire(10));
+ QVERIFY(!semaphore.tryAcquire(10, 0));
QCOMPARE(semaphore.available(), 0);
}
@@ -345,6 +366,48 @@ void tst_QSemaphore::tryAcquireWithTimeoutStarvation()
QVERIFY(consumer.wait());
}
+void tst_QSemaphore::tryAcquireWithTimeoutForever_data()
+{
+ QTest::addColumn<int>("timeout");
+ QTest::newRow("-1") << -1;
+
+ // tryAcquire is documented to take any negative value as "forever"
+ QTest::newRow("INT_MIN") << INT_MIN;
+}
+
+void tst_QSemaphore::tryAcquireWithTimeoutForever()
+{
+ enum { WaitTime = 1000 };
+ struct Thread : public QThread {
+ QSemaphore sem;
+
+ void run() override
+ {
+ QTest::qWait(WaitTime);
+ sem.release(2);
+ }
+ };
+
+ QFETCH(int, timeout);
+ Thread t;
+
+ // sanity check it works if we can immediately acquire
+ t.sem.release(11);
+ QVERIFY(t.sem.tryAcquire(1, timeout));
+ QVERIFY(t.sem.tryAcquire(10, timeout));
+
+ // verify that we do wait for at least WaitTime if we can't acquire immediately
+ QElapsedTimer timer;
+ timer.start();
+ t.start();
+ QVERIFY(t.sem.tryAcquire(1, timeout));
+ QVERIFY(timer.elapsed() >= WaitTime);
+
+ QVERIFY(t.wait());
+
+ QCOMPARE(t.sem.available(), 1);
+}
+
const char alphabet[] = "ACGTH";
const int AlphabetSize = sizeof(alphabet) - 1;
diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
index 094c6ed0a5..1092216fb7 100644
--- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
+++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
@@ -95,6 +95,7 @@ private slots:
void stackSize();
void stressTest();
void takeAllAndIncreaseMaxThreadCount();
+ void waitForDoneAfterTake();
private:
QMutex m_functionTestMutex;
@@ -1267,5 +1268,72 @@ void tst_QThreadPool::takeAllAndIncreaseMaxThreadCount() {
delete task3;
}
+void tst_QThreadPool::waitForDoneAfterTake()
+{
+ class Task : public QRunnable
+ {
+ public:
+ Task(QSemaphore *mainBarrier, QSemaphore *threadBarrier)
+ : m_mainBarrier(mainBarrier)
+ , m_threadBarrier(threadBarrier)
+ {}
+
+ void run()
+ {
+ m_mainBarrier->release();
+ m_threadBarrier->acquire();
+ }
+
+ private:
+ QSemaphore *m_mainBarrier = nullptr;
+ QSemaphore *m_threadBarrier = nullptr;
+ };
+
+ int threadCount = 4;
+
+ // Blocks the main thread from releasing the threadBarrier before all run() functions have started
+ QSemaphore mainBarrier;
+ // Blocks the tasks from completing their run function
+ QSemaphore threadBarrier;
+
+ QThreadPool manager;
+ manager.setMaxThreadCount(threadCount);
+
+ // Fill all the threads with runnables that wait for the threadBarrier
+ for (int i = 0; i < threadCount; i++) {
+ auto *task = new Task(&mainBarrier, &threadBarrier);
+ manager.start(task);
+ }
+
+ QVERIFY(manager.activeThreadCount() == manager.maxThreadCount());
+
+ // Add runnables that are immediately removed from the pool queue.
+ // This sets the queue elements to nullptr in QThreadPool and we want to test that
+ // the threads keep going through the queue after encountering a nullptr.
+ for (int i = 0; i < threadCount; i++) {
+ QRunnable *runnable = createTask(emptyFunct);
+ manager.start(runnable);
+ QVERIFY(manager.tryTake(runnable));
+ }
+
+ // Add another runnable that will not be removed
+ manager.start(createTask(emptyFunct));
+
+ // Wait for the first runnables to start
+ mainBarrier.acquire(threadCount);
+
+ QVERIFY(mainBarrier.available() == 0);
+ QVERIFY(threadBarrier.available() == 0);
+
+ // Release runnables that are waiting and expect all runnables to complete
+ threadBarrier.release(threadCount);
+
+ // Using qFatal instead of QVERIFY to force exit if threads are still running after timeout.
+ // Otherwise, QCoreApplication will still wait for the stale threads and never exit the test.
+ if (!manager.waitForDone(5 * 60 * 1000))
+ qFatal("waitForDone returned false. Aborting to stop background threads.");
+
+}
+
QTEST_MAIN(tst_QThreadPool);
#include "tst_qthreadpool.moc"