Skip to content

Commit

Permalink
change tests to use env vars...
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuyukai committed Jan 25, 2024
1 parent 820d86e commit 4e4219d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 38 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: "Run Pyright"
run: "poetry run pyright --verifytypes serena --ignoreexternal"

test-postgresql:
test-rabbitmq:
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -78,6 +78,8 @@ jobs:

- name: "Run Pytest"
run: "poetry run pytest -rPx --cov"
env:
AMQP_PORT: ${{ job.services.amqp.ports[5672] }}

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand Down
6 changes: 4 additions & 2 deletions src/serena/utils/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,12 @@ def write_longlong_signed(self, value: int) -> None:
self._write(struct.pack(">q", value))

@overload
def write_timestamp(self, value: datetime) -> None: ...
def write_timestamp(self, value: datetime) -> None:
...

@overload
def write_timestamp(self, value: int) -> None: ...
def write_timestamp(self, value: int) -> None:
...

def write_timestamp(self, value: datetime | int) -> None:
"""
Expand Down
23 changes: 23 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from typing import Any

import serena
from serena.connection import AMQPConnection

# Global variables to store AMQP connection details
AMQP_HOST = os.environ.get("AMQP_HOST", "127.0.0.1")
AMQP_PORT = int(os.environ.get("AMQP_PORT", 5672))
AMQP_USERNAME = os.environ.get("AMQP_USERNAME", "guest")
AMQP_PASSWORD = os.environ.get("AMQP_PASSWORD", "guestt")


# pyright complains about this being unused, but it isn't.
@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
) as conn:
yield conn
9 changes: 5 additions & 4 deletions tests/channel/test_channel.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import pytest
from serena.connection import open_connection
from serena.enums import ReplyCode
from serena.exc import UnexpectedCloseError

from tests import _open_connection

pytestmark = pytest.mark.anyio


Expand All @@ -11,15 +12,15 @@ async def test_channel_opening():
Tests opening a channel.
"""

async with open_connection("127.0.0.1") as conn:
async with _open_connection() as conn:
async with conn.open_channel() as channel:
assert channel.open

assert not channel.open


async def test_channel_server_side_close():
async with open_connection("127.0.0.1") as conn:
async with _open_connection() as conn:
with pytest.raises(UnexpectedCloseError) as e:
async with conn.open_channel() as channel:
await channel.basic_get("non-existing-queue")
Expand All @@ -32,7 +33,7 @@ async def test_reusing_channel_id():
Tests reusing channel IDs.
"""

async with open_connection("127.0.0.1") as conn:
async with _open_connection() as conn:
async with conn.open_channel() as channel:
assert channel.id == 1

Expand Down
11 changes: 6 additions & 5 deletions tests/channel/test_exchange.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import uuid

import pytest
from serena.connection import open_connection
from serena.enums import ExchangeType, ReplyCode
from serena.exc import UnexpectedCloseError

from tests import _open_connection

pytestmark = pytest.mark.anyio

test_suffix = uuid.uuid4()
Expand All @@ -15,7 +16,7 @@ async def test_ex_basic_declaration():
Tests basic exchange declaration of all types.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
# no error means that declaration succeeded
for type in ExchangeType:
await channel.exchange_declare(name=f"{type}-{test_suffix}", type=type, durable=False)
Expand All @@ -26,7 +27,7 @@ async def test_ex_declaration_invalid_type():
Tests declaration with an invalid type.
"""

async with open_connection("127.0.0.1") as conn:
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")
Expand All @@ -39,7 +40,7 @@ async def test_ex_delete():
Tests deleting an exchange.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
name = f"delete-{test_suffix}"

