Skip to content

Commit

Permalink
Improve stability of QgsTask::waitForFinished test
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 12, 2017
1 parent 4180846 commit f984242
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions python/core/qgstaskmanager.sip
Expand Up @@ -175,6 +175,8 @@ class QgsTask : QObject
Blocks the current thread until the task finishes or a maximum of ``timeout`` milliseconds.
If the ``timeout`` is ``-1`` the thread will be blocked forever.
In case of a timeout, the task will still be running.
In case the task already is finished, the method will return immediately while
returning ``true``.

The result will be false if the wait timed out and true in any other case.
:rtype: bool
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgstaskmanager.cpp
Expand Up @@ -158,6 +158,8 @@ bool QgsTask::waitForFinished( int timeout )
connect( &timer, &QTimer::timeout, &loop, &QEventLoop::quit );
}

if ( status() == QgsTask::Complete || status() == QgsTask::Terminated )
return true;
loop.exec();

return rv;
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgstaskmanager.h
Expand Up @@ -190,6 +190,8 @@ class CORE_EXPORT QgsTask : public QObject
* Blocks the current thread until the task finishes or a maximum of \a timeout milliseconds.
* If the \a timeout is ``-1`` the thread will be blocked forever.
* In case of a timeout, the task will still be running.
* In case the task already is finished, the method will return immediately while
* returning ``true``.
*
* The result will be false if the wait timed out and true in any other case.
*/
Expand Down
3 changes: 0 additions & 3 deletions tests/src/core/testqgstaskmanager.cpp
Expand Up @@ -671,7 +671,6 @@ void TestQgsTaskManager::waitForFinished()
QTimer timer;
connect( &timer, &QTimer::timeout, finishedTask, &ProgressReportingTask::finish );
timer.start( 100 );
QCOMPARE( finishedTask->status(), QgsTask::Running );
QCOMPARE( finishedTask->waitForFinished(), true );
QCOMPARE( finishedTask->status(), QgsTask::Complete );

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

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

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

connect( &timer, &QTimer::timeout, timeoutTooShortTask, &ProgressReportingTask::finish );
timer.start( 1000 );
QCOMPARE( timeoutTooShortTask->status(), QgsTask::Running );
QCOMPARE( timeoutTooShortTask->waitForFinished( 20 ), false );
QCOMPARE( timeoutTooShortTask->status(), QgsTask::Running );
}
Expand Down

0 comments on commit f984242

Please sign in to comment.