Consistently restore threadpool limit at end of tests

Fix warning about unused variable, and use qScopeGuard to make sure that
the limit of the global threadpool is restored even if one of the tests
fail.

Pick-to: 6.2
Change-Id: I36747cb451074cceea961561478210728ed6d313
Reviewed-by: Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Volker Hilsheimer 2021-10-20 17:49:33 +02:00
parent bfe579613a
commit 7d133a5613

View File

@ -466,6 +466,9 @@ void tst_QThreadPool::setMaxThreadCount()
QFETCH(int, limit);
QThreadPool *threadPool = QThreadPool::globalInstance();
int savedLimit = threadPool->maxThreadCount();
auto restoreThreadCount = qScopeGuard([=]{
threadPool->setMaxThreadCount(savedLimit);
});
// maxThreadCount() should always return the previous argument to
// setMaxThreadCount(), regardless of input
@ -570,7 +573,11 @@ void tst_QThreadPool::reserveThread()
{
QFETCH(int, limit);
QThreadPool *threadpool = QThreadPool::globalInstance();
int savedLimit = threadpool->maxThreadCount();
const int savedLimit = threadpool->maxThreadCount();
auto restoreThreadCount = qScopeGuard([=]{
threadpool->setMaxThreadCount(savedLimit);
});
threadpool->setMaxThreadCount(limit);
// reserve up to the limit
@ -619,9 +626,6 @@ void tst_QThreadPool::reserveThread()
while (threadpool2.activeThreadCount() > 0)
threadpool2.releaseThread();
}
// reset limit on global QThreadPool
threadpool->setMaxThreadCount(savedLimit);
}
void tst_QThreadPool::releaseThread_data()
@ -633,7 +637,10 @@ void tst_QThreadPool::releaseThread()
{
QFETCH(int, limit);
QThreadPool *threadpool = QThreadPool::globalInstance();
int savedLimit = threadpool->maxThreadCount();
const int savedLimit = threadpool->maxThreadCount();
auto restoreThreadCount = qScopeGuard([=]{
threadpool->setMaxThreadCount(savedLimit);
});
threadpool->setMaxThreadCount(limit);
// reserve up to the limit
@ -681,9 +688,6 @@ void tst_QThreadPool::releaseThread()
QCOMPARE(threadpool2.activeThreadCount(), 0);
QCOMPARE(threadpool->activeThreadCount(), 0);
}
// reset limit on global QThreadPool
threadpool->setMaxThreadCount(savedLimit);
}
void tst_QThreadPool::reserveAndStart() // QTBUG-21051
@ -708,6 +712,10 @@ void tst_QThreadPool::reserveAndStart() // QTBUG-21051
// Set up
QThreadPool *threadpool = QThreadPool::globalInstance();
int savedLimit = threadpool->maxThreadCount();
auto restoreThreadCount = qScopeGuard([=]{
threadpool->setMaxThreadCount(savedLimit);
});
threadpool->setMaxThreadCount(1);
QCOMPARE(threadpool->activeThreadCount(), 0);
@ -742,8 +750,6 @@ void tst_QThreadPool::reserveAndStart() // QTBUG-21051
task2.waitBeforeDone.release();
QTRY_COMPARE(threadpool->activeThreadCount(), 0);
threadpool->setMaxThreadCount(savedLimit);
}
void tst_QThreadPool::reserveAndStart2()
@ -764,6 +770,9 @@ void tst_QThreadPool::reserveAndStart2()
// Set up
QThreadPool *threadpool = QThreadPool::globalInstance();
int savedLimit = threadpool->maxThreadCount();
auto restoreThreadCount = qScopeGuard([=]{
threadpool->setMaxThreadCount(savedLimit);
});
threadpool->setMaxThreadCount(2);
// reserve
@ -808,6 +817,10 @@ void tst_QThreadPool::releaseAndBlock()
// Set up
QThreadPool *threadpool = QThreadPool::globalInstance();
const int savedLimit = threadpool->maxThreadCount();
auto restoreThreadCount = qScopeGuard([=]{
threadpool->setMaxThreadCount(savedLimit);
});
threadpool->setMaxThreadCount(1);
QCOMPARE(threadpool->activeThreadCount(), 0);
@ -834,8 +847,6 @@ void tst_QThreadPool::releaseAndBlock()
QCOMPARE(threadpool->activeThreadCount(), 1);
task1.waitBeforeDone.release();
QTRY_COMPARE(threadpool->activeThreadCount(), 0);
threadpool->setMaxThreadCount(savedLimit);
}
static QAtomicInt count;