feat(hub): background chart poll with SSE for positions and market watch
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
"""行情区 chart 后台轮询订阅。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
sys.path.insert(0, str(ROOT / "manual_trading_hub"))
|
||||
|
||||
from hub_chart_cache import ChartPollStore, series_key # noqa: E402
|
||||
|
||||
|
||||
class TestHubChartCache(unittest.TestCase):
|
||||
def test_series_key(self) -> None:
|
||||
self.assertEqual(series_key("Gate_X", "hype/usdt", "5m"), "gate_x|HYPE/USDT|5m")
|
||||
|
||||
def test_position_and_watch_keys(self) -> None:
|
||||
store = ChartPollStore()
|
||||
store.sync_positions_from_rows(
|
||||
[
|
||||
{
|
||||
"key": "okx_auto",
|
||||
"agent": {
|
||||
"ok": True,
|
||||
"positions": [{"symbol": "BTC/USDT"}, {"symbol": "ETH/USDT"}],
|
||||
},
|
||||
}
|
||||
]
|
||||
)
|
||||
store.touch_watch("gate_trend", "HYPE/USDT", "5m")
|
||||
keys = store.active_series_keys()
|
||||
self.assertIn(series_key("okx_auto", "BTC/USDT", "5m"), keys)
|
||||
self.assertIn(series_key("gate_trend", "HYPE/USDT", "5m"), keys)
|
||||
|
||||
def test_poll_increments_version(self) -> None:
|
||||
async def run() -> None:
|
||||
store = ChartPollStore()
|
||||
n = 0
|
||||
|
||||
async def poll():
|
||||
nonlocal n
|
||||
n += 1
|
||||
store.touch_watch("binance", "BTC/USDT", "1d")
|
||||
return {"ok": True, "n": n}
|
||||
|
||||
await store.start(poll)
|
||||
await asyncio.sleep(0.05)
|
||||
self.assertGreaterEqual(store.version, 1)
|
||||
await store.stop()
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user