Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: Clarify AsyncBackend.create_condition_var() behavior with lock argument. #377

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/easynetwork/lowlevel/api_async/backend/_asyncio/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,13 @@ def create_event(self) -> IEvent:
return self.__asyncio.Event()

def create_condition_var(self, lock: ILock | None = None) -> ICondition:
if lock is not None:
assert isinstance(lock, self.__asyncio.Lock) # nosec assert_used

return self.__asyncio.Condition(lock)
match lock:
case None:
return self.__asyncio.Condition()
case self.__asyncio.Lock():
return self.__asyncio.Condition(lock)
case _:
raise TypeError("lock must be a asyncio.Lock")

async def run_in_thread(
self,
Expand Down
11 changes: 7 additions & 4 deletions src/easynetwork/lowlevel/api_async/backend/_trio/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,13 @@ def create_event(self) -> IEvent:
return self.__trio.Event()

def create_condition_var(self, lock: ILock | None = None) -> ICondition:
if lock is not None:
assert isinstance(lock, self.__trio.Lock) # nosec assert_used

return self.__trio.Condition(lock)
match lock:
case None:
return self.__trio.Condition()
case self.__trio.Lock():
return self.__trio.Condition(lock)
case _:
raise TypeError("lock must be a trio.Lock")

async def run_in_thread(
self,
Expand Down
17 changes: 16 additions & 1 deletion src/easynetwork/lowlevel/api_async/backend/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,8 +1175,23 @@ def create_condition_var(self, lock: ILock | None = ...) -> ICondition:
"""
Creates a Condition variable object for inter-task synchronization.

If `lock` is given and not :data:`None`, it should be a lock created by :meth:`create_lock`.
While it is guaranteed to work with a lock from :meth:`create_lock`, it can be any other implementation
(such as the lock returned by :meth:`create_fair_lock`), but it can also refuse other implementations.

Generic code should expect the function to fail::

try:
cond = backend.create_condition_var(backend.create_fair_lock())
except TypeError:
# Cannot use a fair lock. Use the default implementation instead.
cond = backend.create_condition_var()

Parameters:
lock: If given, it must be a lock created by :meth:`create_lock`. Otherwise a new Lock object is created automatically.
lock: The lock instance to use under the hood.

Raises:
TypeError: `lock` type is not supported.

Returns:
A new Condition.
Expand Down
22 changes: 21 additions & 1 deletion tests/unit_test/test_async/test_asyncio_backend/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from easynetwork.lowlevel.api_async.backend._asyncio.dns_resolver import AsyncIODNSResolver
from easynetwork.lowlevel.api_async.backend._asyncio.stream.listener import AbstractAcceptedSocketFactory, AcceptedSocketFactory
from easynetwork.lowlevel.api_async.backend._asyncio.stream.socket import StreamReaderBufferedProtocol
from easynetwork.lowlevel.api_async.backend.abc import ILock

import pytest

Expand Down Expand Up @@ -805,9 +806,28 @@ async def test____create_condition_var____use_asyncio_Condition_class(
condition = backend.create_condition_var(mock_lock)

# Assert
mock_Condition.assert_called_once_with(mock_lock)
if mock_lock is None:
mock_Condition.assert_called_once_with()
else:
mock_Condition.assert_called_once_with(mock_lock)
assert condition is mocker.sentinel.condition_var

async def test____create_condition_var____invalid_Lock_type(
self,
backend: AsyncIOBackend,
mocker: MockerFixture,
) -> None:
# Arrange
mock_lock: MagicMock = mocker.NonCallableMagicMock(spec=ILock)
mock_Condition = mocker.patch("asyncio.Condition", return_value=mocker.sentinel.condition_var)

# Act
with pytest.raises(TypeError, match=r"^lock must be a asyncio\.Lock$"):
_ = backend.create_condition_var(mock_lock)

# Assert
mock_Condition.assert_not_called()

@pytest.mark.parametrize("abandon_on_cancel", [False, True], ids=lambda p: f"abandon_on_cancel=={p}")
async def test____run_in_thread____use_loop_run_in_executor(
self,
Expand Down
22 changes: 21 additions & 1 deletion tests/unit_test/test_async/test_trio_backend/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING, Any, Final

from easynetwork.lowlevel.api_async.backend._trio.backend import TrioBackend
from easynetwork.lowlevel.api_async.backend.abc import ILock

import pytest

Expand Down Expand Up @@ -675,9 +676,28 @@ async def test____create_condition_var____use_trio_Condition_class(
condition = backend.create_condition_var(mock_lock)

# Assert
mock_Condition.assert_called_once_with(mock_lock)
if mock_lock is None:
mock_Condition.assert_called_once_with()
else:
mock_Condition.assert_called_once_with(mock_lock)
assert condition is mocker.sentinel.condition_var

async def test____create_condition_var____invalid_Lock_type(
self,
backend: TrioBackend,
mocker: MockerFixture,
) -> None:
# Arrange
mock_lock: MagicMock = mocker.NonCallableMagicMock(spec=ILock)
mock_Condition = mocker.patch("trio.Condition", return_value=mocker.sentinel.condition_var)

# Act
with pytest.raises(TypeError, match=r"^lock must be a trio\.Lock$"):
_ = backend.create_condition_var(mock_lock)

# Assert
mock_Condition.assert_not_called()

@pytest.mark.parametrize("abandon_on_cancel", [False, True], ids=lambda p: f"abandon_on_cancel=={p}")
async def test____run_in_thread____use_loop_run_in_executor(
self,
Expand Down