Skip to content

Commit f984242

Browse files
committed
Improve stability of QgsTask::waitForFinished test
1 parent 4180846 commit f984242

File tree

4 files changed

+6
-3
lines changed

4 files changed

+6
-3
lines changed

python/core/qgstaskmanager.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ class QgsTask : QObject
175175
Blocks the current thread until the task finishes or a maximum of ``timeout`` milliseconds.
176176
If the ``timeout`` is ``-1`` the thread will be blocked forever.
177177
In case of a timeout, the task will still be running.
178+
In case the task already is finished, the method will return immediately while
179+
returning ``true``.
178180

179181
The result will be false if the wait timed out and true in any other case.
180182
:rtype: bool

src/core/qgstaskmanager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ bool QgsTask::waitForFinished( int timeout )
158158
connect( &timer, &QTimer::timeout, &loop, &QEventLoop::quit );
159159
}
160160

161+
if ( status() == QgsTask::Complete || status() == QgsTask::Terminated )
162+
return true;
161163
loop.exec();
162164

163165
return rv;

src/core/qgstaskmanager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class CORE_EXPORT QgsTask : public QObject
190190
* Blocks the current thread until the task finishes or a maximum of \a timeout milliseconds.
191191
* If the \a timeout is ``-1`` the thread will be blocked forever.
192192
* In case of a timeout, the task will still be running.
193+
* In case the task already is finished, the method will return immediately while
194+
* returning ``true``.
193195
*
194196
* The result will be false if the wait timed out and true in any other case.
195197
*/

tests/src/core/testqgstaskmanager.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,6 @@ void TestQgsTaskManager::waitForFinished()
671671
QTimer timer;
672672
connect( &timer, &QTimer::timeout, finishedTask, &ProgressReportingTask::finish );
673673
timer.start( 100 );
674-
QCOMPARE( finishedTask->status(), QgsTask::Running );
675674
QCOMPARE( finishedTask->waitForFinished(), true );
676675
QCOMPARE( finishedTask->status(), QgsTask::Complete );
677676

@@ -683,7 +682,6 @@ void TestQgsTaskManager::waitForFinished()
683682

684683
connect( &timer, &QTimer::timeout, failedTask, &ProgressReportingTask::terminate );
685684
timer.start( 100 );
686-
QCOMPARE( failedTask->status(), QgsTask::Running );
687685
QCOMPARE( failedTask->waitForFinished(), true );
688686
QCOMPARE( failedTask->status(), QgsTask::Terminated );
689687

@@ -695,7 +693,6 @@ void TestQgsTaskManager::waitForFinished()
695693

696694
connect( &timer, &QTimer::timeout, timeoutTooShortTask, &ProgressReportingTask::finish );
697695
timer.start( 1000 );
698-
QCOMPARE( timeoutTooShortTask->status(), QgsTask::Running );
699696
QCOMPARE( timeoutTooShortTask->waitForFinished( 20 ), false );
700697
QCOMPARE( timeoutTooShortTask->status(), QgsTask::Running );
701698
}

0 commit comments

Comments
 (0)