From 2a3a23c3c95a99d959aaeca5f80447fa921b452a Mon Sep 17 00:00:00 2001 From: Nixevol Date: Mon, 16 Nov 2026 17:44:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B2=A1=E6=9C=89=E6=89=93=E5=BC=80?= =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=97=B6=E4=B8=8D=E5=86=8D=E5=AE=89=E6=8E=92?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E7=8A=B6=E6=80=81=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/euniv/study_push.py | 13 ++++++++++--- backend/tests/test_study_push.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/backend/euniv/study_push.py b/backend/euniv/study_push.py index 801c8b8..677648f 100644 --- a/backend/euniv/study_push.py +++ b/backend/euniv/study_push.py @@ -91,13 +91,20 @@ def notify(employee_id: str | None = None, urgent: bool = False) -> None: loop = _loop if loop is None or not loop.is_running(): return - keys = [(employee_id or "").strip()] if employee_id else list(_clients) - for key in keys: - if key: + if employee_id is not None: + key = employee_id.strip() + if not key or not _clients.get(key): + return + loop.call_soon_threadsafe(_mark, key, urgent) + return + for key in list(_clients): + if key and _clients.get(key): loop.call_soon_threadsafe(_mark, key, urgent) def _mark(employee_id: str, urgent: bool) -> None: + if not _clients.get(employee_id): + return _dirty.add(employee_id) if urgent: _urgent.add(employee_id) diff --git a/backend/tests/test_study_push.py b/backend/tests/test_study_push.py index 7f68660..70fd9df 100644 --- a/backend/tests/test_study_push.py +++ b/backend/tests/test_study_push.py @@ -73,3 +73,20 @@ def test_close_account_drops_open_socket() -> None: assert "gone" not in study_push._clients asyncio.run(main()) + + +def test_notify_without_a_socket_does_not_schedule_a_flush() -> None: + async def main() -> None: + study_push._pending.clear() + study_push._inflight.clear() + study_push._dirty.clear() + study_push._urgent.clear() + study_push._clients.clear() + study_push.attach_loop(asyncio.get_running_loop()) + study_push.notify("nobody") + await asyncio.sleep(0) + assert study_push._pending == {} + assert study_push._inflight == set() + assert "nobody" not in study_push._dirty + + asyncio.run(main())