Skip to main content

rocketmq_protocol/protocol/header/
elect_master_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 cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodec)]
21#[serde(rename_all = "camelCase")]
22#[header(
23    type_id = "rocketmq_protocol::protocol::header::elect_master_response_header::ElectMasterResponseHeader",
24    java_class = "org.apache.rocketmq.remoting.protocol.header.controller.ElectMasterResponseHeader"
25)]
26pub struct ElectMasterResponseHeader {
27    pub master_broker_id: Option<i64>,
28    pub master_address: Option<CheetahString>,
29    pub master_epoch: Option<i32>,
30    pub sync_state_set_epoch: Option<i32>,
31}
32
33#[cfg(test)]
34impl ElectMasterResponseHeader {
35    const MASTER_BROKER_ID: &'static str = "masterBrokerId";
36    const MASTER_ADDRESS: &'static str = "masterAddress";
37    const MASTER_EPOCH: &'static str = "masterEpoch";
38    const SYNC_STATE_SET_EPOCH: &'static str = "syncStateSetEpoch";
39}
40
41#[cfg(test)]
42mod tests {
43    use std::collections::HashMap;
44
45    use super::*;
46    use crate::protocol::command_custom_header::CommandCustomHeader;
47    use crate::protocol::command_custom_header::FromMap;
48
49    #[test]
50    fn elect_master_response_header_serializes_correctly() {
51        let header = ElectMasterResponseHeader {
52            master_broker_id: Some(1),
53            master_address: Some(CheetahString::from_static_str("test_address")),
54            master_epoch: Some(2),
55            sync_state_set_epoch: Some(3),
56        };
57        let map = header.to_map().unwrap();
58        assert_eq!(
59            map.get(&CheetahString::from_static_str(
60                ElectMasterResponseHeader::MASTER_BROKER_ID
61            ))
62            .unwrap(),
63            "1"
64        );
65        assert_eq!(
66            map.get(&CheetahString::from_static_str(
67                ElectMasterResponseHeader::MASTER_ADDRESS
68            ))
69            .unwrap(),
70            "test_address"
71        );
72        assert_eq!(
73            map.get(&CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_EPOCH))
74                .unwrap(),
75            "2"
76        );
77        assert_eq!(
78            map.get(&CheetahString::from_static_str(
79                ElectMasterResponseHeader::SYNC_STATE_SET_EPOCH
80            ))
81            .unwrap(),
82            "3"
83        );
84    }
85
86    #[test]
87
88    fn elect_master_response_header_deserializes_correctly() {
89        let mut map = HashMap::new();
90        map.insert(
91            CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_BROKER_ID),
92            CheetahString::from("1"),
93        );
94        map.insert(
95            CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_ADDRESS),
96            CheetahString::from("test_address"),
97        );
98        map.insert(
99            CheetahString::from_static_str(ElectMasterResponseHeader::MASTER_EPOCH),
100            CheetahString::from("2"),
101        );
102        map.insert(
103            CheetahString::from_static_str(ElectMasterResponseHeader::SYNC_STATE_SET_EPOCH),
104            CheetahString::from("3"),
105        );
106        let header = <ElectMasterResponseHeader as FromMap>::from(&map).unwrap();
107        assert_eq!(header.master_broker_id, Some(1));
108        assert_eq!(
109            header.master_address,
110            Some(CheetahString::from_static_str("test_address"))
111        );
112        assert_eq!(header.master_epoch, Some(2));
113        assert_eq!(header.sync_state_set_epoch, Some(3));
114    }
115
116    #[test]
117    fn elect_master_response_header_handles_missing_optional_fields() {
118        let map = HashMap::new();
119        let header = <ElectMasterResponseHeader as FromMap>::from(&map).unwrap();
120        assert!(header.master_broker_id.is_none());
121        assert!(header.master_address.is_none());
122        assert!(header.master_epoch.is_none());
123        assert!(header.sync_state_set_epoch.is_none());
124    }
125}