Skip to content
Permalink
Browse files
Call QgsTaskWrapper.on_finished with exception rather than result
  • Loading branch information
nyalldawson committed Dec 5, 2016
1 parent fded1a1 commit 619381c0ad5a7ac32bb50527992b84e7da6c1054
Showing with 50 additions and 5 deletions.
  1. +5 −2 python/core/__init__.py
  2. +45 −3 tests/src/python/test_qgstaskmanager.py
@@ -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

@@ -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

@@ -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
@@ -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)

0 comments on commit 619381c

Please sign in to comment.