Skip to content

Commit 42332f2

Browse files
committedAug 7, 2018
Fix QgsTask::waitForFinished test
1 parent 9b4be7a commit 42332f2

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed
 

‎tests/src/core/testqgstaskmanager.cpp

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -730,9 +730,6 @@ void TestQgsTaskManager::taskId()
730730

731731
void TestQgsTaskManager::waitForFinished()
732732
{
733-
if ( QgsTest::isTravis() )
734-
QSKIP( "This test is disabled on Travis CI environment" );
735-
736733
QgsTaskManager manager;
737734
QEventLoop loop;
738735

@@ -742,33 +739,72 @@ void TestQgsTaskManager::waitForFinished()
742739
if ( finishedTask->status() != QgsTask::Running )
743740
loop.exec();
744741

745-
QTimer timer;
746-
connect( &timer, &QTimer::timeout, finishedTask, &ProgressReportingTask::finish );
747-
timer.start( 100 );
742+
// we have to run the timer in a different thread, because waitForFinished will block this thread, meaning the
743+
// event loop never runs and the timer timeout signal never gets through...
744+
QThread *timerThread = new QThread();
745+
connect( timerThread, &QThread::finished, timerThread, &QThread::deleteLater );
746+
QTimer *timer = new QTimer( nullptr );
747+
connect( timer, &QTimer::timeout, finishedTask, &ProgressReportingTask::finish, Qt::DirectConnection );
748+
timer->moveToThread( timerThread );
749+
connect( timerThread, &QThread::started, timer, [ = ]
750+
{
751+
timer->start( 2000 );
752+
} );
753+
connect( timerThread, &QThread::finished, timer, &QTimer::deleteLater );
754+
timerThread->start();
755+
748756
QCOMPARE( finishedTask->waitForFinished(), true );
749757
QCOMPARE( finishedTask->status(), QgsTask::Complete );
750758

759+
timerThread->quit();
760+
751761
ProgressReportingTask *failedTask = new ProgressReportingTask();
752762
connect( failedTask, &ProgressReportingTask::begun, &loop, &QEventLoop::quit );
753763
manager.addTask( failedTask );
754764
if ( failedTask->status() != QgsTask::Running )
755765
loop.exec();
756766

757-
connect( &timer, &QTimer::timeout, failedTask, &ProgressReportingTask::terminate );
758-
timer.start( 100 );
767+
timerThread = new QThread();
768+
connect( timerThread, &QThread::finished, timerThread, &QThread::deleteLater );
769+
timer = new QTimer( nullptr );
770+
connect( timer, &QTimer::timeout, failedTask, &ProgressReportingTask::terminate, Qt::DirectConnection );
771+
timer->moveToThread( timerThread );
772+
connect( timerThread, &QThread::started, timer, [ = ]
773+
{
774+
timer->start( 500 );
775+
} );
776+
connect( timerThread, &QThread::finished, timer, &QTimer::deleteLater );
777+
timerThread->start();
778+
759779
QCOMPARE( failedTask->waitForFinished(), true );
760780
QCOMPARE( failedTask->status(), QgsTask::Terminated );
761781

782+
timerThread->quit();
783+
762784
ProgressReportingTask *timeoutTooShortTask = new ProgressReportingTask();
763785
connect( timeoutTooShortTask, &ProgressReportingTask::begun, &loop, &QEventLoop::quit );
764786
manager.addTask( timeoutTooShortTask );
765787
if ( timeoutTooShortTask->status() != QgsTask::Running )
766788
loop.exec();
767789

768-
connect( &timer, &QTimer::timeout, timeoutTooShortTask, &ProgressReportingTask::finish );
769-
timer.start( 1000 );
790+
timerThread = new QThread();
791+
connect( timerThread, &QThread::finished, timerThread, &QThread::deleteLater );
792+
timer = new QTimer( nullptr );
793+
connect( timer, &QTimer::timeout, timeoutTooShortTask, &ProgressReportingTask::finish, Qt::DirectConnection );
794+
timer->moveToThread( timerThread );
795+
connect( timerThread, &QThread::started, timer, [ = ]
796+
{
797+
timer->start( 1000 );
798+
} );
799+
connect( timerThread, &QThread::finished, timer, &QTimer::deleteLater );
800+
timerThread->start();
801+
770802
QCOMPARE( timeoutTooShortTask->waitForFinished( 20 ), false );
771803
QCOMPARE( timeoutTooShortTask->status(), QgsTask::Running );
804+
805+
timerThread->quit();
806+
807+
flushEvents();
772808
}
773809

774810
void TestQgsTaskManager::progressChanged()

0 commit comments

Comments
 (0)
Please sign in to comment.