Coverage for tests/utils/test_handler_call_wrapper.py: 90%

29 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1import gc 

2from collections.abc import Callable 

3from functools import wraps 

4from typing import Any 

5 

6import pytest 

7 

8from faststream._internal.configs import BrokerConfig 

9from faststream._internal.di import FastDependsConfig 

10from faststream._internal.endpoint.call_wrapper import HandlerCallWrapper 

11 

12 

13@pytest.mark.anyio() 

14@pytest.mark.parametrize("anyio_backend", ("asyncio",)) 

15async def test_handler_exception_does_not_leak_to_event_loop( 

16 anyio_backend: str, 

17) -> None: 

18 async def handler() -> None: 

19 return None 

20 

21 wrapper = HandlerCallWrapper(handler, BrokerConfig()) 

22 wrapper.set_test() 

23 error = ValueError("handler failed") 

24 

25 wrapper.trigger(error=error) 

26 del wrapper 

27 gc.collect() 

28 

29 

30@pytest.mark.asyncio() 

31async def test_handler_exception_remains_available_to_wait_call() -> None: 

32 async def handler() -> None: 

33 return None 

34 

35 wrapper = HandlerCallWrapper(handler, BrokerConfig()) 

36 wrapper.set_test() 

37 error = ValueError("handler failed") 

38 

39 wrapper.trigger(error=error) 

40 

41 with pytest.raises(ValueError, match="handler failed") as exc_info: 

42 await wrapper.wait_call() 

43 

44 assert exc_info.value is error 

45 

46 

47def test_composing_twice_decorates_once() -> None: 

48 async def handler() -> None: 

49 return None 

50 

51 def layer(call: Callable[..., Any]) -> Callable[..., Any]: 

52 @wraps(call) 

53 async def wrapped(*args: Any, **kwargs: Any) -> Any: 

54 return await call(*args, **kwargs) 

55 

56 wrapped.layers = getattr(call, "layers", 0) + 1 # type: ignore[attr-defined] 

57 return wrapped 

58 

59 wrapper = HandlerCallWrapper(handler, BrokerConfig()) 

60 for _ in range(2): 

61 wrapper.set_wrapped( 

62 dependencies=(), 

63 _call_decorators=(layer,), 

64 config=FastDependsConfig(), 

65 ) 

66 

67 # The old name still reads, and answers the composed call 

68 assert wrapper._original_call.layers == 1 # type: ignore[attr-defined]