|
| 1 | +from datetime import datetime, timedelta |
| 2 | +from typing import AsyncGenerator |
| 3 | + |
| 4 | +import pytest_asyncio |
| 5 | + |
| 6 | +from rabbitmq_amqp_python_client import ( |
| 7 | + AsyncConnection, |
| 8 | + AsyncEnvironment, |
| 9 | + OAuth2Options, |
| 10 | + PosixSslConfigurationContext, |
| 11 | + RecoveryConfiguration, |
| 12 | + WinSslConfigurationContext, |
| 13 | +) |
| 14 | + |
| 15 | +from ..utils import token |
| 16 | + |
| 17 | + |
| 18 | +@pytest_asyncio.fixture |
| 19 | +async def async_environment(): |
| 20 | + """Fixture for async environment.""" |
| 21 | + environment = AsyncEnvironment(uri="amqp://guest:guest@localhost:5672/") |
| 22 | + yield environment |
| 23 | + await environment.close() |
| 24 | + |
| 25 | + |
| 26 | +@pytest_asyncio.fixture |
| 27 | +async def environment_auth() -> AsyncGenerator[AsyncEnvironment, None]: |
| 28 | + token_string = token(datetime.now() + timedelta(milliseconds=2500)) |
| 29 | + environment = AsyncEnvironment( |
| 30 | + uri="amqp://localhost:5672", |
| 31 | + oauth2_options=OAuth2Options(token=token_string), |
| 32 | + ) |
| 33 | + yield environment |
| 34 | + await environment.close() |
| 35 | + |
| 36 | + |
| 37 | +@pytest_asyncio.fixture |
| 38 | +async def async_connection() -> AsyncGenerator[AsyncConnection, None]: |
| 39 | + environment = AsyncEnvironment( |
| 40 | + uri="amqp://guest:guest@localhost:5672/", |
| 41 | + ) |
| 42 | + connection = await environment.connection() |
| 43 | + await connection.dial() |
| 44 | + yield connection |
| 45 | + await connection.close() |
| 46 | + |
| 47 | + |
| 48 | +@pytest_asyncio.fixture |
| 49 | +async def async_connection_with_reconnect() -> AsyncGenerator[AsyncConnection, None]: |
| 50 | + """Fixture providing an async connection with recovery enabled.""" |
| 51 | + environment = AsyncEnvironment( |
| 52 | + uri="amqp://guest:guest@localhost:5672/", |
| 53 | + recovery_configuration=RecoveryConfiguration(active_recovery=True), |
| 54 | + ) |
| 55 | + connection = await environment.connection() |
| 56 | + await connection.dial() |
| 57 | + yield connection |
| 58 | + await connection.close() |
| 59 | + |
| 60 | + |
| 61 | +@pytest_asyncio.fixture |
| 62 | +async def async_connection_ssl( |
| 63 | + ssl_context: PosixSslConfigurationContext | WinSslConfigurationContext, |
| 64 | +) -> AsyncGenerator[AsyncConnection, None]: |
| 65 | + environment = AsyncEnvironment( |
| 66 | + uri="amqps://guest:guest@localhost:5671/", |
| 67 | + ssl_context=ssl_context, |
| 68 | + ) |
| 69 | + connection = await environment.connection() |
| 70 | + await connection.dial() |
| 71 | + yield connection |
| 72 | + await connection.close() |
0 commit comments