generated from br3ndonland/template-python
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Gunicorn conf tests to separate module
#11 These tests weren't really testing the start script, so they shouldn't be in test_start.py. Moving the tests to test_gunicorn_conf.py also helps emulate the directory structure of the source code, so tests are more intuitively found.
- Loading branch information
1 parent
35d6772
commit d28753b
Showing
2 changed files
with
86 additions
and
83 deletions.
There are no files selected for viewing
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,85 @@ | ||
import multiprocessing | ||
import os | ||
from typing import Optional | ||
|
||
import pytest | ||
|
||
from inboard import gunicorn_conf | ||
|
||
|
||
class TestCalculateWorkers: | ||
"""Test calculation of the number of Gunicorn worker processes. | ||
--- | ||
""" | ||
|
||
def test_gunicorn_conf_workers_default(self) -> None: | ||
"""Test default number of Gunicorn worker processes.""" | ||
assert gunicorn_conf.workers >= 2 | ||
assert gunicorn_conf.workers == multiprocessing.cpu_count() | ||
|
||
def test_gunicorn_conf_workers_custom_max( | ||
self, monkeypatch: pytest.MonkeyPatch | ||
) -> None: | ||
"""Test custom Gunicorn worker process calculation.""" | ||
monkeypatch.setenv("MAX_WORKERS", "1") | ||
monkeypatch.setenv("WEB_CONCURRENCY", "4") | ||
monkeypatch.setenv("WORKERS_PER_CORE", "0.5") | ||
assert os.getenv("MAX_WORKERS") == "1" | ||
assert os.getenv("WEB_CONCURRENCY") == "4" | ||
assert os.getenv("WORKERS_PER_CORE") == "0.5" | ||
assert ( | ||
gunicorn_conf.calculate_workers( | ||
str(os.getenv("MAX_WORKERS")), | ||
str(os.getenv("WEB_CONCURRENCY")), | ||
str(os.getenv("WORKERS_PER_CORE")), | ||
) | ||
== 1 | ||
) | ||
|
||
@pytest.mark.parametrize("number_of_workers", ["1", "2", "4"]) | ||
def test_gunicorn_conf_workers_custom_concurrency( | ||
self, monkeypatch: pytest.MonkeyPatch, number_of_workers: str | ||
) -> None: | ||
"""Test custom Gunicorn worker process calculation.""" | ||
monkeypatch.setenv("WEB_CONCURRENCY", number_of_workers) | ||
monkeypatch.setenv("WORKERS_PER_CORE", "0.5") | ||
assert os.getenv("WEB_CONCURRENCY") == number_of_workers | ||
assert os.getenv("WORKERS_PER_CORE") == "0.5" | ||
assert ( | ||
gunicorn_conf.calculate_workers( | ||
None, | ||
str(os.getenv("WEB_CONCURRENCY")), | ||
str(os.getenv("WORKERS_PER_CORE")), | ||
) | ||
== int(number_of_workers) | ||
) | ||
|
||
@pytest.mark.parametrize("concurrency", [None, "10"]) | ||
def test_gunicorn_conf_workers_custom_cores( | ||
self, monkeypatch: pytest.MonkeyPatch, concurrency: Optional[str] | ||
) -> None: | ||
"""Test custom Gunicorn worker process calculation. | ||
- Assert that number of workers equals `WORKERS_PER_CORE`, and is at least 2. | ||
- Assert that setting `WEB_CONCURRENCY` overrides `WORKERS_PER_CORE`. | ||
""" | ||
monkeypatch.setenv("WORKERS_PER_CORE", "0.5") | ||
workers_per_core = os.getenv("WORKERS_PER_CORE") | ||
assert workers_per_core == "0.5" | ||
cores: int = multiprocessing.cpu_count() | ||
assert gunicorn_conf.calculate_workers( | ||
None, None, workers_per_core, cores=cores | ||
) == max(int(cores * float(workers_per_core)), 2) | ||
assert ( | ||
gunicorn_conf.calculate_workers(None, "10", workers_per_core, cores=cores) | ||
== 10 | ||
) | ||
monkeypatch.setenv("WEB_CONCURRENCY", concurrency) if concurrency else None | ||
assert os.getenv("WEB_CONCURRENCY") == concurrency | ||
assert ( | ||
gunicorn_conf.calculate_workers( | ||
None, concurrency, workers_per_core, cores=cores | ||
) | ||
== 10 | ||
if concurrency | ||
else max(int(cores * float(workers_per_core)), 2) | ||
) |
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