-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Mutualized sniffio usage (#183)
1 parent
563c2e6
commit 8f6a6ef
Showing
8 changed files
with
105 additions
and
69 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/easynetwork/lowlevel/api_async/backend/_sniffio_helpers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2021-2023, Francis Clairicia-Rose-Claire-Josephine | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# | ||
"""Helper module for sniffio integration""" | ||
|
||
from __future__ import annotations | ||
|
||
__all__ = ["current_async_library", "setup_sniffio_contextvar"] | ||
|
||
import contextvars | ||
import sys | ||
|
||
|
||
def current_async_library() -> str: | ||
try: | ||
from sniffio import current_async_library | ||
except ModuleNotFoundError: | ||
current_async_library = _current_async_library_fallback | ||
return current_async_library() | ||
|
||
|
||
def _current_async_library_fallback() -> str: | ||
if "asyncio" in sys.modules: | ||
import asyncio | ||
|
||
try: | ||
asyncio.get_running_loop() | ||
except RuntimeError: | ||
pass | ||
else: | ||
return "asyncio" | ||
raise RuntimeError("unknown async library, or not in async context") | ||
|
||
|
||
def setup_sniffio_contextvar(context: contextvars.Context, library_name: str | None, /) -> None: | ||
try: | ||
import sniffio | ||
except ModuleNotFoundError: | ||
pass | ||
else: | ||
context.run(sniffio.current_async_library_cvar.set, library_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
tests/unit_test/test_async/test_lowlevel_api/test_backend/test_sniffio_helpers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
|
||
from easynetwork.lowlevel.api_async.backend._sniffio_helpers import _current_async_library_fallback | ||
|
||
import pytest | ||
|
||
|
||
def test____current_async_library_fallback____asyncio_not_imported( | ||
monkeypatch: pytest.MonkeyPatch, | ||
) -> None: | ||
# Arrange | ||
monkeypatch.delitem(sys.modules, "asyncio", raising=False) | ||
|
||
# Act | ||
with pytest.raises(RuntimeError, match=r"^unknown async library, or not in async context$"): | ||
_current_async_library_fallback() | ||
|
||
# Assert | ||
assert "asyncio" not in sys.modules | ||
|
||
|
||
def test____current_async_library_fallback____asyncio_not_running() -> None: | ||
# Arrange | ||
import asyncio | ||
|
||
del asyncio | ||
|
||
# Act & Assert | ||
with pytest.raises(RuntimeError, match=r"^unknown async library, or not in async context$"): | ||
_current_async_library_fallback() | ||
|
||
|
||
def test____current_async_library_fallback____asyncio_is_running() -> None: | ||
# Arrange | ||
import asyncio | ||
|
||
async def main() -> str: | ||
return _current_async_library_fallback() | ||
|
||
# Act | ||
library_name = asyncio.run(main()) | ||
|
||
# Assert | ||
assert library_name == "asyncio" |