Skip to content
Permalink
Browse files

Fix QgsTask::waitForFinished test

  • Loading branch information
nyalldawson committed Aug 7, 2018
1 parent 9b4be7a commit 42332f2dc2c3812d59605b6ac7ec703ab5aa7b4a
Showing with 46 additions and 10 deletions.
  1. +46 −10 tests/src/core/testqgstaskmanager.cpp
@@ -730,9 +730,6 @@ void TestQgsTaskManager::taskId()

void TestQgsTaskManager::waitForFinished()
{
if ( QgsTest::isTravis() )
QSKIP( "This test is disabled on Travis CI environment" );

QgsTaskManager manager;
QEventLoop loop;

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

QTimer timer;
connect( &timer, &QTimer::timeout, finishedTask, &ProgressReportingTask::finish );
timer.start( 100 );
// we have to run the timer in a different thread, because waitForFinished will block this thread, meaning the
// event loop never runs and the timer timeout signal never gets through...
QThread *timerThread = new QThread();
connect( timerThread, &QThread::finished, timerThread, &QThread::deleteLater );
QTimer *timer = new QTimer( nullptr );
connect( timer, &QTimer::timeout, finishedTask, &ProgressReportingTask::finish, Qt::DirectConnection );
timer->moveToThread( timerThread );
connect( timerThread, &QThread::started, timer, [ = ]
{
timer->start( 2000 );
} );
connect( timerThread, &QThread::finished, timer, &QTimer::deleteLater );
timerThread->start();

QCOMPARE( finishedTask->waitForFinished(), true );
QCOMPARE( finishedTask->status(), QgsTask::Complete );

timerThread->quit();

ProgressReportingTask *failedTask = new ProgressReportingTask();
connect( failedTask, &ProgressReportingTask::begun, &loop, &QEventLoop::quit );
manager.addTask( failedTask );
if ( failedTask->status() != QgsTask::Running )
loop.exec();

connect( &timer, &QTimer::timeout, failedTask, &ProgressReportingTask::terminate );
timer.start( 100 );
timerThread = new QThread();
connect( timerThread, &QThread::finished, timerThread, &QThread::deleteLater );
timer = new QTimer( nullptr );
connect( timer, &QTimer::timeout, failedTask, &ProgressReportingTask::terminate, Qt::DirectConnection );
timer->moveToThread( timerThread );
connect( timerThread, &QThread::started, timer, [ = ]
{
timer->start( 500 );
} );
connect( timerThread, &QThread::finished, timer, &QTimer::deleteLater );
timerThread->start();

QCOMPARE( failedTask->waitForFinished(), true );
QCOMPARE( failedTask->status(), QgsTask::Terminated );

timerThread->quit();

ProgressReportingTask *timeoutTooShortTask = new ProgressReportingTask();
connect( timeoutTooShortTask, &ProgressReportingTask::begun, &loop, &QEventLoop::quit );
manager.addTask( timeoutTooShortTask );
if ( timeoutTooShortTask->status() != QgsTask::Running )
loop.exec();

connect( &timer, &QTimer::timeout, timeoutTooShortTask, &ProgressReportingTask::finish );
timer.start( 1000 );
timerThread = new QThread();
connect( timerThread, &QThread::finished, timerThread, &QThread::deleteLater );
timer = new QTimer( nullptr );
connect( timer, &QTimer::timeout, timeoutTooShortTask, &ProgressReportingTask::finish, Qt::DirectConnection );
timer->moveToThread( timerThread );
connect( timerThread, &QThread::started, timer, [ = ]
{
timer->start( 1000 );
} );
connect( timerThread, &QThread::finished, timer, &QTimer::deleteLater );
timerThread->start();

QCOMPARE( timeoutTooShortTask->waitForFinished( 20 ), false );
QCOMPARE( timeoutTooShortTask->status(), QgsTask::Running );

timerThread->quit();

flushEvents();
}

void TestQgsTaskManager::progressChanged()

0 comments on commit 42332f2

Please sign in to comment.
You can’t perform that action at this time.