Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Neagu committed Mar 21, 2024
1 parent 6a5ea59 commit 7235d5c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docker/activity_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from abc import abstractmethod


LISTEN_PORT: Final[int] = 19597

CHECK_INTERVAL_S: Final[float] = 5
THREAD_EXECUTOR_WORKERS: Final[int] = 10

Expand Down
1 change: 1 addition & 0 deletions requirements/test.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pytest
pytest-asyncio
pytest-mock
requests
requests-mock
tenacity
6 changes: 6 additions & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ pytest-asyncio==0.23.6
pytest-mock==3.12.0
# via -r requirements/test.in
requests==2.31.0
# via
# -r requirements/test.in
# requests-mock
requests-mock==1.11.0
# via -r requirements/test.in
six==1.16.0
# via requests-mock
tenacity==8.2.3
# via -r requirements/test.in
tomli==2.0.1
Expand Down
40 changes: 30 additions & 10 deletions tests/test_activity_monitor.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import asyncio
import pytest
import psutil
import requests
import tornado.web
import json
import tornado.httpserver
import tornado.ioloop
import threading
import pytest_asyncio
import requests
import requests_mock

from queue import Queue
from typing import Callable, TYPE_CHECKING
from typing import Callable, Iterable, TYPE_CHECKING
from pytest_mock import MockFixture
from tenacity import AsyncRetrying
from tenacity.stop import stop_after_delay
from tenacity.wait import wait_fixed
from conftest import _ActivityGenerator


if TYPE_CHECKING:
from ..docker import activity_monitor
else:
Expand Down Expand Up @@ -123,14 +126,30 @@ async def test_disk_usage_monitor_still_busy(
assert disk_usage_monitor.is_busy is True


@pytest_asyncio.fixture
async def server_url() -> str:
return "http://localhost:8899"
@pytest.fixture
def mock_jupyter_kernel_monitor(are_kernels_busy: bool) -> Iterable[None]:
with requests_mock.Mocker(real_http=True) as m:
m.get("http://localhost:8888/api/kernels", text=json.dumps([{"id": "atest1"}]))
m.get(
"http://localhost:8888/api/kernels/atest1",
text=json.dumps(
{"execution_state": "running" if are_kernels_busy else "idle"}
),
)
yield


@pytest.fixture
def mock_jupyter_kernel_monitor(mocker: MockFixture) -> None:
activity_monitor.JupyterKernelMonitor._are_kernels_busy = lambda _: False
@pytest.mark.parametrize("are_kernels_busy", [True, False])
def test_jupyter_kernel_monitor(
mock_jupyter_kernel_monitor: None, are_kernels_busy: bool
):
kernel_monitor = activity_monitor.JupyterKernelMonitor(1)
assert kernel_monitor._are_kernels_busy() is are_kernels_busy


@pytest_asyncio.fixture
async def server_url() -> str:
return f"http://localhost:{activity_monitor.LISTEN_PORT}"


@pytest_asyncio.fixture
Expand All @@ -141,7 +160,7 @@ async def tornado_server(mock_jupyter_kernel_monitor: None, server_url: str) ->

def _run_server_worker():
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(8899)
http_server.listen(activity_monitor.LISTEN_PORT)
current_io_loop = tornado.ioloop.IOLoop.current()

def _queue_stopper() -> None:
Expand Down Expand Up @@ -184,14 +203,15 @@ def mock_check_interval(mocker: MockFixture) -> None:
assert activity_monitor.CHECK_INTERVAL_S == 1


@pytest.mark.asyncio
@pytest.mark.parametrize("are_kernels_busy", [False])
async def test_tornado_server_ok(
mock_check_interval: None, tornado_server: None, server_url: str
):
result = requests.get(f"{server_url}/", timeout=5)
assert result.status_code == 200


@pytest.mark.parametrize("are_kernels_busy", [False])
async def test_activity_monitor_becomes_not_busy(
mock_check_interval: None,
socket_server: None,
Expand Down

0 comments on commit 7235d5c

Please sign in to comment.