|
10 | 10 | from asphalt.exceptions.component import ExceptionReporterComponent
|
11 | 11 |
|
12 | 12 |
|
| 13 | +class DummyExceptionReporter(ExceptionReporter): |
| 14 | + def report_exception( |
| 15 | + self, ctx: Context, exception: BaseException, message: str, extra=None |
| 16 | + ) -> None: |
| 17 | + self.reported_exception = exception |
| 18 | + |
| 19 | + |
13 | 20 | @pytest.mark.parametrize(
|
14 | 21 | "install_default_handler", [True, False], ids=["default", "nodefault"]
|
15 | 22 | )
|
@@ -69,25 +76,25 @@ async def test_default_exception_handler(event_loop):
|
69 | 76 | exception handler.
|
70 | 77 |
|
71 | 78 | """
|
72 |
| - reported_exception = reported_message = None |
73 | 79 |
|
74 | 80 | async def fail_task():
|
75 | 81 | return 1 / 0
|
76 | 82 |
|
77 |
| - class DummyExceptionReporter(ExceptionReporter): |
78 |
| - def report_exception( |
79 |
| - self, ctx: Context, exception: BaseException, message: str, extra=None |
80 |
| - ) -> None: |
81 |
| - nonlocal reported_exception, reported_message |
82 |
| - reported_exception = exception |
83 |
| - reported_message = message |
84 |
| - |
85 | 83 | async with Context() as ctx:
|
86 | 84 | component = ExceptionReporterComponent(backend=DummyExceptionReporter)
|
87 | 85 | await component.start(ctx)
|
| 86 | + reporter = ctx.get_resource(ExceptionReporter) |
88 | 87 | event_loop.create_task(fail_task())
|
89 | 88 | await sleep(0.1)
|
90 | 89 | gc.collect()
|
91 | 90 |
|
92 |
| - assert isinstance(reported_exception, ZeroDivisionError) |
93 |
| - assert reported_message == "Task exception was never retrieved" |
| 91 | + assert isinstance(reporter.reported_exception, ZeroDivisionError) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.asyncio |
| 95 | +async def test_default_exception_handler_no_exception(event_loop): |
| 96 | + async with Context() as ctx: |
| 97 | + component = ExceptionReporterComponent(backend=DummyExceptionReporter) |
| 98 | + await component.start(ctx) |
| 99 | + handler = event_loop.get_exception_handler() |
| 100 | + handler(event_loop, {"message": "dummy"}) |
0 commit comments