rocketmq_remoting/protocol/header/
change_invisible_time_request_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 std::fmt::Display;
18
19use cheetah_string::CheetahString;
20use rocketmq_macros::RequestHeaderCodecV2;
21use serde::Deserialize;
22use serde::Serialize;
23
24use crate::rpc::topic_request_header::TopicRequestHeader;
25
26#[derive(Serialize, Deserialize, Debug, Default, RequestHeaderCodecV2)]
27#[serde(rename_all = "camelCase")]
28pub struct ChangeInvisibleTimeRequestHeader {
29    #[required]
30    pub consumer_group: CheetahString,
31
32    #[required]
33    pub topic: CheetahString,
34
35    #[required]
36    pub queue_id: i32,
37
38    //startOffset popTime invisibleTime queueId
39    #[required]
40    pub extra_info: CheetahString,
41
42    #[required]
43    pub offset: i64,
44
45    #[required]
46    pub invisible_time: i64,
47    #[serde(flatten)]
48    pub topic_request_header: Option<TopicRequestHeader>,
49}
50
51impl Display for ChangeInvisibleTimeRequestHeader {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        write!(
54            f,
55            "ChangeInvisibleTimeRequestHeader {{ consumer_group: {}, topic: {}, queue_id: {}, \
56             extra_info: {}, offset: {}, invisible_time: {} }}",
57            self.consumer_group,
58            self.topic,
59            self.queue_id,
60            self.extra_info,
61            self.offset,
62            self.invisible_time
63        )
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use cheetah_string::CheetahString;
70    use serde_json;
71
72    use super::*;
73
74    #[test]
75    fn change_invisible_time_request_header_display_format() {
76        let header = ChangeInvisibleTimeRequestHeader {
77            consumer_group: CheetahString::from("group1"),
78            topic: CheetahString::from("topic1"),
79            queue_id: 1,
80            extra_info: CheetahString::from("info"),
81            offset: 12345,
82            invisible_time: 67890,
83            topic_request_header: None,
84        };
85        assert_eq!(
86            format!("{}", header),
87            "ChangeInvisibleTimeRequestHeader { consumer_group: group1, topic: topic1, queue_id: \
88             1, extra_info: info, offset: 12345, invisible_time: 67890 }"
89        );
90    }
91
92    #[test]
93    fn change_invisible_time_request_header_display_format_with_topic_request_header() {
94        let header = ChangeInvisibleTimeRequestHeader {
95            consumer_group: CheetahString::from("group1"),
96            topic: CheetahString::from("topic1"),
97            queue_id: 1,
98            extra_info: CheetahString::from("info"),
99            offset: 12345,
100            invisible_time: 67890,
101            topic_request_header: Some(TopicRequestHeader {
102                rpc_request_header: None,
103                lo: None,
104            }),
105        };
106        assert_eq!(
107            format!("{}", header),
108            "ChangeInvisibleTimeRequestHeader { consumer_group: group1, topic: topic1, queue_id: \
109             1, extra_info: info, offset: 12345, invisible_time: 67890 }"
110        );
111    }
112
113    #[test]
114    fn change_invisible_time_request_header_serialize() {
115        let header = ChangeInvisibleTimeRequestHeader {
116            consumer_group: CheetahString::from("group1"),
117            topic: CheetahString::from("topic1"),
118            queue_id: 1,
119            extra_info: CheetahString::from("info"),
120            offset: 12345,
121            invisible_time: 67890,
122            topic_request_header: None,
123        };
124        let serialized = serde_json::to_string(&header).unwrap();
125        assert_eq!(
126            serialized,
127            r#"{"consumerGroup":"group1","topic":"topic1","queueId":1,"extraInfo":"info","offset":12345,"invisibleTime":67890}"#
128        );
129    }
130
131    #[test]
132    fn change_invisible_time_request_header_deserialize() {
133        let json = r#"{"consumerGroup":"group1","topic":"topic1","queueId":1,"extraInfo":"info","offset":12345,"invisibleTime":67890}"#;
134        let header: ChangeInvisibleTimeRequestHeader = serde_json::from_str(json).unwrap();
135        assert_eq!(header.consumer_group, CheetahString::from("group1"));
136        assert_eq!(header.topic, CheetahString::from("topic1"));
137        assert_eq!(header.queue_id, 1);
138        assert_eq!(header.extra_info, CheetahString::from("info"));
139        assert_eq!(header.offset, 12345);
140        assert_eq!(header.invisible_time, 67890);
141    }
142}