Skip to content

Commit

Permalink
[ISSUE #1565]🍻Add GetTopicStatsInfoRequestHeader struct 💡
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm authored Dec 5, 2024
1 parent 1731b70 commit be5dc61
Show file tree
Hide file tree
Showing 2 changed files with 74 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 @@ -35,6 +35,7 @@ pub mod get_max_offset_response_header;
pub mod get_min_offset_request_header;
pub mod get_min_offset_response_header;
pub mod get_topic_config_request_header;
pub mod get_topic_stats_info_request_header;
pub mod get_topic_stats_request_header;
pub mod heartbeat_request_header;
pub mod lock_batch_mq_request_header;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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,

#[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"),
topic_request_header: None,
};
let serialized = serde_json::to_string(&header).unwrap();
let expected = r#"{"topic":"test_topic"}"#;
assert_eq!(serialized, expected);
}

#[test]
fn query_consume_time_span_request_header_deserializes_correctly() {
let data = r#"{"topic":"test_topic"}"#;
let header: QueryConsumeTimeSpanRequestHeader = serde_json::from_str(data).unwrap();
assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
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"}"#;
let header: QueryConsumeTimeSpanRequestHeader = serde_json::from_str(data).unwrap();
assert_eq!(header.topic, CheetahString::from_static_str("test_topic"));
assert!(!header.topic_request_header.is_none());
}

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

0 comments on commit be5dc61

Please # to comment.