rocketmq_remoting/protocol/header/
elect_master_response_header.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use cheetah_string::CheetahString;
18use rocketmq_macros::RequestHeaderCodecV2;
19use serde::Deserialize;
20use serde::Serialize;
21
22#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
23#[serde(rename_all = "camelCase")]
24pub struct ElectMasterResponseHeader {
25    pub master_broker_id: Option<i64>,
26    pub master_address: Option<CheetahString>,
27    pub master_epoch: Option<i32>,
28    pub sync_state_set_epoch: Option<i32>,
29}
30
31#[cfg(test)]
32mod tests {
33    use std::collections::HashMap;
34
35    use super::*;
36    use crate::protocol::command_custom_header::CommandCustomHeader;
37    use crate::protocol::command_custom_header::FromMap;
38
39    #[test]
40    fn elect_master_response_header_serializes_correctly() {
41        let header = ElectMasterResponseHeader {
42            master_broker_id: Some(1),
43            master_address: Some(CheetahString::from_static_str("test_address")),
44            master_epoch: Some(2),
45            sync_state_set_epoch: Some(3),
46        };
47        let map = header.to_map().unwrap();
48        assert_eq!(
49            map.get(&CheetahString::from_static_str(
50                ElectMasterResponseHeader::MASTER_BROKER_ID
51            ))
52            .unwrap(),
53            "1"
54        );
55        assert_eq!(
56            map.get(&CheetahString::from_static_str(
57                ElectMasterResponseHeader::MASTER_ADDRESS
58            ))
59            .unwrap(),
60            "test_address"
61        );
62        assert_eq!(
63            map.get(&CheetahString::from_static_str(
64                ElectMasterResponseHeader::MASTER_EPOCH
65            ))
66            .unwrap(),
67            "2"
68        );
69        assert_eq!(
70            map.get(&CheetahString::from_static_str(
71                ElectMasterResponseHeader::SYNC_STATE_SET_EPOCH
72            ))
73            .unwrap(),
74            "3"
75        );
76    }
77
78    #[test]
79
80    fn elect_master_response_header_deserializes_correctly() {
81        let mut map = HashMap::new();
82        map.insert(
83            CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_BROKER_ID),
84            CheetahString::from("1"),
85        );
86        map.insert(
87            CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_ADDRESS),
88            CheetahString::from("test_address"),
89        );
90        map.insert(
91            CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_EPOCH),
92            CheetahString::from("2"),
93        );
94        map.insert(
95            CheetahString::from_static_str(ElectMasterResponseHeader::SYNC_STATE_SET_EPOCH),
96            CheetahString::from("3"),
97        );
98        let header = <ElectMasterResponseHeader as FromMap>::from(&map).unwrap();
99        assert_eq!(header.master_broker_id, Some(1));
100        assert_eq!(
101            header.master_address,
102            Some(CheetahString::from_static_str("test_address"))
103        );
104        assert_eq!(header.master_epoch, Some(2));
105        assert_eq!(header.sync_state_set_epoch, Some(3));
106    }
107
108    #[test]
109    fn elect_master_response_header_handles_missing_optional_fields() {
110        let map = HashMap::new();
111        let header = <ElectMasterResponseHeader as FromMap>::from(&map).unwrap();
112        assert!(header.master_broker_id.is_none());
113        assert!(header.master_address.is_none());
114        assert!(header.master_epoch.is_none());
115        assert!(header.sync_state_set_epoch.is_none());
116    }
117}