def test_unsuccessful_no_arg_operation_calls_error_and_finished_callback( self): def foo(): # this is a bad operation # that should appear in the stack trace raise RuntimeError("Bad operation") recv = AsyncTaskTest.Receiver() t = AsyncTask(foo, success_cb=recv.on_success, error_cb=recv.on_error, finished_cb=recv.on_finished) t.start() t.join() self.assertTrue(recv.finished_cb_called) self.assertFalse(recv.success_cb_called) self.assertTrue(recv.error_cb_called) self.assertTrue(isinstance(recv.task_exc, RuntimeError), msg="Expected RuntimeError, found " + recv.task_exc.__class__.__name__) self.assertEqual(2, len(recv.task_exc_stack)) # line number of self.target in asynchronous.py self.assertEqual(91, recv.task_exc_stack[0][1]) # line number of raise statement above self.assertEqual(95, recv.task_exc_stack[1][1])
def test_successful_no_arg_operation_calls_success_and_finished_callback(self): def foo(): return 42 recv = AsyncTaskTest.Receiver() t = AsyncTask(foo, success_cb=recv.on_success, error_cb=recv.on_error, finished_cb=recv.on_finished) t.start() t.join() self.assertTrue(recv.finished_cb_called) self.assertTrue(recv.success_cb_called) self.assertFalse(recv.error_cb_called) self.assertEqual(42, recv.task_output)
def test_correct_exception_is_raised_when_called_on_other_thread(self): self.exc = None self.exit_code = None def collect_error(e): self.exc = e thread = AsyncTask(self.task, error_cb=collect_error) thread.start() while thread.is_alive(): QApplication.processEvents() thread.join(0.5) self.assertTrue(isinstance(self.exc.exc_value, CustomException)) self.assertEqual(TaskExitCode.ERROR, thread.exit_code)
def test_successful_args_and_kwargs_operation_calls_success_and_finished_callback(self): def foo(scale, shift): return scale*42 + shift recv = AsyncTaskTest.Receiver() scale, shift = 2, 4 t = AsyncTask(foo, args = (scale,), kwargs={'shift': shift}, success_cb=recv.on_success, error_cb=recv.on_error, finished_cb=recv.on_finished) t.start() t.join() self.assertTrue(recv.finished_cb_called) self.assertTrue(recv.success_cb_called) self.assertFalse(recv.error_cb_called) self.assertEqual(scale*42 + shift, recv.task_output)
def test_unsuccessful_args_and_kwargs_operation_calls_error_and_finished_callback(self): def foo(scale, shift): raise RuntimeError("Bad operation") recv = AsyncTaskTest.Receiver() scale, shift = 2, 4 t = AsyncTask(foo, args = (scale,), kwargs={'shift': shift}, success_cb=recv.on_success, error_cb=recv.on_error, finished_cb=recv.on_finished) t.start() t.join() self.assertTrue(recv.finished_cb_called) self.assertFalse(recv.success_cb_called) self.assertTrue(recv.error_cb_called) self.assertTrue(isinstance(recv.task_exc, RuntimeError))
def test_successful_no_arg_operation_calls_success_and_finished_callback( self): def foo(): return 42 recv = AsyncTaskTest.Receiver() t = AsyncTask(foo, success_cb=recv.on_success, error_cb=recv.on_error, finished_cb=recv.on_finished) t.start() t.join() self.assertTrue(recv.finished_cb_called) self.assertTrue(recv.success_cb_called) self.assertFalse(recv.error_cb_called) self.assertEqual(42, recv.task_output)
def test_unsuccessful_operation_with_error_cb(self): def foo(scale, shift): def bar(): raise RuntimeError("Bad operation") bar() recv = AsyncTaskTest.Receiver() scale, shift = 2, 4 t = AsyncTask(foo, args = (scale,), kwargs={'shift': shift}, error_cb=recv.on_error) t.start() t.join() self.assertTrue(recv.error_cb_called) self.assertTrue(isinstance(recv.task_exc, RuntimeError)) self.assertEqual(3, len(recv.task_exc_stack)) self.assertEqual(135, recv.task_exc_stack[1][1]) self.assertEqual(134, recv.task_exc_stack[2][1])
def test_successful_positional_args_operation_calls_success_and_finished_callback( self): def foo(shift): return 42 + shift recv = AsyncTaskTest.Receiver() shift = 2 t = AsyncTask(foo, args=(shift, ), success_cb=recv.on_success, error_cb=recv.on_error, finished_cb=recv.on_finished) t.start() t.join() self.assertTrue(recv.finished_cb_called) self.assertTrue(recv.success_cb_called) self.assertFalse(recv.error_cb_called) self.assertEqual(42 + shift, recv.task_output)
def test_unsuccessful_args_and_kwargs_operation_calls_error_and_finished_callback( self): def foo(scale, shift): raise RuntimeError("Bad operation") recv = AsyncTaskTest.Receiver() scale, shift = 2, 4 t = AsyncTask(foo, args=(scale, ), kwargs={'shift': shift}, success_cb=recv.on_success, error_cb=recv.on_error, finished_cb=recv.on_finished) t.start() t.join() self.assertTrue(recv.finished_cb_called) self.assertFalse(recv.success_cb_called) self.assertTrue(recv.error_cb_called) self.assertTrue(isinstance(recv.task_exc, RuntimeError))
def test_unsuccessful_operation_with_error_cb(self): def foo(scale, shift): def bar(): raise RuntimeError("Bad operation") bar() recv = AsyncTaskTest.Receiver() scale, shift = 2, 4 t = AsyncTask(foo, args=(scale, ), kwargs={'shift': shift}, error_cb=recv.on_error) t.start() t.join() self.assertTrue(recv.error_cb_called) self.assertTrue(isinstance(recv.task_exc, RuntimeError)) self.assertEqual(3, len(recv.task_exc_stack)) self.assertEqual(135, recv.task_exc_stack[1][1]) self.assertEqual(134, recv.task_exc_stack[2][1])
def test_unsuccessful_no_arg_operation_calls_error_and_finished_callback(self): def foo(): # this is a bad operation # that should appear in the stack trace raise RuntimeError("Bad operation") recv = AsyncTaskTest.Receiver() t = AsyncTask(foo, success_cb=recv.on_success, error_cb=recv.on_error, finished_cb=recv.on_finished) t.start() t.join() self.assertTrue(recv.finished_cb_called) self.assertFalse(recv.success_cb_called) self.assertTrue(recv.error_cb_called) self.assertTrue(isinstance(recv.task_exc, RuntimeError), msg="Expected RuntimeError, found " + recv.task_exc.__class__.__name__) self.assertEqual(2, len(recv.task_exc_stack)) # line number of self.target in asynchronous.py self.assertEqual(91, recv.task_exc_stack[0][1]) # line number of raise statement above self.assertEqual(95, recv.task_exc_stack[1][1])