Skip to content

Commit

Permalink
🚧(backends) fix tests failure
Browse files Browse the repository at this point in the history
WIP.
  • Loading branch information
quitterie-lcs committed Sep 20, 2024
1 parent 3740b05 commit 7f0ebc0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/ralph/backends/data/async_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pydantic import AnyUrl, PositiveInt
from pydantic_settings import SettingsConfigDict
from websockets.http11 import USER_AGENT
from websockets.asyncio.client import connect, ClientConnection

from ralph.backends.data.base import (
BaseAsyncDataBackend,
Expand Down Expand Up @@ -40,7 +41,7 @@ class WSClientOptions(ClientOptions):
Setting it to `None` disables keepalive pings.
ping_timeout (float): Timeout for keepalive pings in seconds.
Setting it to `None` disables timeouts.
read_limit (int): High-water mark of read buffer in bytes.
#read_limit (int): High-water mark of read buffer in bytes.
user_agent_header (str): Value of the `User-Agent` request header.
It defaults to "Python/x.y.z websockets/X.Y".
Setting it to `None` removes the header.
Expand All @@ -55,7 +56,7 @@ class WSClientOptions(ClientOptions):
origin: Optional[str] = None
ping_interval: Optional[float] = 20
ping_timeout: Optional[float] = 20
read_limit: int = 2**16
#read_limit: int = 2**16
user_agent_header: Optional[str] = USER_AGENT
write_limit: int = 2**16

Expand Down Expand Up @@ -93,11 +94,11 @@ def __init__(self, settings: Optional[WSDataBackendSettings] = None):
super().__init__(settings)
self._client = None

async def client(self) -> websockets.WebSocketClientProtocol:
async def client(self) -> ClientConnection:
"""Create a websocket client connected to `settings.URI` if it doesn't exist."""
if not self._client:
try:
self._client = await websockets.connect(
self._client = await connect(
str(self.settings.URI), **self.settings.CLIENT_OPTIONS.model_dump()
)
except (websockets.WebSocketException, OSError, TimeoutError) as error:
Expand Down
1 change: 1 addition & 0 deletions tests/backends/data/test_async_lrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,4 @@ async def simulate_network_latency(_):
# Server side processing time should be 3 times faster with unlimited
assert limited_concurrency_duration > 2.1 * concurrent_duration
assert limited_concurrency_duration <= 3.1 * concurrent_duration

22 changes: 12 additions & 10 deletions tests/backends/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,16 @@ def mock_entry_points(group):
),
]

monkeypatch.setattr("ralph.backends.loader.entry_points", mock_entry_points)
with monkeypatch.context() as mocked_context:
mocked_context.setattr("ralph.backends.loader.entry_points", mock_entry_points)
get_lrs_backends.cache_clear()
assert get_lrs_backends() == {
"test_backend": TestBackend,
"async_es": AsyncESLRSBackend,
"async_mongo": AsyncMongoLRSBackend,
"clickhouse": ClickHouseLRSBackend,
"es": ESLRSBackend,
"fs": FSLRSBackend,
"mongo": MongoLRSBackend,
}
get_lrs_backends.cache_clear()
assert get_lrs_backends() == {
"test_backend": TestBackend,
"async_es": AsyncESLRSBackend,
"async_mongo": AsyncMongoLRSBackend,
"clickhouse": ClickHouseLRSBackend,
"es": ESLRSBackend,
"fs": FSLRSBackend,
"mongo": MongoLRSBackend,
}
4 changes: 3 additions & 1 deletion tests/fixtures/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from pymongo import MongoClient
from pymongo.errors import CollectionInvalid

from websockets.asyncio.server import serve

from ralph.backends.data.async_es import AsyncESDataBackend
from ralph.backends.data.async_lrs import AsyncLRSDataBackend
from ralph.backends.data.async_mongo import AsyncMongoDataBackend
Expand Down Expand Up @@ -893,7 +895,7 @@ async def forward(websocket):
await websocket.send(json.dumps(event))
await asyncio.sleep(random.randrange(0, 500) / 10000.0)

async with websockets.serve(forward, "0.0.0.0", WS_TEST_PORT) as server:
async with serve(forward, "0.0.0.0", WS_TEST_PORT) as server:
yield server


Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from importlib import reload
from pathlib import Path
from typing import Optional, Union

import asyncio
import pytest
from click.exceptions import BadParameter
from click.testing import CliRunner
Expand Down

0 comments on commit 7f0ebc0

Please sign in to comment.