fix: stabilize long-running quote refresh

This commit is contained in:
王鹏
2026-11-09 10:42:08 +08:00
parent b7e0894f4b
commit 5f4df03581
6 changed files with 281 additions and 84 deletions

View File

@@ -5,6 +5,7 @@ import pytest
from grid_trading.domain.models import Instrument
from grid_trading.market.tencent import (
QuoteFetchError,
TENCENT_ENDPOINT,
TencentQuoteProvider,
infer_tencent_symbol,
parse_tencent_response,
@@ -18,6 +19,20 @@ def test_infer_tencent_symbol_from_a_share_code():
assert infer_tencent_symbol("sz000001") == "sz000001"
def test_provider_builds_request_url_for_async_qt_client():
provider = TencentQuoteProvider()
url = provider.build_request_url(
[
Instrument(id=1, code="000001", name="平安银行"),
Instrument(id=2, code="510300", name="沪深300ETF"),
]
)
assert provider.uses_default_fetcher
assert url == f"{TENCENT_ENDPOINT}sz000001,sh510300"
def test_parse_tencent_response_extracts_realtime_fields():
raw = (
'v_sz000001="51~平安银行~000001~10.60~10.47~10.44~0~0~0~'

View File

@@ -410,7 +410,25 @@ def test_quote_refresh_does_not_block_main_window(tmp_path, monkeypatch):
assert not window.quote_monitor.is_refreshing
worker_thread = window.quote_monitor._thread
assert worker_thread is not None
assert worker_thread.isRunning()
assert window.quote_monitor.refresh()
deadline = time.perf_counter() + 2
while window.quote_monitor.is_refreshing and time.perf_counter() < deadline:
app.processEvents()
time.sleep(0.01)
assert not window.quote_monitor.is_refreshing
assert window.quote_monitor._thread is worker_thread
window.close()
deadline = time.perf_counter() + 2
while window._close_pending and time.perf_counter() < deadline:
app.processEvents()
time.sleep(0.01)
assert not window._close_pending
service.close()
app.processEvents()