You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
5.0 KiB

#!/usr/bin/env python3
"""
设备断开事件修复测试
验证设备连接和断开事件能正确区分
"""
import asyncio
import sys
import os
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app.core.device.manager import device_manager
from app.services.auto_discovery_adb_service import AutoDiscoveryAdbService
from app.core.adb.client import DeviceEvent
from app.utils.structured_log import get_structured_logger, LogLevel
logger = get_structured_logger(__name__, LogLevel.INFO)
async def test_device_disconnect_fix():
"""测试设备断开事件修复"""
print("开始设备断开事件修复测试...")
try:
# 1. 初始化
print("\n1. 初始化测试环境...")
auto_discovery_service = AutoDiscoveryAdbService()
print("✅ 自动发现ADB服务初始化成功")
# 2. 模拟设备连接事件
print("\n2. 模拟设备连接事件...")
connect_event = DeviceEvent(present=True, serial="test_device_001", status="device")
await auto_discovery_service._handle_device_event(connect_event)
print("✅ 设备连接事件处理完成")
# 3. 检查设备是否在自动发现列表中
print("\n3. 检查设备是否在自动发现列表中...")
auto_devices = await device_manager.get_auto_discovered_devices()
device_found = any(device["device_id"] == "test_device_001" for device in auto_devices)
if device_found:
print("✅ 设备已添加到自动发现列表")
else:
print("❌ 设备未添加到自动发现列表")
return False
# 4. 模拟设备断开事件
print("\n4. 模拟设备断开事件...")
disconnect_event = DeviceEvent(present=False, serial="test_device_001", status="device")
await auto_discovery_service._handle_device_event(disconnect_event)
print("✅ 设备断开事件处理完成")
# 5. 检查设备是否从自动发现列表中移除
print("\n5. 检查设备是否从自动发现列表中移除...")
auto_devices_after = await device_manager.get_auto_discovered_devices()
device_still_exists = any(device["device_id"] == "test_device_001" for device in auto_devices_after)
if not device_still_exists:
print("✅ 设备已从自动发现列表中移除")
else:
print("❌ 设备仍存在于自动发现列表中")
return False
# 6. 测试统一设备列表
print("\n6. 测试统一设备列表...")
unified_devices = await device_manager.get_all_devices_unified()
device_in_unified = any(device["device_id"] == "test_device_001" for device in unified_devices)
if not device_in_unified:
print("✅ 设备已从统一设备列表中移除")
else:
print("❌ 设备仍存在于统一设备列表中")
return False
# 7. 再次连接设备
print("\n7. 再次连接设备...")
connect_event_2 = DeviceEvent(present=True, serial="test_device_002", status="device")
await auto_discovery_service._handle_device_event(connect_event_2)
print("✅ 第二个设备连接事件处理完成")
# 8. 检查第二个设备
print("\n8. 检查第二个设备...")
auto_devices_final = await device_manager.get_auto_discovered_devices()
device_2_found = any(device["device_id"] == "test_device_002" for device in auto_devices_final)
if device_2_found:
print("✅ 第二个设备已添加到自动发现列表")
else:
print("❌ 第二个设备未添加到自动发现列表")
return False
# 9. 断开第二个设备
print("\n9. 断开第二个设备...")
disconnect_event_2 = DeviceEvent(present=False, serial="test_device_002", status="device")
await auto_discovery_service._handle_device_event(disconnect_event_2)
print("✅ 第二个设备断开事件处理完成")
# 10. 最终检查
print("\n10. 最终检查...")
final_devices = await device_manager.get_auto_discovered_devices()
if len(final_devices) == 0:
print("✅ 所有设备已正确移除,自动发现列表为空")
else:
print(f"❌ 自动发现列表仍有 {len(final_devices)} 个设备")
return False
print("\n🎉 设备断开事件修复测试通过!")
print("✅ 设备连接和断开事件能正确区分和处理")
return True
except Exception as e:
print(f"\n❌ 测试失败: {e}")
import traceback
traceback.print_exc()
return False
finally:
# 清理资源
print("\n清理测试资源...")
await device_manager.cleanup()
print("✅ 资源清理完成")
if __name__ == "__main__":
asyncio.run(test_device_disconnect_fix())