Skip to main content

rocketmq_protocol/protocol/header/
query_message_response_header.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use rocketmq_macros::RequestHeaderCodec;
16use serde::Deserialize;
17use serde::Serialize;
18
19#[derive(Debug, Clone, Serialize, Deserialize, RequestHeaderCodec, Default, PartialEq)]
20#[serde(rename_all = "camelCase")]
21#[header(
22    type_id = "rocketmq_protocol::protocol::header::query_message_response_header::QueryMessageResponseHeader",
23    java_class = "org.apache.rocketmq.remoting.protocol.header.QueryMessageResponseHeader"
24)]
25pub struct QueryMessageResponseHeader {
26    #[header(required)]
27    pub index_last_update_timestamp: i64,
28    #[header(required)]
29    pub index_last_update_phyoffset: i64,
30}
31
32#[cfg(test)]
33mod tests {
34    use std::collections::HashMap;
35
36    use super::*;
37    use crate::protocol::command_custom_header::{CommandCustomHeader, FromMap};
38    use crate::protocol::remoting_command::RemotingCommand;
39
40    #[test]
41    fn serde_uses_java_field_names_and_requires_both_values() {
42        let header = QueryMessageResponseHeader {
43            index_last_update_timestamp: 131_415,
44            index_last_update_phyoffset: 131_496,
45        };
46        let json = serde_json::to_string(&header).unwrap();
47
48        assert_eq!(
49            json,
50            r#"{"indexLastUpdateTimestamp":131415,"indexLastUpdatePhyoffset":131496}"#
51        );
52        assert_eq!(
53            serde_json::from_str::<QueryMessageResponseHeader>(&json).unwrap(),
54            header
55        );
56        assert!(serde_json::from_str::<QueryMessageResponseHeader>("{}").is_err());
57        assert!(serde_json::from_str::<QueryMessageResponseHeader>(
58            r#"{"indexLastUpdateTimestamp":1,"indexLastUpdatePhyoffset":"invalid"}"#
59        )
60        .is_err());
61    }
62
63    #[test]
64    fn request_header_codec_round_trips_signed_boundaries_and_requires_both_values() {
65        let header = QueryMessageResponseHeader {
66            index_last_update_timestamp: i64::MIN,
67            index_last_update_phyoffset: i64::MAX,
68        };
69        let map = header.to_map().unwrap();
70        let decoded = <QueryMessageResponseHeader as FromMap>::from(&map).unwrap();
71
72        assert_eq!(decoded, header);
73        assert!(<QueryMessageResponseHeader as FromMap>::from(&HashMap::new()).is_err());
74    }
75
76    #[test]
77    fn remoting_command_decodes_the_same_header_with_both_codec_paths() {
78        let header = QueryMessageResponseHeader {
79            index_last_update_timestamp: 123_456_789,
80            index_last_update_phyoffset: 987_654_321,
81        };
82        let mut command = RemotingCommand::create_success_response_command_with_header(header);
83        command.make_custom_header_to_net();
84
85        let normal: QueryMessageResponseHeader = command.decode_command_custom_header().unwrap();
86        let fast: QueryMessageResponseHeader = command.decode_command_custom_header_fast().unwrap();
87
88        assert_eq!(normal, fast);
89        assert_eq!(normal.index_last_update_timestamp, 123_456_789);
90        assert_eq!(normal.index_last_update_phyoffset, 987_654_321);
91    }
92}