Files
gogo2/test_realtime_cob.py
Dobromir Popov 3cadae60f7 COB data and dash
2025-06-18 16:23:47 +03:00

92 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""
Test script for real-time COB functionality
"""
import asyncio
import aiohttp
import json
import time
from datetime import datetime
async def test_realtime_cob():
"""Test real-time COB data streaming"""
# Test API endpoints
base_url = "http://localhost:8053"
async with aiohttp.ClientSession() as session:
print("Testing COB Dashboard API endpoints...")
# Test symbols endpoint
try:
async with session.get(f"{base_url}/api/symbols") as response:
if response.status == 200:
data = await response.json()
print(f"✓ Symbols: {data}")
else:
print(f"✗ Symbols endpoint failed: {response.status}")
except Exception as e:
print(f"✗ Error testing symbols endpoint: {e}")
# Test real-time stats for BTC/USDT
try:
async with session.get(f"{base_url}/api/realtime/BTC/USDT") as response:
if response.status == 200:
data = await response.json()
print(f"✓ Real-time stats for BTC/USDT:")
print(f" Current mid price: {data.get('current', {}).get('mid_price', 'N/A')}")
print(f" 1s window updates: {data.get('1s_window', {}).get('update_count', 'N/A')}")
print(f" 5s window updates: {data.get('5s_window', {}).get('update_count', 'N/A')}")
else:
print(f"✗ Real-time stats endpoint failed: {response.status}")
error_data = await response.text()
print(f" Error: {error_data}")
except Exception as e:
print(f"✗ Error testing real-time stats endpoint: {e}")
# Test WebSocket connection
print("\nTesting WebSocket connection...")
try:
async with session.ws_connect(f"{base_url.replace('http', 'ws')}/ws") as ws:
print("✓ WebSocket connected")
# Wait for some data
message_count = 0
start_time = time.time()
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
data = json.loads(msg.data)
message_count += 1
if data.get('type') == 'cob_update':
symbol = data.get('data', {}).get('stats', {}).get('symbol', 'Unknown')
mid_price = data.get('data', {}).get('stats', {}).get('mid_price', 0)
print(f"✓ Received COB update for {symbol}: ${mid_price:.2f}")
# Check for real-time stats
if 'realtime_1s' in data.get('data', {}).get('stats', {}):
print(f" ✓ Real-time 1s stats available")
if 'realtime_5s' in data.get('data', {}).get('stats', {}):
print(f" ✓ Real-time 5s stats available")
# Stop after 5 messages or 10 seconds
if message_count >= 5 or (time.time() - start_time) > 10:
break
elif msg.type == aiohttp.WSMsgType.ERROR:
print(f"✗ WebSocket error: {ws.exception()}")
break
print(f"✓ Received {message_count} WebSocket messages")
except Exception as e:
print(f"✗ WebSocket connection failed: {e}")
if __name__ == "__main__":
print("Testing Real-time COB Dashboard")
print("=" * 40)
asyncio.run(test_realtime_cob())
print("\nTest completed!")