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

[ISSUE #1566]🚀Add QueryConsumeTimeSpanRequestHeader struct🍻 #1575

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rocketmq-remoting/src/protocol/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub mod namesrv;
pub mod notify_consumer_ids_changed_request_header;
pub mod pull_message_request_header;
pub mod pull_message_response_header;
pub mod query_consume_time_span_request_header;
pub mod query_consumer_offset_request_header;
pub mod query_consumer_offset_response_header;
pub mod query_message_request_header;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use cheetah_string::CheetahString;
use rocketmq_macros::RequestHeaderCodec;
use serde::Deserialize;
use serde::Serialize;

use crate::rpc::rpc_request_header::RpcRequestHeader;

#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodec)]
#[serde(rename_all = "camelCase")]
pub struct QueryConsumeTimeSpanRequestHeader {
#[required]
pub topic: CheetahString,

#[required]
pub group: CheetahString,

#[serde(flatten)]
pub rpc_request_header: Option<RpcRequestHeader>,
}

#[cfg(test)]
mod tests {
use cheetah_string::CheetahString;

use super::*;

#[test]
fn query_consume_time_span_request_header_serializes_correctly() {
let header = QueryConsumeTimeSpanRequestHeader {
topic: CheetahString::from_static_str("test_topic"),
group: CheetahString::from_static_str("test_group"),
rpc_request_header: None,
};
let serialized = serde_json::to_string(&header).unwrap();
let expected = r#"{"topic":"test_topic","group":"test_group"}"#;
assert_eq!(serialized, expected);
}

#[test]
fn query_consume_time_span_request_header_deserializes_correctly() {
let data = r#"{"topic":"test_topic","group":"test_group"}"#;
let header: QueryConsumeTimeSpanRequestHeader = serde_json::from_str(data).unwrap();
assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
assert_eq!(header.group, CheetahString::from_static_str("test_group"));
assert!(!header.rpc_request_header.is_none());
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix incorrect test assertions

The assertions for rpc_request_header are incorrect. They're asserting that the optional header is present when it should be None.

Fix the assertions in both test methods:

-assert!(!header.rpc_request_header.is_none());
+assert!(header.rpc_request_header.is_none());

Also applies to: 70-70

}

#[test]
fn query_consume_time_span_request_header_handles_missing_optional_fields() {
let data = r#"{"topic":"test_topic","group":"test_group"}"#;
let header: QueryConsumeTimeSpanRequestHeader = serde_json::from_str(data).unwrap();
assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
assert_eq!(header.group, CheetahString::from_static_str("test_group"));
assert!(!header.rpc_request_header.is_none());
}

#[test]
fn query_consume_time_span_request_header_handles_invalid_data() {
let data = r#"{"topic":12345,"group":"test_group"}"#;
let result: Result<QueryConsumeTimeSpanRequestHeader, _> = serde_json::from_str(data);
assert!(result.is_err());
}
}
Loading