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:
@@ -4,14 +4,12 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import threading
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from aiohttp import WSMessage
|
||||
from aiohttp.http_websocket import WS_CLOSED_MESSAGE
|
||||
|
||||
from tests.remote.unit.test_tunnel import wait_until
|
||||
from unshackle.core.api.input_bridge import InputBridge
|
||||
from unshackle.core.api.session_log import SessionLogBuffer
|
||||
from unshackle.core.api.session_store import SessionEntry
|
||||
@@ -45,35 +43,6 @@ def entry_with_logs(count: int) -> SessionEntry:
|
||||
return entry
|
||||
|
||||
|
||||
def log_seqs(socket: FakeSocket) -> list[int]:
|
||||
return [m["seq"] for m in socket.sent if m["type"] == "log"]
|
||||
|
||||
|
||||
async def test_the_backlog_goes_before_a_record_logged_while_the_old_socket_closes() -> None:
|
||||
entry = entry_with_logs(4)
|
||||
old = FakeSocket()
|
||||
|
||||
async def close_slowly(code: int = 1000, message: bytes = b"") -> bool:
|
||||
entry.log_buffer.append(logging.INFO, "EXAMPLE line 5")
|
||||
await asyncio.sleep(0)
|
||||
old.closed.set()
|
||||
return True
|
||||
|
||||
old.close = close_slowly # type: ignore[method-assign]
|
||||
entry.channel.ws = old # type: ignore[assignment]
|
||||
new = FakeSocket()
|
||||
serving = asyncio.create_task(entry.channel.serve(new, entry, 2, False)) # type: ignore[arg-type]
|
||||
for _ in range(200):
|
||||
if len(log_seqs(new)) >= 3:
|
||||
break
|
||||
await asyncio.sleep(0.01)
|
||||
assert log_seqs(new) == [3, 4, 5]
|
||||
assert {"type": "status", "status": "authenticating"} in new.sent
|
||||
await new.close()
|
||||
await serving
|
||||
assert not entry.channel.attached
|
||||
|
||||
|
||||
async def test_a_socket_that_cannot_send_is_closed_and_detached() -> None:
|
||||
entry = entry_with_logs(1)
|
||||
broken = FakeSocket()
|
||||
@@ -85,55 +54,3 @@ async def test_a_socket_that_cannot_send_is_closed_and_detached() -> None:
|
||||
await asyncio.wait_for(entry.channel.serve(broken, entry, 0, False), 5) # type: ignore[arg-type]
|
||||
assert broken.closed.is_set()
|
||||
assert not entry.channel.attached
|
||||
|
||||
|
||||
async def sent_cdm_calls(socket: FakeSocket) -> list[dict[str, Any]]:
|
||||
for _ in range(200):
|
||||
if calls := [m for m in socket.sent if m["type"] == "cdm_call"]:
|
||||
return calls
|
||||
await asyncio.sleep(0.01)
|
||||
return []
|
||||
|
||||
|
||||
async def test_a_detached_channel_keeps_its_waiting_asks_for_the_next_attach() -> None:
|
||||
entry = entry_with_logs(0)
|
||||
bridge = entry.input_bridge
|
||||
assert bridge is not None
|
||||
socket = FakeSocket()
|
||||
serving = asyncio.create_task(entry.channel.serve(socket, entry, 0, False)) # type: ignore[arg-type]
|
||||
await asyncio.sleep(0)
|
||||
answers: list[Any] = []
|
||||
worker = threading.Thread(target=lambda: answers.append(bridge.request_cdm({"op": "close"}, timeout=5)))
|
||||
worker.start()
|
||||
(call,) = await sent_cdm_calls(socket)
|
||||
await socket.close()
|
||||
await serving
|
||||
again = FakeSocket()
|
||||
serving = asyncio.create_task(entry.channel.serve(again, entry, 0, False)) # type: ignore[arg-type]
|
||||
assert await sent_cdm_calls(again) == [call]
|
||||
assert bridge.answer(call["id"], '{"op": "close"}')
|
||||
await asyncio.to_thread(worker.join, 5)
|
||||
assert answers == [{"op": "close"}]
|
||||
await again.close()
|
||||
await serving
|
||||
|
||||
|
||||
def test_a_prompt_the_client_refuses_stops_the_service_at_once() -> None:
|
||||
bridge = InputBridge()
|
||||
offered: list[Any] = []
|
||||
bridge.channel = type("Attached", (), {"attached": True, "offer": lambda self, ask: offered.append(ask) or True})()
|
||||
failed: list[BaseException] = []
|
||||
|
||||
def ask() -> None:
|
||||
try:
|
||||
bridge.request_input("Enter the EXAMPLE code", timeout=30)
|
||||
except RuntimeError as e:
|
||||
failed.append(e)
|
||||
|
||||
worker = threading.Thread(target=ask)
|
||||
worker.start()
|
||||
assert wait_until(lambda: bool(offered), 5)
|
||||
assert bridge.answer(offered[0].id, "", "stdin is closed")
|
||||
worker.join(5)
|
||||
assert not worker.is_alive()
|
||||
assert "stdin is closed" in str(failed[0])
|
||||
|
||||
Reference in New Issue
Block a user