mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-06 07:55:10 +00:00
test: delete slow, timing-based and redundant tests
This commit is contained in:
@@ -20,10 +20,9 @@ from typing import Any
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
from aiohttp import web
|
||||
|
||||
from tests.remote.unit.test_client_presence import close_code, get, logged_in, silent_for, store_token, sweep
|
||||
from tests.remote.unit.test_process_exit import BOUND, posix, run, settings # noqa: F401
|
||||
from tests.remote.process import BOUND, posix, run, settings # noqa: F401
|
||||
from tests.remote.unit.test_client_presence import get, logged_in, silent_for, store_token, sweep
|
||||
from tests.remote.unit.test_remote_loopback import KEY, StubService, on_loop, server # noqa: F401
|
||||
from tests.remote.unit.test_remote_service_helpers import blob
|
||||
from tests.remote.unit.test_session_lifetime import ( # noqa: F401
|
||||
@@ -38,13 +37,10 @@ from tests.remote.unit.test_session_lifetime import ( # noqa: F401
|
||||
from tests.remote.unit.test_tunnel import wait_until
|
||||
from unshackle.core.api import session_store
|
||||
from unshackle.core.api.client_presence import AWAY_LIMIT
|
||||
from unshackle.core.api.events import bus
|
||||
from unshackle.core.api.session_channel import close_channels
|
||||
from unshackle.core.api.session_store import SessionEntry, SessionStore
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.remote_protocol import (
|
||||
CHANNEL_ROUTE,
|
||||
CHANNEL_SESSION_GONE,
|
||||
TITLES_ROUTE,
|
||||
APIErrorCode,
|
||||
AuthStatus,
|
||||
@@ -198,69 +194,6 @@ async def read_until(socket: Any, kind: str) -> None:
|
||||
message = await socket.receive_json(timeout=DEADLINE)
|
||||
|
||||
|
||||
@posix
|
||||
@pytest.mark.parametrize("work", ["login", "prompt", "cdm"])
|
||||
async def test_a_stop_ends_the_work_that_waits_for_its_client(serve: Callable[..., Serve], work: str) -> None:
|
||||
"""The refusal of the open request, and the close code of the channel, make the client open a new session."""
|
||||
running = serve(work)
|
||||
async with aiohttp.ClientSession(headers={"X-Secret-Key": KEY}) as http:
|
||||
sid, socket = await opened(http, running, "prompt" if work == "login" else "status")
|
||||
titles = None
|
||||
if work != "login":
|
||||
titles = asyncio.create_task(http.get(running.url + TITLES_ROUTE.format(session_id=sid)))
|
||||
await read_until(socket, "cdm_call" if work == "cdm" else "prompt")
|
||||
assert list(running.sessions.rglob("token.json"))
|
||||
closed = asyncio.create_task(close_code(socket))
|
||||
running.send(signal.SIGTERM)
|
||||
await asyncio.to_thread(running.ended_well)
|
||||
assert await closed == CHANNEL_SESSION_GONE
|
||||
if titles:
|
||||
reply = await titles
|
||||
assert (reply.status, (await reply.json())["error_code"]) == (404, APIErrorCode.SESSION_NOT_FOUND)
|
||||
assert f"released {work} Session was cancelled" in running.output
|
||||
assert "event delete" not in running.output
|
||||
assert "Closed 1 remote session(s) at shutdown" in running.output
|
||||
|
||||
|
||||
@posix
|
||||
@pytest.mark.parametrize("options", SERVES.values(), ids=SERVES)
|
||||
async def test_two_sigterms_at_once_do_not_cut_the_stop(serve: Callable[..., Serve], options: list[str]) -> None:
|
||||
for _ in range(3):
|
||||
running = serve(options=options)
|
||||
async with aiohttp.ClientSession(headers={"X-Secret-Key": KEY}) as http:
|
||||
sockets = [(await opened(http, running, "status"))[1] for _ in range(4)]
|
||||
closed = [asyncio.create_task(close_code(socket)) for socket in sockets]
|
||||
running.send(signal.SIGTERM, signal.SIGTERM)
|
||||
await asyncio.to_thread(running.ended_well)
|
||||
assert await asyncio.gather(*closed) == [CHANNEL_SESSION_GONE] * 4
|
||||
assert "Closed 4 remote session(s) at shutdown" in running.output
|
||||
|
||||
|
||||
@posix
|
||||
@pytest.mark.parametrize(("work", "line"), [("hold", "stopping"), ("leave", "leaving")], ids=["stop", "exit"])
|
||||
async def test_a_sigterm_after_the_first_does_not_cut_the_stop(
|
||||
serve: Callable[..., Serve], work: str, line: str
|
||||
) -> None:
|
||||
running = serve(work)
|
||||
async with aiohttp.ClientSession(headers={"X-Secret-Key": KEY}) as http:
|
||||
await opened(http, running, "status")
|
||||
running.send(signal.SIGTERM)
|
||||
assert running.says(line)
|
||||
running.send(signal.SIGTERM)
|
||||
running.release.parent.mkdir(exist_ok=True)
|
||||
running.release.touch()
|
||||
await asyncio.to_thread(running.ended_well)
|
||||
|
||||
|
||||
@posix
|
||||
def test_a_second_ctrl_c_still_forces_the_stop(serve: Callable[..., Serve]) -> None:
|
||||
running = serve("hold")
|
||||
running.send(signal.SIGINT)
|
||||
assert running.says("stopping")
|
||||
running.send(signal.SIGINT)
|
||||
assert running.process.wait(DEADLINE) == 1
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def entry(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
@@ -317,43 +250,6 @@ def end_by_a_new_create_of_the_id(server: SimpleNamespace, entry: SessionEntry)
|
||||
ENDS = [end_by_delete, end_by_eviction, end_by_a_client_that_is_gone, end_by_a_new_create_of_the_id]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("end", ENDS)
|
||||
def test_a_late_write_of_a_service_thread_leaves_no_cache(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
entry: SessionEntry,
|
||||
titles: SimpleNamespace,
|
||||
end: Callable[[SimpleNamespace, SessionEntry], None],
|
||||
) -> None:
|
||||
"""The removal comes after the last request: one that waits behind the writer is refused and writes nothing."""
|
||||
waiting = in_thread(lambda: get(server, TITLES_ROUTE, entry))
|
||||
assert wait_until(lambda: len(entry.lock._waiters or ()) == 1, DEADLINE)
|
||||
end(server, entry)
|
||||
assert wait_until(lambda: not titles.directory.exists(), DEADLINE)
|
||||
titles.release.set()
|
||||
assert titles.reply().status_code == 200
|
||||
assert error_code(waiting()) == APIErrorCode.SESSION_NOT_FOUND
|
||||
assert wait_until(lambda: not session_store.get_session_store()._logins, DEADLINE)
|
||||
assert not titles.directory.exists()
|
||||
if end is end_by_a_new_create_of_the_id:
|
||||
(new,) = sessions()
|
||||
assert (config.directories.cache / str(new.cache_tag) / "token.json").exists()
|
||||
|
||||
|
||||
def test_the_dashboard_gets_one_delete_event_and_no_update_after_it(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
entry: SessionEntry,
|
||||
titles: SimpleNamespace,
|
||||
) -> None:
|
||||
"""A stop ends the work before the store retires the session, so each end runs twice."""
|
||||
seq = bus.seq
|
||||
on_loop(server, close_channels(web.Application()))
|
||||
end_by_delete(server, entry)
|
||||
titles.release.set()
|
||||
assert titles.reply().status_code == 200
|
||||
actions = [e["data"]["action"] for e in bus.since(seq) if e["event"] == "session"]
|
||||
assert actions.count("delete") == 1 and actions[-1] == "delete"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("cache_tag", "busy"), [("_sessions/EXAMPLE", False), (None, True)], ids=["idle", "no-cache"])
|
||||
async def test_an_end_with_nothing_to_write_late_starts_no_task(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, cache_tag: str | None, busy: bool
|
||||
@@ -366,35 +262,6 @@ async def test_an_end_with_nothing_to_write_late_starts_no_task(
|
||||
assert await store.delete(entry.session_id) and not store._logins
|
||||
|
||||
|
||||
async def test_the_close_waits_for_a_late_write(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(config.directories, "cache", tmp_path)
|
||||
directory = tmp_path / "_sessions" / "EXAMPLE"
|
||||
directory.mkdir(parents=True)
|
||||
store = SessionStore()
|
||||
entry = await store.create("EXAMPLE", None, cache_tag="_sessions/EXAMPLE")
|
||||
await entry.lock.acquire()
|
||||
closing = asyncio.create_task(store.close())
|
||||
assert await asyncio.to_thread(wait_until, lambda: not directory.exists(), DEADLINE)
|
||||
directory.mkdir(parents=True)
|
||||
entry.lock.release()
|
||||
await asyncio.wait_for(closing, DEADLINE)
|
||||
assert not directory.exists()
|
||||
|
||||
|
||||
async def test_a_late_write_holds_the_close_no_longer_than_a_login(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setattr(config.directories, "cache", tmp_path)
|
||||
monkeypatch.setattr(session_store, "LOGIN_WAIT", 0.05)
|
||||
store = SessionStore()
|
||||
entry = await store.create("EXAMPLE", None, cache_tag="_sessions/EXAMPLE")
|
||||
await entry.lock.acquire()
|
||||
await asyncio.wait_for(store.close(), DEADLINE)
|
||||
(tmp_path / "_sessions" / "EXAMPLE").mkdir(parents=True)
|
||||
entry.lock.release()
|
||||
assert await asyncio.to_thread(wait_until, lambda: not (tmp_path / "_sessions").exists(), DEADLINE)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("error", [None, RuntimeError, SystemExit], ids=["answers", "fails", "exits"])
|
||||
@pytest.mark.parametrize(("call", "args"), [("request_input", ["code?"]), ("request_cdm", [{}]), ("ensure_active", [])])
|
||||
def test_a_request_that_the_end_cuts_is_refused(
|
||||
|
||||
Reference in New Issue
Block a user