Skip to content

Commit

Permalink
Fix parent task progress report calculation when subtask progress cha…
Browse files Browse the repository at this point in the history
…nges

(cherry-picked from 95438cc)
  • Loading branch information
nyalldawson committed Aug 8, 2018
1 parent 2b6ef28 commit d16b5db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
10 changes: 6 additions & 4 deletions src/core/qgstaskmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ QgsTask::QgsTask( const QString &name, Flags flags )

QgsTask::~QgsTask()
{
Q_ASSERT_X( mStatus != Running, "delete", QString( "status was %1" ).arg( mStatus ).toLatin1() );
Q_ASSERT_X( mStatus != Running, "delete", QStringLiteral( "status was %1" ).arg( mStatus ).toLatin1() );

Q_FOREACH ( const SubTask &subTask, mSubTasks )
{
Expand Down Expand Up @@ -213,10 +213,12 @@ void QgsTask::setProgress( double progress )
}

// avoid flooding with too many events
if ( static_cast< int >( mTotalProgress * 10 ) != static_cast< int >( progress * 10 ) )
emit progressChanged( progress );

double prevProgress = mTotalProgress;
mTotalProgress = progress;

// avoid spamming with too many progressChanged reports
if ( static_cast< int >( prevProgress * 10 ) != static_cast< int >( mTotalProgress * 10 ) )
emit progressChanged( progress );
}

void QgsTask::completed()
Expand Down
27 changes: 17 additions & 10 deletions tests/src/core/testqgstaskmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ void TestQgsTaskManager::addTask()

task->cancel();
task2->cancel();

while ( manager.countActiveTasks() > 0 )
{
QCoreApplication::processEvents();
}
}

void TestQgsTaskManager::taskTerminationBeforeDelete()
Expand All @@ -396,6 +401,7 @@ void TestQgsTaskManager::taskTerminationBeforeDelete()
// if task is not terminated assert will trip
delete manager;
QApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
flushEvents();
}

void TestQgsTaskManager::taskFinished()
Expand All @@ -404,9 +410,8 @@ void TestQgsTaskManager::taskFinished()
// from main thread
QgsTaskManager manager;

bool *resultObtained = new bool;
*resultObtained = false;
FinishTask *task = new FinishTask( resultObtained );
bool resultObtained = false;
FinishTask *task = new FinishTask( &resultObtained );
task->desiredResult = true;
manager.addTask( task );
while ( task->status() == QgsTask::Running
Expand All @@ -419,9 +424,9 @@ void TestQgsTaskManager::taskFinished()
QCoreApplication::processEvents();
}
flushEvents();
QCOMPARE( *resultObtained, true );
QCOMPARE( resultObtained, true );

task = new FinishTask( resultObtained );
task = new FinishTask( &resultObtained );
task->desiredResult = false;
manager.addTask( task );

Expand All @@ -432,7 +437,7 @@ void TestQgsTaskManager::taskFinished()
QCoreApplication::processEvents();
}
flushEvents();
QCOMPARE( *resultObtained, false );
QCOMPARE( resultObtained, false );
}

void TestQgsTaskManager::subTask()
Expand Down Expand Up @@ -484,13 +489,15 @@ void TestQgsTaskManager::subTask()
// test progress calculation
QSignalSpy spy( parent, &QgsTask::progressChanged );
parent->emitProgressChanged( 50 );
flushEvents();
QCOMPARE( std::round( parent->progress() ), 17.0 );
//QCOMPARE( spy.count(), 1 );
QCOMPARE( spy.count(), 1 );
QCOMPARE( std::round( spy.last().at( 0 ).toDouble() ), 17.0 );

subTask->emitProgressChanged( 100 );
flushEvents();
QCOMPARE( std::round( parent->progress() ), 50.0 );
//QCOMPARE( spy.count(), 2 );
QCOMPARE( spy.count(), 2 );
QCOMPARE( std::round( spy.last().at( 0 ).toDouble() ), 50.0 );

subTask2->finish();
Expand All @@ -500,12 +507,12 @@ void TestQgsTaskManager::subTask()
}
flushEvents();
QCOMPARE( std::round( parent->progress() ), 83.0 );
//QCOMPARE( spy.count(), 3 );
QCOMPARE( spy.count(), 3 );
QCOMPARE( std::round( spy.last().at( 0 ).toDouble() ), 83.0 );

parent->emitProgressChanged( 100 );
QCOMPARE( std::round( parent->progress() ), 100.0 );
//QCOMPARE( spy.count(), 4 );
QCOMPARE( spy.count(), 4 );
QCOMPARE( std::round( spy.last().at( 0 ).toDouble() ), 100.0 );
parent->terminate();
subTask->terminate();
Expand Down

0 comments on commit d16b5db

Please sign in to comment.