summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2017-02-10 10:22:40 +0100
committerMarc Mutz <marc.mutz@kdab.com>2017-02-15 17:02:43 +0000
commitdca21964bec79a4415b406526a38c59ab4c56798 (patch)
treedf9c36033c888a5665108a4b0d8d8f5b99815174 /tests
parent4f286cb793ab2423476953699eba02441e06fcd7 (diff)
Wait for runnables to start up in tst_QThreadPool::cancel()
In order to get reproducible runs of the test, we need to wait in the main thread until all runnables have started executing. Otherwise, what the cancel() loop below actually does will vary from run to run. Change-Id: Ib912b0943e7bbd55c9480ae6fd4011ba20ac457e Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 410a14cc768ad084a4e474ffd8b61471405fce0f)
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp13
1 files changed, 10 insertions, 3 deletions
diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
index 3396e268a2..5516ee2029 100644
--- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
+++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
@@ -962,16 +962,19 @@ void tst_QThreadPool::clear()
void tst_QThreadPool::cancel()
{
QSemaphore sem(0);
+ QSemaphore startedThreads(0);
+
class BlockingRunnable : public QRunnable
{
public:
QSemaphore & sem;
+ QSemaphore &startedThreads;
QAtomicInt &dtorCounter;
QAtomicInt &runCounter;
int dummy;
- explicit BlockingRunnable(QSemaphore &s, QAtomicInt &c, QAtomicInt &r)
- : sem(s), dtorCounter(c), runCounter(r){}
+ explicit BlockingRunnable(QSemaphore &s, QSemaphore &started, QAtomicInt &c, QAtomicInt &r)
+ : sem(s), startedThreads(started), dtorCounter(c), runCounter(r){}
~BlockingRunnable()
{
@@ -980,6 +983,7 @@ void tst_QThreadPool::cancel()
void run()
{
+ startedThreads.release();
runCounter.fetchAndAddRelaxed(1);
sem.acquire();
count.ref();
@@ -999,11 +1003,14 @@ void tst_QThreadPool::cancel()
QAtomicInt dtorCounter = 0;
QAtomicInt runCounter = 0;
for (int i = 0; i < runs; i++) {
- runnables[i] = new BlockingRunnable(sem, dtorCounter, runCounter);
+ runnables[i] = new BlockingRunnable(sem, startedThreads, dtorCounter, runCounter);
runnables[i]->setAutoDelete(i != 0 && i != (runs-1)); //one which will run and one which will not
threadPool.cancel(runnables[i]); //verify NOOP for jobs not in the queue
threadPool.start(runnables[i]);
}
+ // wait for all worker threads to have started up:
+ QVERIFY(startedThreads.tryAcquire(MaxThreadCount, 60*1000 /* 1min */));
+
for (int i = 0; i < runs; i++) {
threadPool.cancel(runnables[i]);
}