await channel.exchange_declare(name=name, type=ExchangeType.DIRECT)
Expand All @@ -62,7 +63,7 @@ async def test_ex_binding():
exchange_name = f"ex-bind-1-{test_suffix}"
ex_2 = f"ex-bind-2-{test_suffix}"

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
await channel.exchange_declare(exchange_name, ExchangeType.DIRECT)
await channel.exchange_declare(ex_2, ExchangeType.DIRECT)
await channel.exchange_bind(exchange_name, ex_2, routing_key="")
Expand Down
16 changes: 9 additions & 7 deletions tests/channel/test_publish.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import pytest
from serena.connection import open_connection
from serena.enums import ReplyCode
from serena.exc import MessageReturnedError
from serena.message import AMQPMessage
from serena.payloads.header import BasicHeader

from tests import _open_connection

pytestmark = pytest.mark.anyio


Expand All @@ -12,7 +14,7 @@ async def test_basic_publish():
Tests publishing a message to a queue, and getting the message back.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
queue = await channel.queue_declare(name="", exclusive=True)

result = await channel.basic_get(queue.name)
Expand All @@ -30,15 +32,15 @@ async def test_consumption():
Tests consuming asynchronously.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
queue = await channel.queue_declare(name="", exclusive=True)

counter = 0
for _i in range(0, 10):
await channel.basic_publish("", routing_key=queue.name, body=b"test")
counter += 1

messages = []
messages: list[AMQPMessage] = []
queue = await channel.queue_declare(name=queue.name, passive=True)
assert queue.message_count == 10

Expand All @@ -59,7 +61,7 @@ async def test_acks():
Tests message acknowledgement.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
queue = await channel.queue_declare(name="", exclusive=True)
await channel.basic_publish("", routing_key=queue.name, body=b"test")

Expand All @@ -83,7 +85,7 @@ async def test_publishing_headers():
Tests publishing header data.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
queue = await channel.queue_declare("", exclusive=True)
headers = BasicHeader(message_id="123456")

Expand All @@ -99,7 +101,7 @@ async def test_return():
Tests message returning.
"""

async with open_connection("127.0.0.1") as conn:
async with _open_connection() as conn:
async with conn.open_channel() as channel:
with pytest.raises(MessageReturnedError) as e:
await channel.basic_publish("", routing_key="non-existent-queue", body=b"")
Expand Down
24 changes: 13 additions & 11 deletions tests/channel/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import anyio
import pytest
from anyio.abc import TaskStatus
from serena.connection import open_connection
from serena.channel import Channel
from serena.enums import ExchangeType, ReplyCode
from serena.exc import UnexpectedCloseError

from tests import _open_connection

pytestmark = pytest.mark.anyio

test_suffix = uuid.uuid4()
Expand All @@ -17,7 +19,7 @@ async def test_queue_declaration():
Tests basic queue declaration.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
declared = await channel.queue_declare(name=f"queue-{test_suffix}")
assert declared.name == f"queue-{test_suffix}"
assert declared.message_count == 0
Expand All @@ -28,10 +30,10 @@ async def test_queue_exclusive_declaration():
Tests exclusive declaration.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
declared = await channel.queue_declare(name=f"queue-ex-{test_suffix}", exclusive=True)

async with open_connection("127.0.0.1") as conn:
async with _open_connection() as conn:
with pytest.raises(UnexpectedCloseError) as e:
async with conn.open_channel() as channel:
await channel.queue_declare(declared.name, passive=True)
Expand All @@ -45,7 +47,7 @@ async def test_queue_delete():
Tests deleting queues.
"""

async with open_connection("127.0.0.1") as conn:
async with _open_connection() as conn:
with pytest.raises(UnexpectedCloseError) as e:
async with conn.open_channel() as channel:
declared = await channel.queue_declare("", exclusive=True)
Expand All @@ -67,7 +69,7 @@ async def test_delete_not_empty():
Tests deleting a non-empty queue.
"""

async with open_connection("127.0.0.1") as conn:
async with _open_connection() as conn:
with pytest.raises(UnexpectedCloseError) as e:
async with conn.open_channel() as channel:
declared = await channel.queue_declare("", exclusive=True)
Expand All @@ -82,13 +84,13 @@ async def test_delete_in_use():
Tests deleting a queue in use.
"""

