137 lines
5.1 KiB
Python
137 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
并行设备启动测试脚本
|
|
测试设备启动修改为并行启动,确保深度相机启动失败不影响其他设备启动
|
|
"""
|
|
|
|
import requests
|
|
import time
|
|
import json
|
|
|
|
class ParallelStartupTester:
|
|
def __init__(self, base_url="http://localhost:5001"):
|
|
self.base_url = base_url
|
|
|
|
def test_parallel_startup(self):
|
|
"""测试并行启动功能"""
|
|
print("=== 测试并行设备启动功能 ===")
|
|
|
|
try:
|
|
# 1. 首先停止任何正在运行的测试
|
|
print("1. 停止当前测试...")
|
|
response = requests.get(f"{self.base_url}/api/test/stop")
|
|
if response.status_code == 200:
|
|
print("✓ 成功停止当前测试")
|
|
else:
|
|
print(f"⚠ 停止测试响应: {response.status_code}")
|
|
|
|
time.sleep(2)
|
|
|
|
# 2. 启动设备测试
|
|
print("\n2. 启动设备测试...")
|
|
response = requests.get(f"{self.base_url}/api/test/start")
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
print("✓ 设备测试启动成功")
|
|
print(f"响应消息: {result.get('message', 'N/A')}")
|
|
|
|
# 等待设备初始化完成
|
|
print("\n3. 等待设备初始化完成...")
|
|
time.sleep(10) # 给设备足够时间进行并行初始化
|
|
|
|
# 3. 检查设备状态
|
|
print("\n4. 检查设备状态...")
|
|
status_response = requests.get(f"{self.base_url}/api/device/status")
|
|
|
|
if status_response.status_code == 200:
|
|
status = status_response.json()
|
|
print("✓ 获取设备状态成功")
|
|
print(f"测试状态: {status.get('is_testing', 'N/A')}")
|
|
|
|
# 显示设备状态详情
|
|
if 'devices' in status:
|
|
print("\n设备状态详情:")
|
|
for device_name, device_status in status['devices'].items():
|
|
print(f" {device_name}: {device_status}")
|
|
|
|
return True
|
|
else:
|
|
print(f"✗ 获取设备状态失败: {status_response.status_code}")
|
|
return False
|
|
|
|
else:
|
|
print(f"✗ 设备测试启动失败: {response.status_code}")
|
|
print(f"响应内容: {response.text}")
|
|
return False
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
print("✗ 无法连接到设备测试服务器,请确保服务器正在运行")
|
|
return False
|
|
except Exception as e:
|
|
print(f"✗ 测试过程中发生错误: {e}")
|
|
return False
|
|
|
|
def test_device_resilience(self):
|
|
"""测试设备故障恢复能力"""
|
|
print("\n=== 测试设备故障恢复能力 ===")
|
|
|
|
try:
|
|
# 多次启动和停止测试,验证系统稳定性
|
|
for i in range(3):
|
|
print(f"\n第 {i+1} 轮测试:")
|
|
|
|
# 启动测试
|
|
start_response = requests.get(f"{self.base_url}/api/test/start")
|
|
if start_response.status_code == 200:
|
|
print(" ✓ 启动成功")
|
|
else:
|
|
print(f" ✗ 启动失败: {start_response.status_code}")
|
|
|
|
time.sleep(5) # 等待设备初始化
|
|
|
|
# 停止测试
|
|
stop_response = requests.get(f"{self.base_url}/api/test/stop")
|
|
if stop_response.status_code == 200:
|
|
print(" ✓ 停止成功")
|
|
else:
|
|
print(f" ✗ 停止失败: {stop_response.status_code}")
|
|
|
|
time.sleep(2) # 等待清理
|
|
|
|
print("\n✓ 设备故障恢复能力测试完成")
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"✗ 故障恢复测试失败: {e}")
|
|
return False
|
|
|
|
def run_all_tests(self):
|
|
"""运行所有测试"""
|
|
print("开始并行设备启动测试...\n")
|
|
|
|
# 测试并行启动
|
|
test1_result = self.test_parallel_startup()
|
|
|
|
# 测试设备故障恢复
|
|
test2_result = self.test_device_resilience()
|
|
|
|
# 输出测试结果
|
|
print("\n=== 测试结果汇总 ===")
|
|
print(f"并行启动测试: {'✓ 通过' if test1_result else '✗ 失败'}")
|
|
print(f"故障恢复测试: {'✓ 通过' if test2_result else '✗ 失败'}")
|
|
|
|
if test1_result and test2_result:
|
|
print("\n🎉 所有测试通过!并行设备启动功能正常工作")
|
|
else:
|
|
print("\n⚠ 部分测试失败,请检查日志")
|
|
|
|
return test1_result and test2_result
|
|
|
|
def main():
|
|
tester = ParallelStartupTester()
|
|
tester.run_all_tests()
|
|
|
|
if __name__ == '__main__':
|
|
main() |