mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 23:45:07 +00:00
feat(remote)!: make remote serve WebSocket-only and refuse older clients with CLIENT_OUTDATED
This commit is contained in:
@@ -24,11 +24,9 @@ from aiohttp import web
|
||||
|
||||
from tests.remote.unit.test_remote_loopback import server # noqa: F401
|
||||
from tests.remote.unit.test_remote_loopback import (
|
||||
CODE,
|
||||
KEY,
|
||||
OTHER_KEY,
|
||||
StubService,
|
||||
gate_channel,
|
||||
make_client,
|
||||
on_loop,
|
||||
)
|
||||
@@ -54,20 +52,18 @@ from unshackle.core.api.client_presence import (
|
||||
from unshackle.core.api.input_bridge import AUTH_INPUT_TIMEOUT, InputBridge
|
||||
from unshackle.core.api.session_store import SessionEntry, SessionStore
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.remote_channel import CDM_CLOSE_TIMEOUT
|
||||
from unshackle.core.remote_channel import CDM_CLOSE_TIMEOUT, RECONNECT_DELAY, RECONNECT_MAX
|
||||
from unshackle.core.remote_lifetime import END_TIMEOUT
|
||||
from unshackle.core.remote_protocol import (
|
||||
CHANNEL_ROUTE,
|
||||
CHANNEL_SESSION_GONE,
|
||||
LOGS_ROUTE,
|
||||
PROMPT_ROUTE,
|
||||
SESSION_CREATE_ROUTE,
|
||||
SESSION_ROUTE,
|
||||
TITLES_ROUTE,
|
||||
APIErrorCode,
|
||||
AuthStatus,
|
||||
)
|
||||
from unshackle.core.remote_service import PROBE_TIMEOUT
|
||||
from unshackle.core.remote_service import RemoteError
|
||||
from unshackle.core.tunnel import HEARTBEAT
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
@@ -222,6 +218,7 @@ def test_a_request_that_waits_for_the_lock_does_not_start_after_the_end(
|
||||
assert create(server.url, session_id).status_code == 200
|
||||
(entry,) = sessions()
|
||||
assert logged_in(entry)
|
||||
entry.presence.attached()
|
||||
try:
|
||||
first = in_thread(lambda: get(server, TITLES_ROUTE, entry))
|
||||
assert wait_until(lambda: len(started) == 1, DEADLINE)
|
||||
@@ -235,37 +232,6 @@ def test_a_request_that_waits_for_the_lock_does_not_start_after_the_end(
|
||||
assert len(started) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("route", [SESSION_ROUTE, PROMPT_ROUTE, LOGS_ROUTE])
|
||||
def test_the_polls_of_a_client_without_its_channel_keep_its_session_during_a_long_request(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
logins: list[Any], # noqa: F811
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
route: str,
|
||||
) -> None:
|
||||
refuse, release = gate_channel(monkeypatch), Event()
|
||||
monkeypatch.setitem(StubService.hooks, "titles", lambda stub: release.wait(DEADLINE))
|
||||
service = make_client(server.url)
|
||||
try:
|
||||
service.authenticate()
|
||||
(entry,) = sessions()
|
||||
assert state_is(entry, ClientState.PRESENT)
|
||||
listing = in_thread(service.get_titles)
|
||||
assert wait_until(entry.lock.locked, DEADLINE)
|
||||
refuse.set()
|
||||
on_loop(server, entry.channel.ws.close()) # type: ignore[union-attr]
|
||||
assert state_is(entry, ClientState.AWAY), "a close that the server starts is not the client's"
|
||||
for _ in range(3):
|
||||
silent_for(entry, AWAY_LIMIT - 5)
|
||||
assert get(server, route, entry).ok and entry.lock.locked()
|
||||
assert entry.presence.silence < AWAY_LIMIT / 2 and entry.presence.state is ClientState.AWAY
|
||||
assert sweep(server) == 0
|
||||
release.set()
|
||||
assert len(listing()) == 3
|
||||
finally:
|
||||
release.set()
|
||||
service.close()
|
||||
|
||||
|
||||
def test_a_delete_after_the_close_of_the_channel_returns_the_login_cache(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
logins: list[Any], # noqa: F811
|
||||
@@ -331,34 +297,18 @@ def half_open_gateway(server: SimpleNamespace) -> Iterator[str]: # noqa: F811
|
||||
on_loop(server, runner.cleanup())
|
||||
|
||||
|
||||
def test_a_user_who_types_a_code_keeps_the_session_that_the_channel_failed_to_open(
|
||||
def test_a_refused_channel_upgrade_stops_the_client_with_one_line(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
logins: list[Any], # noqa: F811
|
||||
) -> None:
|
||||
"""The server saw a channel and its loss, and the client polls: it must do so while its user types."""
|
||||
typing, typed = Event(), Event()
|
||||
with half_open_gateway(server) as url:
|
||||
service = make_client(url)
|
||||
|
||||
def ask(prompt: str, qr: object = None) -> str:
|
||||
typing.set()
|
||||
assert typed.wait(DEADLINE)
|
||||
return CODE
|
||||
|
||||
service._input_bridge = SimpleNamespace(request_input=ask)
|
||||
monkeypatch.setitem(config.serve, "session_ttl", 3)
|
||||
login = in_thread(service.authenticate)
|
||||
try:
|
||||
assert typing.wait(DEADLINE)
|
||||
(entry,) = sessions()
|
||||
assert entry.presence.state is ClientState.AWAY
|
||||
silent_for(entry, AWAY_LIMIT - 5)
|
||||
assert wait_until(lambda: entry.presence.silence < AWAY_LIMIT / 2, DEADLINE), "the client does not poll"
|
||||
typed.set()
|
||||
assert login() is None
|
||||
with pytest.raises(RemoteError, match=r"^The remote server at \S+ refused the session channel: HTTP 502$"):
|
||||
service.authenticate()
|
||||
finally:
|
||||
typed.set()
|
||||
service.close()
|
||||
assert wait_until(lambda: sessions() == [], DEADLINE)
|
||||
|
||||
|
||||
Socket = aiohttp.ClientWebSocketResponse
|
||||
@@ -450,7 +400,7 @@ def test_a_new_channel_or_a_request_after_the_leave_is_a_client_that_still_works
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
channel: Callable[..., None],
|
||||
) -> None:
|
||||
"""A client that gives up on a slow channel closes it with 1000 and then polls."""
|
||||
"""A client that gives up on a slow channel closes it with 1000, then opens a new one or sends a request."""
|
||||
|
||||
async def check(entry: SessionEntry, connect: Callable[[], Any]) -> None:
|
||||
await (await connect()).close()
|
||||
@@ -478,26 +428,22 @@ def test_a_client_that_never_comes_back_for_its_session_loses_it(
|
||||
assert ends_after(server, entry, FIRST_CONTACT_LIMIT)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("client_kind", ["polls", "no-id"])
|
||||
def test_a_client_without_a_channel_keeps_the_ttl(
|
||||
def test_a_client_without_the_session_channel_is_refused(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
logins: list[Any], # noqa: F811
|
||||
client_kind: str,
|
||||
) -> None:
|
||||
if client_kind == "polls":
|
||||
assert create(server.url, str(uuid.uuid4())).status_code == 200
|
||||
(entry,) = sessions()
|
||||
assert get(server, PROMPT_ROUTE, entry).ok
|
||||
else:
|
||||
body = {"service": "EXAMPLE", "title_id": "example-1"}
|
||||
headers = {"X-Secret-Key": KEY}
|
||||
assert requests.post(server.url + SESSION_CREATE_ROUTE, json=body, headers=headers, timeout=DEADLINE).ok
|
||||
(entry,) = sessions()
|
||||
assert logged_in(entry) and stays(server, entry)
|
||||
entry.idle_since = time.monotonic() - (TTL - 5)
|
||||
assert sweep(server) == 0
|
||||
entry.idle_since = time.monotonic() - (TTL + 1)
|
||||
assert sweep(server) == 1
|
||||
body = {"service": "EXAMPLE", "title_id": "example-1"}
|
||||
headers = {"X-Secret-Key": KEY}
|
||||
refused = requests.post(server.url + SESSION_CREATE_ROUTE, json=body, headers=headers, timeout=DEADLINE)
|
||||
assert refused.status_code == 426 and refused.json()["error_code"] == APIErrorCode.CLIENT_OUTDATED
|
||||
assert "\n" not in refused.json()["message"] and sessions() == []
|
||||
assert create(server.url, str(uuid.uuid4())).status_code == 200
|
||||
(entry,) = sessions()
|
||||
assert logged_in(entry)
|
||||
titles = get(server, TITLES_ROUTE, entry)
|
||||
assert titles.status_code == 426 and titles.json()["error_code"] == APIErrorCode.CLIENT_OUTDATED
|
||||
assert get(server, SESSION_ROUTE, entry).ok
|
||||
assert entry.presence.state is ClientState.AWAITED
|
||||
|
||||
|
||||
@pytest.mark.parametrize("busy", [False, True], ids=["idle", "busy"])
|
||||
@@ -524,18 +470,14 @@ def test_each_limit_is_longer_than_the_silence_of_a_client_that_still_works() ->
|
||||
"""The numbers of the client: a ping is lost, the DELETE is late, or the create reply never arrives."""
|
||||
detection = HEARTBEAT * 1.5
|
||||
delete_arrives = 5 + CDM_CLOSE_TIMEOUT + 3 * END_TIMEOUT
|
||||
reconnects = sum((1, 2, 4, 8, 16, 30))
|
||||
reconnects = sum(min(RECONNECT_DELAY * 2**n, RECONNECT_MAX) for n in range(6))
|
||||
assert 120 + END_TIMEOUT < FIRST_CONTACT_LIMIT <= 150
|
||||
assert detection + PROBE_TIMEOUT + 100 < AWAY_LIMIT <= 180
|
||||
assert detection + reconnects < AWAY_LIMIT <= 180
|
||||
assert max(delete_arrives, reconnects) < LEFT_LIMIT <= 90
|
||||
presence = ClientPresence()
|
||||
presence.detached(left=False)
|
||||
for ttl_seconds in range(1, 7200):
|
||||
assert presence.limit(ttl_seconds) > ttl_seconds / 3 + PROBE_TIMEOUT
|
||||
|
||||
|
||||
def test_a_client_that_is_present_has_no_limit() -> None:
|
||||
presence = ClientPresence(named=True)
|
||||
presence = ClientPresence()
|
||||
presence.attached()
|
||||
assert presence.limit(TTL) == math.inf
|
||||
|
||||
|
||||
Reference in New Issue
Block a user