async def consumer(channel, queue_name, *, task_status: TaskStatus):
async def consumer(channel: Channel, queue_name: str, *, task_status: TaskStatus[None]):
async with channel.basic_consume(queue_name):
task_status.started()
await anyio.sleep_forever()

async with open_connection("127.0.0.1") as conn:
with pytest.raises(ExceptionGroup) as e:
async with _open_connection() as conn:
with pytest.raises(ExceptionGroup[UnexpectedCloseError]) as e:
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)
Expand All @@ -103,7 +105,7 @@ async def test_queue_purge():
Tests purging a queue.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
queue = await channel.queue_declare(exclusive=True)
assert (await channel.queue_purge(queue.name)) == 0

Expand All @@ -116,7 +118,7 @@ async def test_queue_bind():
Tests binding a queue.
"""

async with open_connection("127.0.0.1") as conn, conn.open_channel() as channel:
async with _open_connection() as conn, conn.open_channel() as channel:
exchange_name = await channel.exchange_declare(
f"test-ex-{test_suffix}", ExchangeType.DIRECT, auto_delete=True
)
Expand Down
10 changes: 6 additions & 4 deletions tests/test_channel_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import anyio
import pytest
from serena import UnexpectedCloseError, open_connection
from serena import UnexpectedCloseError

from tests import _open_connection

pytestmark = pytest.mark.anyio

Expand All @@ -14,7 +16,7 @@ async def test_channel_pool():
Tests opening and using the channel pool.
"""

async with open_connection("127.0.0.1") as conn:
async with _open_connection() as conn:
async with conn.open_channel_pool(initial_channels=2) as pool:
assert pool.idle_channels == 2

Expand All @@ -30,7 +32,7 @@ async def test_channel_pool_consume():
"""

async with (
open_connection("127.0.0.1") as conn,
_open_connection() as conn,
conn.open_channel_pool(initial_channels=2) as pool,
):
queue = await pool.queue_declare(name=f"test-cc-{test_suffix}", auto_delete=True)
Expand All @@ -51,7 +53,7 @@ async def test_channel_pool_errors():
"""

async with (
open_connection("127.0.0.1") as conn,
_open_connection() as conn,
conn.open_channel_pool(initial_channels=2) as pool,
):
with pytest.raises(UnexpectedCloseError):
Expand Down
10 changes: 6 additions & 4 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from serena.enums import ReplyCode
from serena.exc import UnexpectedCloseError

from tests import _open_connection

pytestmark = pytest.mark.anyio


Expand All @@ -12,7 +14,7 @@ async def test_connection_opening():
Tests the basics of opening and closing a connection.
"""

async with open_connection("127.0.0.1") as conn:
async with _open_connection() as conn:
assert conn.open

assert not conn.open
Expand All @@ -24,7 +26,7 @@ async def test_heartbeat_intervals():
Tests heartbeat intervals.
"""

async with open_connection("127.0.0.1", heartbeat_interval=2) as conn:
async with _open_connection(heartbeat_interval=2) as conn:
await sleep(4)

assert conn.heartbeat_statistics().heartbeat_count > 2
Expand All @@ -36,7 +38,7 @@ async def test_bad_virtual_host():
"""

with pytest.raises(UnexpectedCloseError) as e:
async with open_connection("127.0.0.1", virtual_host="/does_not_exist"):
async with _open_connection(virtual_host="/does_not_exist"):
pass

assert e.value.reply_code == ReplyCode.not_allowed
Expand All @@ -48,7 +50,7 @@ async def test_bad_authentication():
"""

with pytest.raises(UnexpectedCloseError) as e:
async with open_connection("127.0.0.1", password="not the right password!"):
async with _open_connection(password="not the right password!"):
pass

assert e.value.reply_code == ReplyCode.access_refused
Expand Down

0 comments on commit 4e4219d

Please sign in to comment.