Skip to content

Commit

Permalink
fix handful of failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuyukai committed Jan 25, 2024
1 parent daad633 commit f1077fd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/serena/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions tests/channel/test_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
4 changes: 2 additions & 2 deletions tests/channel/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
6 changes: 4 additions & 2 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f1077fd

Please sign in to comment.