Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

g3-dpi: 添加 stomp 协议识别代码的测试用例 #604

Open
zh-jq-b opened this issue Mar 12, 2025 · 0 comments
Open

g3-dpi: 添加 stomp 协议识别代码的测试用例 #604

zh-jq-b opened this issue Mar 12, 2025 · 0 comments
Labels
good first issue Good for newcomers

Comments

@zh-jq-b
Copy link
Member

zh-jq-b commented Mar 12, 2025

问题描述

当前项目中 lib/g3-dpi/src/protocol/stomp.rs 文件实现了 STOMP 协议的检查逻辑,但缺少对应的单元测试。需要在lib/g3-dpi/tests 目录下添加单元测试,以确保STOMP协议识别的正确性。

任务要求

  1. 在g3-dpi的 tests 目录下创建一个新的测试文件stomp.rs
  2. ProtocolInspectState 中的以下方法编写单元测试:
    • check_stomp_client_connect_request
  3. 测试用例应覆盖以下场景:
    • 正常情况下,输入符合 STOMP 协议的请求数据,方法应返回正确的 Protocol::Stomp
    • 输入不符合 STOMP 协议的请求数据,方法应返回 Ok(None)
    • 输入数据长度不足,方法应返回 Err(ProtocolInspectError::NeedMoreData)
    • 异常输入场景,不会panic。
  4. 若存在新的STOMP协议版本,需要补充文档至https://github.com/bytedance/g3/blob/master/doc/standards.md#stomp

示例代码结构(AI生成,仅供参考)

use super::*;
use crate::protocol::stomp::*;

#[test]
fn test_check_stomp_client_connect_request_valid() {
    // 准备符合 STOMP 协议的请求数据
    let data = b"CONNECT\n\n\0";
    let mut state = ProtocolInspectState::new();
    let result = state.check_stomp_client_connect_request(data);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), Some(Protocol::Stomp));
}

#[test]
fn test_check_stomp_client_connect_request_invalid() {
    // 准备不符合 STOMP 协议的请求数据
    let data = b"INVALID_REQUEST";
    let mut state = ProtocolInspectState::new();
    let result = state.check_stomp_client_connect_request(data);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), None);
}

#[test]
fn test_check_stomp_client_connect_request_need_more_data() {
    // 准备长度不足的请求数据
    let data = b"CO";
    let mut state = ProtocolInspectState::new();
    let result = state.check_stomp_client_connect_request(data);
    assert!(result.is_err());
    assert!(matches!(result.unwrap_err(), ProtocolInspectError::NeedMoreData(_)));
}

// 为 check_stomp_connect_method 和 check_stomp_stomp_method 编写类似的测试用例

注意事项

  • 确保测试用例的独立性,每个测试用例应该能够独立运行,不依赖于其他测试用例的状态。
  • 测试用例的命名应该清晰明了,能够准确反映测试的场景。
  • 可以根据需要添加更多的测试用例,以覆盖更多的边界情况。

提交要求

  • 提交的代码应遵循项目的代码风格和规范。
@zh-jq-b zh-jq-b added the good first issue Good for newcomers label Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant