diff --git a/src/serena/connection.py b/src/serena/connection.py index 8ca3544..1e44cbb 100644 --- a/src/serena/connection.py +++ b/src/serena/connection.py @@ -86,7 +86,7 @@ class HeartbeatStatistics: prev_heartbeat_mn: int = attr.ib(default=-1) #: The current heartbeat time, in monotonic nanoseconds. - cur_heartbeat_mn: int = attr.ib(default=None) + cur_heartbeat_mn: int = attr.ib(default=-1) #: The previous heartbeat time, in wall clock time. prev_heartbeat_time: datetime = attr.ib(default=None) @@ -100,7 +100,7 @@ def interval(self) -> int | None: Returns the interval between two heartbeats in nanoseconds. """ - if self.prev_heartbeat_mn == -1: + if self.prev_heartbeat_mn == -1 or self.cur_heartbeat_mn == -1: return None return self.cur_heartbeat_mn - self.prev_heartbeat_mn diff --git a/tests/__init__.py b/tests/__init__.py index dd69cec..36547ce 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -17,7 +17,10 @@ @asynccontextmanager async def _open_connection(**kwargs: Any) -> AsyncGenerator[AMQPConnection, None]: # type: ignore async with serena.open_connection( - AMQP_HOST, port=AMQP_PORT, username=AMQP_USERNAME, password=AMQP_PASSWORD, - **kwargs # type: ignore + AMQP_HOST, + port=AMQP_PORT, + username=AMQP_USERNAME, + password=AMQP_PASSWORD, + **kwargs, # type: ignore ) as conn: yield conn diff --git a/tests/channel/test_exchange.py b/tests/channel/test_exchange.py index bf18c32..66eb689 100644 --- a/tests/channel/test_exchange.py +++ b/tests/channel/test_exchange.py @@ -27,12 +27,12 @@ async def test_ex_declaration_invalid_type(): Tests declaration with an invalid type. """ - async with _open_connection() as conn: - with pytest.raises(UnexpectedCloseError) as e: - async with conn.open_channel() as channel: - await channel.exchange_declare(name="invalid", type="invalid") + with pytest.raises(UnexpectedCloseError) as e: + async with _open_connection() as conn: + async with conn.open_channel() as channel: + await channel.exchange_declare(name="invalid", type="invalid") - assert e.value.reply_code in (ReplyCode.command_invalid, ReplyCode.precondition_failed) + assert e.value.reply_code in (ReplyCode.command_invalid, ReplyCode.precondition_failed) async def test_ex_delete(): diff --git a/tests/channel/test_queue.py b/tests/channel/test_queue.py index def82e4..a254205 100644 --- a/tests/channel/test_queue.py +++ b/tests/channel/test_queue.py @@ -90,13 +90,13 @@ async def consumer(channel: Channel, queue_name: str, *, task_status: TaskStatus await anyio.sleep_forever() async with _open_connection() as conn: - with pytest.raises(ExceptionGroup[UnexpectedCloseError]) as e: + with pytest.raises(ExceptionGroup) as e: # type: ignore async with conn.open_channel() as channel, anyio.create_task_group() as group: queue = await channel.queue_declare(exclusive=True) await group.start(consumer, channel, queue.name) await channel.queue_delete(queue.name, if_unused=True) - assert isinstance(unwrapped := e.value.exceptions[0], UnexpectedCloseError) + assert isinstance(unwrapped := e.value.exceptions[0], UnexpectedCloseError) # type: ignore assert unwrapped.reply_code == ReplyCode.precondition_failed diff --git a/tests/test_connection.py b/tests/test_connection.py index 8914376..2a8ce0c 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -4,7 +4,7 @@ from serena.enums import ReplyCode from serena.exc import UnexpectedCloseError -from tests import _open_connection +from tests import _open_connection, AMQP_HOST, AMQP_PORT, AMQP_USERNAME pytestmark = pytest.mark.anyio @@ -50,7 +50,9 @@ async def test_bad_authentication(): """ with pytest.raises(UnexpectedCloseError) as e: - async with _open_connection(password="not the right password!"): + async with open_connection( + AMQP_HOST, port=AMQP_PORT, username=AMQP_USERNAME, password="not the right password!" + ): pass assert e.value.reply_code == ReplyCode.access_refused