Skip to content

Commit

Permalink
[ISSUE #1566]🚀Add QueryConsumeTimeSpanRequestHeader struct🍻 (#1575)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm authored Dec 5, 2024
1 parent e99f8e0 commit 1731b70
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
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::protocol::header::namesrv::topic_operation_header::TopicRequestHeader;

#[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 topic_request_header: Option<TopicRequestHeader>,
}

#[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"),
topic_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.topic_request_header.is_none());
}

#[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.topic_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());
}
}

0 comments on commit 1731b70

Please sign in to comment.