From 03ec1b09069d3bbfa70d0af281739510a68b11e5 Mon Sep 17 00:00:00 2001 From: Nir Geller Date: Sun, 18 Aug 2024 19:58:22 +0300 Subject: [PATCH] Fix coverage --- pyproject.toml | 2 +- tests/test_config.py | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2242d424a..5400a68ae 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -97,7 +97,7 @@ filterwarnings = [ [tool.coverage.run] source_pkgs = ["uvicorn", "tests"] plugins = ["coverage_conditional_plugin"] -omit = ["uvicorn/workers.py", "uvicorn/__main__.py"] +omit = ["uvicorn/workers.py", "uvicorn/__main__.py", "uvicorn/_compat.py"] [tool.coverage.report] precision = 2 diff --git a/tests/test_config.py b/tests/test_config.py index e16cc5d56..48c2b1c10 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,5 +1,6 @@ from __future__ import annotations +import asyncio import configparser import io import json @@ -8,6 +9,7 @@ import socket import sys import typing +from contextlib import closing from pathlib import Path from typing import Any, Literal from unittest.mock import MagicMock @@ -25,7 +27,7 @@ Scope, StartResponse, ) -from uvicorn.config import Config +from uvicorn.config import Config, LoopFactoryType from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware from uvicorn.middleware.wsgi import WSGIMiddleware from uvicorn.protocols.http.h11_impl import H11Protocol @@ -545,3 +547,22 @@ def test_warn_when_using_reload_and_workers(caplog: pytest.LogCaptureFixture) -> Config(app=asgi_app, reload=True, workers=2) assert len(caplog.records) == 1 assert '"workers" flag is ignored when reloading is enabled.' in caplog.records[0].message + + +@pytest.mark.parametrize( + ("loop_type", "expected_loop_factory"), + [ + ("none", None), + ("asyncio", asyncio.ProactorEventLoop if sys.platform == "win32" else asyncio.SelectorEventLoop), # type: ignore + ], +) +def test_get_loop_factory(loop_type: LoopFactoryType, expected_loop_factory: Any): + config = Config(app=asgi_app, loop=loop_type) + loop_factory = config.get_loop_factory() + if loop_factory is None: + assert expected_loop_factory is loop_factory + else: + loop = loop_factory() + with closing(loop): + assert loop is not None + assert isinstance(loop, expected_loop_factory)