Skip to main content

rocketmq_protocol/protocol/header/
polling_info_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;
16#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq, Default, RequestHeaderCodec)]
17#[serde(rename_all = "camelCase")]
18#[header(
19    type_id = "rocketmq_protocol::protocol::header::polling_info_response_header::PollingInfoResponseHeader",
20    java_class = "org.apache.rocketmq.remoting.protocol.header.PollingInfoResponseHeader"
21)]
22pub struct PollingInfoResponseHeader {
23    #[header(required)]
24    pub polling_num: i32,
25}
26
27#[cfg(test)]
28mod tests {
29    use std::collections::HashMap;
30
31    use super::*;
32    use crate::protocol::command_custom_header::{CommandCustomHeader, FromMap};
33
34    #[test]
35    fn serde_uses_the_java_field_name_and_requires_the_value() {
36        let header = PollingInfoResponseHeader { polling_num: -50 };
37        let json = serde_json::to_string(&header).unwrap();
38
39        assert_eq!(json, r#"{"pollingNum":-50}"#);
40        assert_eq!(
41            serde_json::from_str::<PollingInfoResponseHeader>(&json).unwrap(),
42            header
43        );
44        assert!(serde_json::from_str::<PollingInfoResponseHeader>("{}").is_err());
45        assert!(serde_json::from_str::<PollingInfoResponseHeader>(r#"{"pollingNum":"invalid"}"#).is_err());
46    }
47
48    #[test]
49    fn request_header_codec_round_trips_signed_boundaries_and_requires_the_value() {
50        for polling_num in [i32::MIN, 0, i32::MAX] {
51            let header = PollingInfoResponseHeader { polling_num };
52            let map = header.to_map().unwrap();
53            let decoded = <PollingInfoResponseHeader as FromMap>::from(&map).unwrap();
54
55            assert_eq!(decoded, header);
56        }
57
58        assert!(<PollingInfoResponseHeader as FromMap>::from(&HashMap::new()).is_err());
59    }
60}