Skip to content

Commit b64025d

Browse files
committed
Add python test for task manager
1 parent d270b4f commit b64025d

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

python/core/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,17 @@ def __exit__(self, ex_type, ex_value, traceback):
187187

188188
class QgsTaskWrapper(QgsTask):
189189

190-
def __init__(self, description, function, *extraArgs):
190+
def __init__(self, description, function, *args, **kwargs):
191191
QgsTask.__init__(self, description)
192-
self.extraArgs = extraArgs
192+
self.args = args
193+
self.kwargs = kwargs
193194
self.function = function
194195
self.result = None
195196
self.exception = None
196197

197198
def run(self):
198199
try:
199-
self.function(self, *self.extraArgs)
200+
self.result = self.function(self, *self.args, **self.kwargs)
200201
except Exception as ex:
201202
# report error
202203
self.exception = ex
@@ -206,7 +207,7 @@ def run(self):
206207
self.completed()
207208

208209

209-
def fromFunction(cls, description, function, extraArgs):
210-
return QgsTaskWrapper(description, function, extraArgs)
210+
def fromFunction(cls, description, function, *args, **kwargs):
211+
return QgsTaskWrapper(description, function, *args, **kwargs)
211212

212213
QgsTask.fromFunction = classmethod(fromFunction)

src/core/qgstaskmanager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ void QgsTask::unhold()
7171

7272
void QgsTask::setProgress( double progress )
7373
{
74+
7475
mProgress = progress;
7576
emit progressChanged( progress );
7677
}

tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ ADD_PYTHON_TEST(PyQgsSyntacticSugar test_syntactic_sugar.py)
108108
ADD_PYTHON_TEST(PyQgsStringUtils test_qgsstringutils.py)
109109
ADD_PYTHON_TEST(PyQgsSymbol test_qgssymbol.py)
110110
ADD_PYTHON_TEST(PyQgsSymbolLayerUtils test_qgssymbollayerutils.py)
111+
ADD_PYTHON_TEST(PyQgsTaskManager test_qgstaskmanager.py)
111112
ADD_PYTHON_TEST(PyQgsTextFormatWidget test_qgstextformatwidget.py)
112113
ADD_PYTHON_TEST(PyQgsTreeWidgetItem test_qgstreewidgetitem.py)
113114
ADD_PYTHON_TEST(PyQgsUnitTypes test_qgsunittypes.py)

tests/src/python/test_qgsfloatingwidget.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
__revision__ = '$Format:%H$'
1414

1515
import qgis # NOQA
16-
from PyQt.QtGui import QWidget, QGridLayout
16+
from PyQt.QtGui import QGridLayout
17+
from PyQt.QtWidgets import QWidget
1718

1819
import os
1920

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsTaskManager.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '26/04/2016'
11+
__copyright__ = 'Copyright 2016, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
import os
17+
18+
from qgis.core import (
19+
QgsTask,
20+
QgsTaskManager
21+
)
22+
23+
from qgis.testing import start_app, unittest
24+
from time import sleep
25+
26+
start_app()
27+
28+
29+
def run(task, result):
30+
if not result:
31+
raise Exception('cancelled')
32+
else:
33+
return result
34+
35+
36+
def run_with_kwargs(task, password, result):
37+
if not password == 1:
38+
raise Exception('bad password value')
39+
else:
40+
return result
41+
42+
43+
def cancellable(task):
44+
while not task.isCancelled():
45+
pass
46+
if task.isCancelled():
47+
raise Exception('cancelled')
48+
49+
50+
def progress_function(task):
51+
task.setProgress(50)
52+
53+
while not task.isCancelled():
54+
pass
55+
if task.isCancelled():
56+
raise Exception('cancelled')
57+
58+
59+
class TestQgsTaskManager(unittest.TestCase):
60+
61+
def testTaskFromFunction(self):
62+
""" test creating task from function """
63+
64+
task = QgsTask.fromFunction('test task', run, 20)
65+
QgsTaskManager.instance().addTask(task)
66+
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
67+
pass
68+
69+
self.assertEqual(task.result, 20)
70+
self.assertEqual(task.status(), QgsTask.Complete)
71+
72+
# try a task which cancels itself
73+
bad_task = QgsTask.fromFunction('test task', run)
74+
QgsTaskManager.instance().addTask(bad_task)
75+
while bad_task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
76+
pass
77+
78+
self.assertFalse(bad_task.result)
79+
self.assertEqual(bad_task.status(), QgsTask.Terminated)
80+
81+
def testTaskFromFunctionWithKwargs(self):
82+
""" test creating task from function using kwargs """
83+
84+
task = QgsTask.fromFunction('test task', run_with_kwargs, result=5, password=1)
85+
QgsTaskManager.instance().addTask(task)
86+
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
87+
pass
88+
89+
self.assertEqual(task.result, 5)
90+
self.assertEqual(task.status(), QgsTask.Complete)
91+
92+
def testTaskFromFunctionIsCancellable(self):
93+
""" test that task from function can check cancelled status """
94+
bad_task = QgsTask.fromFunction('test task', cancellable)
95+
QgsTaskManager.instance().addTask(bad_task)
96+
while bad_task.status() != QgsTask.Running:
97+
pass
98+
99+
bad_task.cancel()
100+
while bad_task.status() == QgsTask.Running:
101+
pass
102+
103+
self.assertEqual(bad_task.status(), QgsTask.Terminated)
104+
105+
def testTaskFromFunctionCanSetProgress(self):
106+
""" test that task from function can set progress """
107+
task = QgsTask.fromFunction('test task', progress_function)
108+
QgsTaskManager.instance().addTask(task)
109+
while task.status() != QgsTask.Running:
110+
pass
111+
112+
#wait a fraction so that setProgress gets a chance to be called
113+
sleep(0.001)
114+
self.assertEqual(task.progress(), 50)
115+
116+
task.cancel()
117+
while task.status() == QgsTask.Running:
118+
pass
119+
120+
121+
if __name__ == '__main__':
122+
unittest.main()

0 commit comments

Comments
 (0)