Skip to content

Commit

Permalink
Increase test coverage of Gunicorn conf
Browse files Browse the repository at this point in the history
84% -> 100%

- Use same syntax when calculating `max_workers` and `workers_per_core`
- Add test method to check worker configuration to test_start.py
  • Loading branch information
br3ndonland committed Sep 13, 2020
1 parent c6e04dc commit cd7604c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
11 changes: 5 additions & 6 deletions inboard/gunicorn_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

from inboard.start import configure_logging

workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1")
# Gunicorn setup
max_workers_str = os.getenv("MAX_WORKERS")
web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)
workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1")
use_max_workers = None
if max_workers_str:
if max_workers_str and int(max_workers_str) > 0:
use_max_workers = int(max_workers_str)
web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)

host = os.getenv("HOST", "0.0.0.0")
port = os.getenv("PORT", "80")
bind_env = os.getenv("BIND", None)
Expand All @@ -18,9 +18,8 @@
cores = multiprocessing.cpu_count()
workers_per_core = float(workers_per_core_str)
default_web_concurrency = workers_per_core * cores
if web_concurrency_str:
if web_concurrency_str and int(web_concurrency_str) > 0:
web_concurrency = int(web_concurrency_str)
assert web_concurrency > 0
else:
web_concurrency = max(int(default_web_concurrency), 2)
if use_max_workers:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,50 @@ def test_start_server_uvicorn_gunicorn(
)
mock_logger.debug.assert_called_once_with("Running Uvicorn with Gunicorn.") # type: ignore[attr-defined] # noqa: E501

@pytest.mark.parametrize(
"app_module",
[
"inboard.app.base.main:app",
"inboard.app.fastapibase.main:app",
"inboard.app.starlettebase.main:app",
],
)
def test_start_server_uvicorn_gunicorn_custom_config(
self,
app_module: str,
gunicorn_conf_path: Path,
logging_conf_dict: Dict[str, Any],
mock_logger: logging.Logger,
mocker: MockerFixture,
monkeypatch: MonkeyPatch,
) -> None:
"""Test `start.start_server` with Uvicorn managed by Gunicorn."""
assert os.getenv("GUNICORN_CONF", str(gunicorn_conf_path))
monkeypatch.setenv("LOG_FORMAT", "gunicorn")
monkeypatch.setenv("LOG_LEVEL", "debug")
monkeypatch.setenv("MAX_WORKERS", "1")
monkeypatch.setenv("PROCESS_MANAGER", "gunicorn")
monkeypatch.setenv("WEB_CONCURRENCY", "4")
assert os.getenv("LOG_FORMAT") == "gunicorn"
assert os.getenv("LOG_LEVEL") == "debug"
assert os.getenv("MAX_WORKERS") == "1"
assert os.getenv("PROCESS_MANAGER") == "gunicorn"
assert os.getenv("WEB_CONCURRENCY") == "4"
start.start_server(
str(os.getenv("PROCESS_MANAGER")),
app_module=app_module,
logger=mock_logger,
logging_conf_dict=logging_conf_dict,
)
monkeypatch.delenv("WEB_CONCURRENCY")
start.start_server(
str(os.getenv("PROCESS_MANAGER")),
app_module=app_module,
logger=mock_logger,
logging_conf_dict=logging_conf_dict,
)
mock_logger.debug.assert_called_with("Running Uvicorn with Gunicorn.") # type: ignore[attr-defined] # noqa: E501

def test_start_server_uvicorn_incorrect_module(
self,
logging_conf_dict: Dict[str, Any],
Expand Down

0 comments on commit cd7604c

Please sign in to comment.