Skip to content

Commit

Permalink
Call QgsTaskWrapper.on_finished with exception rather than result
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 5, 2016
1 parent fded1a1 commit 619381c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
7 changes: 5 additions & 2 deletions python/core/__init__.py
Expand Up @@ -222,11 +222,14 @@ def finished(self, result):
if not self.on_finished:
return

if result == QgsTask.ResultFail and self.exception is None:
self.exception = Exception('Task cancelled')

try:
if self.returned_values:
self.on_finished(result, self.returned_values)
self.on_finished(self.exception, self.returned_values)
else:
self.on_finished(result)
self.on_finished(self.exception)
except Exception as ex:
self.exception = ex

Expand Down
48 changes: 45 additions & 3 deletions tests/src/python/test_qgstaskmanager.py
Expand Up @@ -62,16 +62,27 @@ def run_no_result(task):
return


def finished_no_val(result):
def finished_no_val(e):
assert e is None
finished_no_val.called = True
return


def run_fail(task):
raise Exception('fail')


def finished_fail(e):
assert e
finished_fail.finished_exception = e


def run_single_val_result(task):
return 5


def finished_single_value_result(result, value):
def finished_single_value_result(e, value):
assert e is None
finished_single_value_result.value = value
return

Expand All @@ -80,7 +91,8 @@ def run_multiple_val_result(task):
return 5, 'whoo'


def finished_multiple_value_result(result, results):
def finished_multiple_value_result(e, results):
assert e is None
finished_multiple_value_result.value = results[0]
finished_multiple_value_result.statement = results[1]
return
Expand Down Expand Up @@ -178,6 +190,36 @@ def testTaskFromFunctionFinished(self):
self.assertFalse(task.exception)
self.assertTrue(finished_no_val.called)

def testTaskFromFunctionFinishedFail(self):
""" test that task from function which fails calls finished with exception"""
task = QgsTask.fromFunction('test task', run_fail, on_finished=finished_fail)
QgsApplication.taskManager().addTask(task)
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
pass
while QgsApplication.taskManager().countActiveTasks() > 0:
QCoreApplication.processEvents()

# check that the finished function was called
self.assertTrue(task.exception)
self.assertTrue(finished_fail.finished_exception)
self.assertEqual(task.exception, finished_fail.finished_exception)

def testTaskFromFunctionCancelledWhileQueued(self):
""" test that task from finished is called with exception when task is terminated while queued"""
task = QgsTask.fromFunction('test task', run_no_result, on_finished=finished_fail)
task.hold()
QgsApplication.taskManager().addTask(task)
task.cancel()
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
pass
while QgsApplication.taskManager().countActiveTasks() > 0:
QCoreApplication.processEvents()

# check that the finished function was called
self.assertTrue(task.exception)
self.assertTrue(finished_fail.finished_exception)
self.assertEqual(task.exception, finished_fail.finished_exception)

def testTaskFromFunctionFinishedWithVal(self):
""" test that task from function can have callback finished function and is passed result values"""
task = QgsTask.fromFunction('test task', run_single_val_result, on_finished=finished_single_value_result)
Expand Down

0 comments on commit 619381c

Please sign in to comment.