Skip to content

Commit

Permalink
Fix QgsTask::waitForFinished test
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2018
1 parent 9b4be7a commit 42332f2
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions tests/src/core/testqgstaskmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand Down

0 comments on commit 42332f2

Please sign in to comment.