fix: refresh quotes without blocking UI

This commit is contained in:
王鹏
2026-07-09 10:01:15 +08:00
parent 1e8858ac04
commit 659846fe18
3 changed files with 114 additions and 4 deletions

View File

@@ -1,4 +1,7 @@
from decimal import Decimal
import time
from grid_trading.domain.models import Instrument
def test_formatters_render_money_percent_and_empty_values():
@@ -30,3 +33,40 @@ def test_main_window_can_be_constructed_offscreen(tmp_path, monkeypatch):
window.close()
service.close()
app.processEvents()
def test_quote_refresh_does_not_block_main_window(tmp_path, monkeypatch):
monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen")
from PySide6.QtWidgets import QApplication
from grid_trading.services.trading_service import TradingService
from grid_trading.ui.main_window import MainWindow
class SlowQuoteProvider:
def fetch_quotes(self, instruments):
time.sleep(0.3)
return {}
app = QApplication.instance() or QApplication([])
service = TradingService(tmp_path / "grid.db", quote_provider=SlowQuoteProvider())
service.ensure_defaults()
service.add_instrument(Instrument(id=None, code="000001", name="Ping An Bank"))
window = MainWindow(service)
started_at = time.perf_counter()
window._refresh_quotes()
elapsed = time.perf_counter() - started_at
assert elapsed < 0.15
deadline = time.perf_counter() + 2
while getattr(window, "_quote_thread", None) is not None and time.perf_counter() < deadline:
app.processEvents()
time.sleep(0.01)
assert getattr(window, "_quote_thread", None) is None
window.close()
service.close()
app.processEvents()