Skip to content

Commit

Permalink
Verify process manager for base ASGI app
Browse files Browse the repository at this point in the history
92e6b68
d2220ba
735618a
62d2e32
ed03219

- Verify that `PROCESS_MANAGER` environment variable is set to either
  "gunicorn" or "uvicorn"
- Add unit test for exception when using inappropriate `PROCESS_MANAGER`
  • Loading branch information
br3ndonland committed Sep 13, 2020
1 parent ed03219 commit 5e6af54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
9 changes: 4 additions & 5 deletions inboard/app/base/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ async def __call__(
}
)
version = f"{sys.version_info.major}.{sys.version_info.minor}"
server = (
"Uvicorn"
if os.getenv("PROCESS_MANAGER") == "uvicorn"
else "Uvicorn, Gunicorn,"
)
process_manager = os.getenv("PROCESS_MANAGER")
if process_manager not in ["gunicorn", "uvicorn"]:
raise NameError("Process manager needs to be either uvicorn or gunicorn.")
server = "Uvicorn" if process_manager == "uvicorn" else "Uvicorn, Gunicorn,"
message = f"Hello World, from {server} and Python {version}!"
response: Dict = {"type": "http.response.body", "body": message.encode("utf-8")}
await send(response)
Expand Down
12 changes: 12 additions & 0 deletions tests/app/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ def test_get_asgi_uvicorn_gunicorn(
assert response.status_code == 200
assert response.text == "Hello World, from Uvicorn, Gunicorn, and Python 3.8!"

def test_get_asgi_incorrect_process_manager(
self, client_asgi: TestClient, monkeypatch: MonkeyPatch
) -> None:
"""Test `GET` request to base ASGI app with incorrect `PROCESS_MANAGER`."""
monkeypatch.setenv("PROCESS_MANAGER", "incorrect")
monkeypatch.setenv("WITH_RELOAD", "false")
assert os.getenv("PROCESS_MANAGER") == "incorrect"
assert os.getenv("WITH_RELOAD") == "false"
with pytest.raises(NameError) as e:
client_asgi.get("/")
assert str(e) == "Process manager needs to be either uvicorn or gunicorn."

def test_get_root(self, clients: List[TestClient]) -> None:
"""Test a `GET` request to the root endpoint."""
for client in clients:
Expand Down

0 comments on commit 5e6af54

Please sign in to comment.