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
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1from typing import TYPE_CHECKING, Protocol
3from faststream.exceptions import IncorrectState
5if TYPE_CHECKING:
6 from faststream.confluent.helpers.client import AsyncConfluentProducer
9class ProducerState(Protocol):
10 __slots__ = ()
12 @property
13 def producer(self) -> "AsyncConfluentProducer": ...
15 def __bool__(self) -> bool: ...
17 async def ping(self, timeout: float) -> bool: ...
19 async def stop(self) -> None: ...
21 async def flush(self) -> None: ...
24class EmptyProducerState:
25 __slots__ = ()
27 @property
28 def producer(self) -> "AsyncConfluentProducer":
29 msg = "You can't use producer here, please connect broker first."
30 raise IncorrectState(msg)
32 async def ping(self, timeout: float) -> bool: # ruff: ignore[no-self-use]
33 return False
35 def __bool__(self) -> bool:
36 return False
38 async def stop(self) -> None:
39 pass
41 async def flush(self) -> None:
42 pass
45class RealProducer:
46 __slots__ = ("producer",)
48 def __init__(self, producer: "AsyncConfluentProducer") -> None:
49 self.producer = producer
51 def __bool__(self) -> bool:
52 return True
54 async def stop(self) -> None:
55 await self.producer.stop()
57 async def ping(self, timeout: float) -> bool:
58 return await self.producer.ping(timeout=timeout)
60 async def flush(self) -> None:
61 await self.producer.flush()