Skip to content

添加了对API的测试;修改了WS的管理类#14

Closed
qwasd7680 wants to merge 2 commits intomainfrom
dev
Closed

添加了对API的测试;修改了WS的管理类#14
qwasd7680 wants to merge 2 commits intomainfrom
dev

Conversation

@qwasd7680
Copy link
Copy Markdown
Owner

No description provided.

Comment on lines +60 to +61
except Exception:
pass
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception handling in the _send_and_close method is overly broad and silently ignores all exceptions. This can suppress important errors that should be handled or logged to maintain robustness and aid in debugging.

Recommendation:

  • Modify the exception handling to log the error or handle specific exceptions more appropriately. For example:
except WebSocketException as e:
    log.error(f"Failed to close WebSocket for client {client_id}: {e}")
except Exception as e:
    log.error(f"Unexpected error: {e}")

Comment on lines 106 to 111
print(f"[Task] 开始执行相册 {album_id} 的阻塞下载任务...")

try:
# 配置 Jmcomic 选项 (使用 Path 对象来构建 base_dir)
optionStr = f"""
client:
cache: null
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sync_download_and_zip_task function is a blocking operation within an asynchronous FastAPI application. This can lead to performance issues as it might block the server's event loop, affecting the responsiveness of the application.

Recommendation:

  • Consider refactoring this function to be asynchronous or ensure that it runs in a way that does not block the event loop. Utilizing asyncio for file I/O operations or processing could improve performance. For example:
async def async_download_and_zip_task(album_id: int, client_id: str):
    # async implementation here

Comment on lines +53 to +54
file_path.unlink()
assert file_path.exists() == False
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test performs a file deletion (file_path.unlink()) and immediately checks if the file still exists. This approach assumes the file operation always succeeds, which might not be the case due to external factors like file system permissions or locks.

Recommendation:
To improve the robustness of the test, consider wrapping file operations in a try-except block to handle potential exceptions and provide more informative error messages if operations fail. Additionally, use a setup and teardown method for file creation and deletion to ensure test isolation.

Comment on lines +79 to +80
zip_file_path.unlink()
assert zip_file_path.exists() == False
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous issue, this test also checks the existence of a file after attempting to delete it. This can lead to false negatives if the delete operation fails for reasons not captured by the test.

Recommendation:
Implement error handling around file operations and consider using a more robust method for ensuring files are cleaned up after tests. This could include using Python's tempfile module for temporary files that are automatically cleaned up.

Comment on lines 6 to 7
FILE_PATH = Path(f"{current_dir}/temp")
os.makedirs(FILE_PATH, exist_ok=True)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security and Error Handling Issue

The use of os.makedirs(FILE_PATH, exist_ok=True) directly with a path constructed from os.getcwd() can lead to security vulnerabilities and runtime errors if the directory permissions are not properly set or if the directory cannot be created due to system restrictions.

Recommendation:

  • Ensure that the directory permissions are checked before attempting to create the directory.
  • Handle exceptions that may arise from os.makedirs to prevent the test from failing unexpectedly due to file system issues.
  • Consider using a temporary directory specifically for testing purposes, which can be managed and cleaned up by the testing framework (e.g., using tempfile.TemporaryDirectory() in Python).

@qwasd7680 qwasd7680 closed this Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant