Files
douyin_guaji-main/deleted_tests_backup/test_screenshot_service_ocr_stop.py
T
Your Name 0b5f150691 Refactor and clean up code across multiple scripts
- Removed trailing whitespace and ensured files end with a single newline in cleanup_unused_files.py.
- Added fix_trailing_whitespace.py to batch remove trailing whitespace from Python files.
- Updated install_dependencies.py to improve dependency installation prompts.
- Cleaned up pick_coords.py by removing unnecessary whitespace and improving code readability.
- Enhanced region_selector.py by disabling specific pylint checks for better compatibility with third-party libraries.
- Improved WPS流程图生成工具.py by cleaning up whitespace and enhancing readability.
- Updated __init__.py in typings to remove unnecessary lines.
- Modified app.js to adjust default configuration values and clean up unused code.
2026-01-02 23:57:03 +08:00

114 lines
3.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试OCR时停止后台截图服务的功能
"""
import time
import sys
import os
# 添加项目根目录到路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_screenshot_service_stop_during_ocr():
"""测试OCR过程中后台截图服务是否被停止"""
print("测试OCR时停止后台截图服务功能...")
# 1. 启动截图服务
print("\n1. 启动后台截图服务...")
from operations.screenshot_service import ScreenshotService
service = ScreenshotService.get_instance()
# 模拟设备ID
device_id = "emulator-5554"
if not service.is_running():
service.start(device_id, interval=1.5)
time.sleep(0.5)
if service.is_running():
print("[OK] 后台截图服务已启动")
else:
print("[FAIL] 后台截图服务启动失败")
return False
# 记录初始状态
initial_cache_duration = service._cache_valid_duration
print(f"初始缓存有效期: {initial_cache_duration}秒")
# 2. 模拟OCR操作
print("\n2. 模拟OCR操作...")
# 创建模拟的analyser
class MockAnalyser:
def __init__(self):
self.device_id = device_id
self._fudai_detail_task_count_from_stage = 2
def _log_kv(self, key, value, **kwargs):
timestamp = time.strftime("%H:%M:%S", time.localtime())
print(f"[{timestamp}] [{key}] {value}")
analyser = MockAnalyser()
# 记录开始时的服务状态
service_running_before = service.is_running()
print(f"OCR开始前服务运行状态: {service_running_before}")
start_time = time.time() # 初始化开始时间
try:
# 调用read_fudai_detail_once - 这应该会停止后台服务
from fudai_detail_modules.fudai_detail_reader import read_fudai_detail_once
start_time = time.time()
result = read_fudai_detail_once(analyser)
elapsed = time.time() - start_time
print(".2f")
print(f" 返回状态: {result[0]}")
except Exception as e:
elapsed = time.time() - start_time
print(".2f")
print(f" 错误: {e}")
# 3. 检查OCR后的服务状态
print("\n3. 检查OCR后的服务状态...")
time.sleep(0.5) # 等待服务状态稳定
service_running_after = service.is_running()
final_cache_duration = service._cache_valid_duration
print(f"OCR结束后服务运行状态: {service_running_after}")
print(f"最终缓存有效期: {final_cache_duration}秒")
# 4. 验证结果
success = True
if service_running_before and not service_running_after:
print("[OK] 后台截图服务在OCR期间被正确停止")
elif not service_running_before:
print("[INFO] 后台截图服务在OCR开始前就已经停止")
else:
print("[FAIL] 后台截图服务在OCR期间没有被停止")
success = False
if final_cache_duration == initial_cache_duration:
print("[OK] 缓存有效期在OCR结束后被正确恢复")
else:
print(f"[WARNING] 缓存有效期未恢复: 期望{initial_cache_duration}, 实际{final_cache_duration}")
# 5. 清理
if service.is_running():
service.stop()
print("\n5. 测试完成,停止后台截图服务")
print(f"\n测试结果: {'PASS' if success else 'FAIL'}")
return success
if __name__ == "__main__":
success = test_screenshot_service_stop_during_ocr()
sys.exit(0 if success else 1)