From 7d133a5613b7d9bae544fa56d26577c0c9d50ff4 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 20 Oct 2021 17:49:33 +0200 Subject: 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 Reviewed-by: Thiago Macieira --- .../corelib/thread/qthreadpool/tst_qthreadpool.cpp | 35 ++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp index 404ebb68a1..db030c4db4 100644 --- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp +++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp @@ -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; -- cgit v1.2.3