def test_done_callback_raises(self): LOGGER.removeHandler(STDERR_HANDLER) logging_stream = io.StringIO() handler = logging.StreamHandler(logging_stream) LOGGER.addHandler(handler) try: raising_was_called = False fn_was_called = False def raising_fn(callback_future): nonlocal raising_was_called raising_was_called = True raise Exception('doh!') def fn(callback_future): nonlocal fn_was_called fn_was_called = True f = Future() f.add_done_callback(raising_fn) f.add_done_callback(fn) f.set_result(5) self.assertTrue(raising_was_called) self.assertTrue(fn_was_called) self.assertIn('Exception: doh!', logging_stream.getvalue()) finally: LOGGER.removeHandler(handler) LOGGER.addHandler(STDERR_HANDLER)
def test_old_style_exception(self): class OldStyle: # Does not inherit from object def __str__(self): return 'doh!' callback_exc_info = [None] def fn(callback_future): callback_exc_info[0] = callback_future.exception_info() f = Future() f.add_done_callback(fn) try: raise OldStyle() except OldStyle: want_exc_info = sys.exc_info() f.set_exception_info(*want_exc_info[1:]) self.assertEqual(f.exception_info(), want_exc_info[1:]) self.assertEqual(callback_exc_info[0], want_exc_info[1:]) try: f.result() except OldStyle: got_exc_info = sys.exc_info() else: self.fail('OldStyle exception not raised') self.assertEqual(got_exc_info[:2], want_exc_info[:2]) got_tb = traceback.extract_tb(got_exc_info[2]) want_tb = traceback.extract_tb(want_exc_info[2]) self.assertEqual(got_tb[-len(want_tb):], want_tb)
def test_done_callback_already_cancelled(self): was_cancelled = [None] def fn(callback_future): was_cancelled[0] = callback_future.cancelled() f = Future() self.assertTrue(f.cancel()) f.add_done_callback(fn) self.assertTrue(was_cancelled[0])
def test_done_callback_already_failed(self): callback_exception = [None] def fn(callback_future): callback_exception[0] = callback_future.exception() f = Future() f.set_exception(Exception('test')) f.add_done_callback(fn) self.assertEqual(('test',), callback_exception[0].args)
def test_done_callback_already_successful(self): callback_result = [None] def fn(callback_future): callback_result[0] = callback_future.result() f = Future() f.set_result(5) f.add_done_callback(fn) self.assertEqual(5, callback_result[0])
def test_done_callback_with_result(self): callback_result = [None] def fn(callback_future): callback_result[0] = callback_future.result() f = Future() f.add_done_callback(fn) f.set_result(5) self.assertEqual(5, callback_result[0])
def test_done_callback_already_successful(self): self.callback_result = None def fn(callback_future): self.callback_result = callback_future.result() f = Future() f.set_result(5) f.add_done_callback(fn) self.assertEquals(5, self.callback_result)
def test_done_callback_already_cancelled(self): self.was_cancelled = None def fn(callback_future): self.was_cancelled = callback_future.cancelled() f = Future() self.assertTrue(f.cancel()) f.add_done_callback(fn) self.assertTrue(self.was_cancelled)
def test_done_callback_already_failed(self): self.callback_exception = None def fn(callback_future): self.callback_exception = callback_future.exception() f = Future() f.set_exception(Exception('test')) f.add_done_callback(fn) self.assertEquals(('test',), self.callback_exception.args)
def test_done_callback_with_result(self): self.callback_result = None def fn(callback_future): self.callback_result = callback_future.result() f = Future() f.add_done_callback(fn) f.set_result(5) self.assertEquals(5, self.callback_result)
def test_done_callback_with_cancel(self): was_cancelled = None def fn(callback_future): nonlocal was_cancelled was_cancelled = callback_future.cancelled() f = Future() f.add_done_callback(fn) self.assertTrue(f.cancel()) self.assertTrue(was_cancelled)
def test_done_callback_with_exception(self): callback_exception = [None] def fn(callback_future): callback_exception[0] = callback_future.exception() f = Future() f.add_done_callback(fn) f.set_exception(Exception("test")) self.assertEqual(("test",), callback_exception[0].args)
def test_done_callback_with_exception(self): callback_exception = None def fn(callback_future): nonlocal callback_exception callback_exception = callback_future.exception() f = Future() f.add_done_callback(fn) f.set_exception(Exception('test')) self.assertEqual(('test',), callback_exception.args)
def test_done_callback_already_failed(self): callback_exception = None def fn(callback_future): nonlocal callback_exception callback_exception = callback_future.exception() f = Future() f.set_exception(Exception("test")) f.add_done_callback(fn) self.assertEqual(("test",), callback_exception.args)
def test_done_callback_raises(self): with captured_stderr() as stderr: raising_was_called = [False] fn_was_called = [False] def raising_fn(callback_future): raising_was_called[0] = True raise Exception("doh!") def fn(callback_future): fn_was_called[0] = True f = Future() f.add_done_callback(raising_fn) f.add_done_callback(fn) f.set_result(5) self.assertTrue(raising_was_called) self.assertTrue(fn_was_called) self.assertIn("Exception: doh!", stderr.getvalue())
def test_done_callback_raises(self): with test.support.captured_stderr() as stderr: raising_was_called = False fn_was_called = False def raising_fn(callback_future): nonlocal raising_was_called raising_was_called = True raise Exception('doh!') def fn(callback_future): nonlocal fn_was_called fn_was_called = True f = Future() f.add_done_callback(raising_fn) f.add_done_callback(fn) f.set_result(5) self.assertTrue(raising_was_called) self.assertTrue(fn_was_called) self.assertIn('Exception: doh!', stderr.getvalue())
def test_done_callback_raises(self): with test.support.captured_stderr() as stderr: raising_was_called = False fn_was_called = False def raising_fn(callback_future): nonlocal raising_was_called raising_was_called = True raise Exception('doh!') def fn(callback_future): nonlocal fn_was_called fn_was_called = True f = Future() f.add_done_callback(raising_fn) f.add_done_callback(fn) f.set_result(5) self.assertTrue(raising_was_called) self.assertTrue(fn_was_called) self.assertIn('Exception: doh!', stderr.getvalue())
def test_done_callback_raises(self): with captured_stderr() as stderr: raising_was_called = [False] raising_old_style_was_called = [False] fn_was_called = [False] def raising_fn(callback_future): raising_was_called[0] = True raise Exception('doh!') def raising_old_style_fn(callback_future): raising_old_style_was_called[0] = True class OldStyle: # Does not inherit from object def __str__(self): return 'doh!' raise OldStyle() def fn(callback_future): fn_was_called[0] = True f = Future() f.add_done_callback(raising_fn) f.add_done_callback(raising_old_style_fn) f.add_done_callback(fn) f.set_result(5) self.assertTrue(raising_was_called) self.assertTrue(raising_old_style_was_called) self.assertTrue(fn_was_called) self.assertIn('Exception: doh!', stderr.getvalue()) self.assertIn('OldStyle: doh!', stderr.getvalue())
def test_done_callback_raises(self): with captured_stderr() as stderr: raising_was_called = [False] raising_old_style_was_called = [False] fn_was_called = [False] def raising_fn(callback_future): raising_was_called[0] = True raise Exception('doh!') def raising_old_style_fn(callback_future): raising_old_style_was_called[0] = True class OldStyle: # Does not inherit from object def __str__(self): return 'doh!' raise OldStyle() def fn(callback_future): fn_was_called[0] = True f = Future() f.add_done_callback(raising_fn) f.add_done_callback(raising_old_style_fn) f.add_done_callback(fn) f.set_result(5) self.assertTrue(raising_was_called) self.assertTrue(raising_old_style_was_called) self.assertTrue(fn_was_called) self.assertIn('Exception: doh!', stderr.getvalue()) self.assertIn('OldStyle: doh!', stderr.getvalue())