Coverage for faststream/confluent/publisher/state.py: 81%

26 statements  

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

1from typing import TYPE_CHECKING, Protocol 

2 

3from faststream.exceptions import IncorrectState 

4 

5if TYPE_CHECKING: 

6 from faststream.confluent.helpers.client import AsyncConfluentProducer 

7 

8 

9class ProducerState(Protocol): 

10 __slots__ = () 

11 

12 @property 

13 def producer(self) -> "AsyncConfluentProducer": ... 

14 

15 def __bool__(self) -> bool: ... 

16 

17 async def ping(self, timeout: float) -> bool: ... 

18 

19 async def stop(self) -> None: ... 

20 

21 async def flush(self) -> None: ... 

22 

23 

24class EmptyProducerState: 

25 __slots__ = () 

26 

27 @property 

28 def producer(self) -> "AsyncConfluentProducer": 

29 msg = "You can't use producer here, please connect broker first." 

30 raise IncorrectState(msg) 

31 

32 async def ping(self, timeout: float) -> bool: # ruff: ignore[no-self-use] 

33 return False 

34 

35 def __bool__(self) -> bool: 

36 return False 

37 

38 async def stop(self) -> None: 

39 pass 

40 

41 async def flush(self) -> None: 

42 pass 

43 

44 

45class RealProducer: 

46 __slots__ = ("producer",) 

47 

48 def __init__(self, producer: "AsyncConfluentProducer") -> None: 

49 self.producer = producer 

50 

51 def __bool__(self) -> bool: 

52 return True 

53 

54 async def stop(self) -> None: 

55 await self.producer.stop() 

56 

57 async def ping(self, timeout: float) -> bool: 

58 return await self.producer.ping(timeout=timeout) 

59 

60 async def flush(self) -> None: 

61 await self.producer.flush()