Import AITURK IDE 1.0.0-beta.1 from Hermes 63279301; preserve MIT license
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
"""Tests for tui_gateway.loop_noise — the WS peer-hangup teardown filter (#50005)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from tui_gateway.loop_noise import (
|
||||
_is_benign_teardown,
|
||||
install_loop_noise_filter,
|
||||
)
|
||||
|
||||
|
||||
class _FakeConnectionLostCallback:
|
||||
"""Stand-in whose repr matches asyncio's ``_call_connection_lost`` flood."""
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "<Handle _ProactorBasePipeTransport._call_connection_lost(None)>"
|
||||
|
||||
|
||||
def test_benign_teardown_matches_reset_in_connection_lost():
|
||||
ctx = {
|
||||
"exception": ConnectionResetError(10054, "forcibly closed"),
|
||||
"handle": _FakeConnectionLostCallback(),
|
||||
}
|
||||
assert _is_benign_teardown(ctx) is True
|
||||
|
||||
|
||||
def test_benign_teardown_matches_aborted_and_broken_pipe():
|
||||
for exc in (
|
||||
ConnectionAbortedError(10053, "aborted"),
|
||||
BrokenPipeError("epipe"),
|
||||
):
|
||||
ctx = {"exception": exc, "callback": _FakeConnectionLostCallback()}
|
||||
assert _is_benign_teardown(ctx) is True
|
||||
|
||||
|
||||
def test_reset_outside_connection_lost_is_not_suppressed():
|
||||
# Same error type, but NOT from the connection-lost teardown path — must
|
||||
# fall through to the default handler.
|
||||
ctx = {
|
||||
"exception": ConnectionResetError("reset in a real handler"),
|
||||
"handle": "<Handle some_other_handler()>",
|
||||
}
|
||||
assert _is_benign_teardown(ctx) is False
|
||||
|
||||
|
||||
def test_unrelated_exception_is_not_suppressed():
|
||||
ctx = {
|
||||
"exception": ValueError("boom"),
|
||||
"handle": _FakeConnectionLostCallback(),
|
||||
}
|
||||
assert _is_benign_teardown(ctx) is False
|
||||
|
||||
|
||||
def test_no_exception_is_not_suppressed():
|
||||
assert _is_benign_teardown({"message": "loop warning, no exc"}) is False
|
||||
|
||||
|
||||
def test_install_suppresses_flood_and_forwards_real_errors():
|
||||
loop = asyncio.new_event_loop()
|
||||
try:
|
||||
forwarded: list[dict] = []
|
||||
loop.set_exception_handler(lambda _loop, ctx: forwarded.append(ctx))
|
||||
|
||||
install_loop_noise_filter(loop)
|
||||
|
||||
# Benign teardown flood → swallowed, not forwarded.
|
||||
loop.call_exception_handler(
|
||||
{
|
||||
"exception": ConnectionResetError(10054, "forcibly closed"),
|
||||
"handle": _FakeConnectionLostCallback(),
|
||||
}
|
||||
)
|
||||
assert forwarded == []
|
||||
|
||||
# Genuine loop error → forwarded to the previous handler unchanged.
|
||||
real_ctx = {"exception": RuntimeError("genuine loop bug")}
|
||||
loop.call_exception_handler(real_ctx)
|
||||
assert len(forwarded) == 1
|
||||
assert forwarded[0] is real_ctx
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user