Skip to content

Commit

Permalink
fix waitforfinished when task is not started
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 authored and nyalldawson committed Nov 14, 2019
1 parent 0018190 commit 9887827
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/core/qgstaskmanager.cpp
Expand Up @@ -28,7 +28,9 @@
QgsTask::QgsTask( const QString &name, Flags flags )
: mFlags( flags )
, mDescription( name )
, mNotStartedMutex( 1 )
{
mNotStartedMutex.acquire();
}

QgsTask::~QgsTask()
Expand All @@ -41,6 +43,7 @@ QgsTask::~QgsTask()
delete subTask.task;
}
mNotFinishedMutex.unlock();
mNotStartedMutex.release();
}

void QgsTask::setDescription( const QString &description )
Expand All @@ -56,6 +59,7 @@ qint64 QgsTask::elapsedTime() const
void QgsTask::start()
{
mNotFinishedMutex.lock();
mNotStartedMutex.release();
mStartCount++;
Q_ASSERT( mStartCount == 1 );

Expand Down Expand Up @@ -152,6 +156,9 @@ QList<QgsMapLayer *> QgsTask::dependentLayers() const

bool QgsTask::waitForFinished( int timeout )
{
// We wait the task to be started
mNotStartedMutex.acquire();

bool rv = true;
if ( mOverallStatus == Complete || mOverallStatus == Terminated )
{
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgstaskmanager.h
Expand Up @@ -311,6 +311,12 @@ class CORE_EXPORT QgsTask : public QObject
*/
QMutex mNotFinishedMutex;

/**
* This semaphore remains locked from task creation until the task actually start,
* it's used in waitForFinished to actually wait the task to be started.
*/
QSemaphore mNotStartedMutex;

//! Progress of this (parent) task alone
double mProgress = 0.0;
//! Overall progress of this task and all subtasks
Expand Down
12 changes: 1 addition & 11 deletions tests/src/python/test_qgslayoutlegend.py
Expand Up @@ -396,10 +396,6 @@ def testSymbolExpressions(self):

counterTask = point_layer.countSymbolFeatures()
counterTask.waitForFinished()
TM = QgsApplication.taskManager()
actask = TM.activeTasks()
print(TM.tasks(), actask)
count = actask[0]
legend.model().refreshLayerLegend(legendlayer)
legendnodes = legend.model().layerLegendNodes(legendlayer)
legendnodes[0].setUserLabel('[% @symbol_id %]')
Expand All @@ -408,7 +404,6 @@ def testSymbolExpressions(self):
label1 = legendnodes[0].evaluateLabel()
label2 = legendnodes[1].evaluateLabel()
label3 = legendnodes[2].evaluateLabel()
count.waitForFinished()
self.assertEqual(label1, '0')
#self.assertEqual(label2, '5')
#self.assertEqual(label3, '12')
Expand Down Expand Up @@ -463,7 +458,7 @@ def testSymbolExpressionRender(self):
group = legend.model().rootGroup().addGroup("Group [% 1 + 5 %] [% @layout_name %]")
layer_tree_layer = group.addLayer(point_layer)
counterTask = point_layer.countSymbolFeatures()
counterTask.waitForFinished() # does this even work?
counterTask.waitForFinished()
layer_tree_layer.setCustomProperty("legend/title-label", 'bbbb [% 1+2 %] xx [% @layout_name %] [% @layer_name %]')
QgsMapLayerLegendUtils.setLegendNodeUserLabel(layer_tree_layer, 0, 'xxxx')
legend.model().refreshLayerLegend(layer_tree_layer)
Expand All @@ -475,11 +470,6 @@ def testSymbolExpressionRender(self):
legend.setLinkedMap(map)
legend.updateLegend()
print(layer_tree_layer.labelExpression())
TM = QgsApplication.taskManager()
actask = TM.activeTasks()
print(TM.tasks(), actask)
count = actask[0]
count.waitForFinished()
map.setExtent(QgsRectangle(-102.51, 41.16, -102.36, 41.30))
checker = QgsLayoutChecker(
'composer_legend_symbol_expression', layout)
Expand Down

0 comments on commit 9887827

Please sign in to comment.