|
9 | 9 | # without thread support. |
10 | 10 | test.support.import_module('threading') |
11 | 11 |
|
| 12 | +import io |
| 13 | +import logging |
12 | 14 | import multiprocessing |
13 | 15 | import sys |
14 | 16 | import threading |
|
21 | 23 |
|
22 | 24 | from concurrent import futures |
23 | 25 | from concurrent.futures._base import ( |
24 | | - PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future, wait) |
| 26 | + PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future, |
| 27 | + LOGGER, STDERR_HANDLER, wait) |
25 | 28 | import concurrent.futures.process |
26 | 29 |
|
27 | 30 | def create_future(state=PENDING, exception=None, result=None): |
@@ -617,24 +620,33 @@ def fn(callback_future): |
617 | 620 | self.assertTrue(was_cancelled) |
618 | 621 |
|
619 | 622 | def test_done_callback_raises(self): |
620 | | - raising_was_called = False |
621 | | - fn_was_called = False |
622 | | - |
623 | | - def raising_fn(callback_future): |
624 | | - nonlocal raising_was_called |
625 | | - raising_was_called = True |
626 | | - raise Exception('doh!') |
627 | | - |
628 | | - def fn(callback_future): |
629 | | - nonlocal fn_was_called |
630 | | - fn_was_called = True |
631 | | - |
632 | | - f = Future() |
633 | | - f.add_done_callback(raising_fn) |
634 | | - f.add_done_callback(fn) |
635 | | - f.set_result(5) |
636 | | - self.assertTrue(raising_was_called) |
637 | | - self.assertTrue(fn_was_called) |
| 623 | + LOGGER.removeHandler(STDERR_HANDLER) |
| 624 | + logging_stream = io.StringIO() |
| 625 | + handler = logging.StreamHandler(logging_stream) |
| 626 | + LOGGER.addHandler(handler) |
| 627 | + try: |
| 628 | + raising_was_called = False |
| 629 | + fn_was_called = False |
| 630 | + |
| 631 | + def raising_fn(callback_future): |
| 632 | + nonlocal raising_was_called |
| 633 | + raising_was_called = True |
| 634 | + raise Exception('doh!') |
| 635 | + |
| 636 | + def fn(callback_future): |
| 637 | + nonlocal fn_was_called |
| 638 | + fn_was_called = True |
| 639 | + |
| 640 | + f = Future() |
| 641 | + f.add_done_callback(raising_fn) |
| 642 | + f.add_done_callback(fn) |
| 643 | + f.set_result(5) |
| 644 | + self.assertTrue(raising_was_called) |
| 645 | + self.assertTrue(fn_was_called) |
| 646 | + self.assertIn('Exception: doh!', logging_stream.getvalue()) |
| 647 | + finally: |
| 648 | + LOGGER.removeHandler(handler) |
| 649 | + LOGGER.addHandler(STDERR_HANDLER) |
638 | 650 |
|
639 | 651 | def test_done_callback_already_successful(self): |
640 | 652 | callback_result = None |
|
0 commit comments