diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-10-20 17:49:33 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-10-22 09:43:42 +0200 |
commit | 7d133a5613b7d9bae544fa56d26577c0c9d50ff4 (patch) | |
tree | e9d163bcea1f37cc5dae65b1e1eb8a58e53fb965 /tests/auto/corelib/thread | |
parent | bfe579613a12efe13be35b1a52975dddeb0d8026 (diff) |
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>
Diffstat (limited to 'tests/auto/corelib/thread')
-rw-r--r-- | tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp | 35 |
1 files 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; |