From 10247851d4079aa73aeacb62085972e705ddd105 Mon Sep 17 00:00:00 2001 From: James Pine Date: Sun, 19 Apr 2026 19:27:21 -0700 Subject: [PATCH] test(offline): make concurrency test deterministic and bounded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the `sleep(0.15)` ordering hack with an explicit `threading.Event` the fast thread sets in `finally`. The slow thread waits on that event (bounded), then observes the flag — so we deterministically verify the slow thread still sees offline mode after the fast thread has exited. Also add timeouts to `barrier.wait()` and assert `not thread.is_alive()` after the joins so the test can't hang on an unexpected failure path. Co-Authored-By: Claude Opus 4.7 (1M context) --- backend/tests/test_offline_guard.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/backend/tests/test_offline_guard.py b/backend/tests/test_offline_guard.py index 8a5a4e8f..5e3d2cd9 100644 --- a/backend/tests/test_offline_guard.py +++ b/backend/tests/test_offline_guard.py @@ -15,7 +15,6 @@ run this file serially. import os import sys import threading -import time from pathlib import Path import pytest @@ -81,12 +80,13 @@ def test_concurrent_threads_share_offline_window(): observations: list[bool] = [] errors: list[Exception] = [] barrier = threading.Barrier(2) + fast_exited = threading.Event() def slow(): try: with force_offline_if_cached(True, "slow"): - barrier.wait() # sync with fast - time.sleep(0.15) # fast will exit during this sleep + barrier.wait(timeout=5) + assert fast_exited.wait(timeout=5), "fast thread did not exit" observations.append(_hf_const().HF_HUB_OFFLINE) except Exception as exc: # noqa: BLE001 errors.append(exc) @@ -94,9 +94,11 @@ def test_concurrent_threads_share_offline_window(): def fast(): try: with force_offline_if_cached(True, "fast"): - barrier.wait() + barrier.wait(timeout=5) except Exception as exc: # noqa: BLE001 errors.append(exc) + finally: + fast_exited.set() t_slow = threading.Thread(target=slow) t_fast = threading.Thread(target=fast) @@ -105,8 +107,10 @@ def test_concurrent_threads_share_offline_window(): t_slow.join(timeout=5) t_fast.join(timeout=5) + assert not t_slow.is_alive(), "slow thread did not finish" + assert not t_fast.is_alive(), "fast thread did not finish" assert not errors, errors - assert [True] == observations, "slow thread lost offline protection" + assert observations == [True], "slow thread lost offline protection" assert original == _hf_const().HF_HUB_OFFLINE