Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: init ping method #1592

Merged
merged 17 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion faststream/broker/core/usecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
BrokerMiddleware,
ConnectionType,
CustomCallable,
MsgType,
MsgType
)
from faststream.exceptions import NOT_CONNECTED_YET
from faststream.log.logging import set_logger_fmt
Expand Down Expand Up @@ -342,3 +342,8 @@ async def publish(
publish = partial(m(None).publish_scope, publish)

return await publish(msg, **kwargs)

@abstractmethod
async def ping(self, timeout: int | None) -> bool:
"""Check connection alive."""
raise NotImplementedError()
10 changes: 10 additions & 0 deletions faststream/rabbit/broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,13 @@ async def declare_exchange(
"""Declares exchange object in **RabbitMQ**."""
assert self.declarer, NOT_CONNECTED_YET # nosec B101
return await self.declarer.declare_exchange(exchange)

async def ping(self, timeout: int | None) -> bool:
await super().ping(timeout)
try:
channel = await self._connection.channel()
await channel.declare_queue(name='test_queue', durable=True)
await channel.close()
return True
except Exception:
return False
18 changes: 18 additions & 0 deletions tests/brokers/base/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@ async def test_connect_by_url_priority(self, settings):
await broker.connect(**kwargs)
assert await self.ping(broker)
await broker.close()

@pytest.mark.asyncio()
async def test_connection_alive(self, settings):
kwargs = self.get_broker_args(settings)
broker = self.broker()
await broker.connect(**kwargs)
alive = await broker.ping(timeout=5)
assert alive
await broker.close()

@pytest.mark.asyncio()
async def test_connection_not_alive(self, settings):
kwargs = self.get_broker_args(settings)
broker = self.broker()
await broker.connect(**kwargs)
await broker.close()
alive = await broker.ping(timeout=5)
assert not alive
16 changes: 16 additions & 0 deletions tests/brokers/rabbit/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,19 @@ async def test_connect_merge_args_and_kwargs_native(self, settings):
broker = self.broker("fake-url") # will be ignored
assert await broker.connect(url=settings.url)
await broker.close()

@pytest.mark.asyncio()
async def test_connection_alive(self, settings):
broker = self.broker("fake-url") # will be ignored
await broker.connect(url=settings.url)
alive = await broker.ping(timeout=5)
assert alive
await broker.close()

@pytest.mark.asyncio()
async def test_connection_not_alive(self, settings):
broker = self.broker("fake-url") # will be ignored
await broker.connect(url=settings.url)
await broker.close()
alive = await broker.ping(timeout=5)
assert not alive