rocketmq_remoting/protocol/header/
notification_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 cheetah_string::CheetahString;
18use rocketmq_macros::RequestHeaderCodecV2;
19use serde::Deserialize;
20use serde::Serialize;
21
22use crate::protocol::header::namesrv::topic_operation_header::TopicRequestHeader;
23
24#[derive(Debug, Serialize, Deserialize, RequestHeaderCodecV2)]
25pub struct NotificationRequestHeader {
26    #[serde(rename = "consumerGroup")]
27    #[required]
28    pub consumer_group: CheetahString,
29
30    #[serde(rename = "topic")]
31    #[required]
32    pub topic: CheetahString,
33
34    #[serde(rename = "queueId")]
35    #[required]
36    pub queue_id: i32,
37
38    #[serde(rename = "pollTime")]
39    #[required]
40    pub poll_time: i64,
41
42    #[serde(rename = "bornTime")]
43    #[required]
44    pub born_time: i64,
45
46    /// Indicates if the message is ordered; defaults to false.
47    #[serde(default)]
48    pub order: bool,
49
50    /// Attempt ID
51    #[serde(rename = "attemptId", skip_serializing_if = "Option::is_none")]
52    pub attempt_id: Option<CheetahString>,
53
54    #[serde(flatten)]
55    pub topic_request_header: Option<TopicRequestHeader>,
56}
57
58#[cfg(test)]
59mod tests {
60    use serde_json;
61
62    use super::*;
63
64    #[test]
65    fn test_notification_request_header_serialization() {
66        let header = NotificationRequestHeader {
67            consumer_group: CheetahString::from("consumer_group_1"),
68            topic: CheetahString::from("test_topic"),
69            queue_id: 10,
70            poll_time: 1234567890,
71            born_time: 1234567891,
72            order: true,
73            attempt_id: Some(CheetahString::from("attempt_1")),
74            topic_request_header: None,
75        };
76
77        let serialized = serde_json::to_string(&header).expect("Failed to serialize header");
78
79        let deserialized: NotificationRequestHeader =
80            serde_json::from_str(&serialized).expect("Failed to deserialize header");
81        assert_eq!(header.queue_id, deserialized.queue_id);
82    }
83
84    #[test]
85    fn test_notification_request_header_default_order() {
86        let header = NotificationRequestHeader {
87            consumer_group: CheetahString::from("consumer_group_1"),
88            topic: CheetahString::from("test_topic"),
89            queue_id: 10,
90            poll_time: 1234567890,
91            born_time: 1234567891,
92            order: false, // Defaults to false
93            attempt_id: None,
94            topic_request_header: None,
95        };
96
97        let serialized = serde_json::to_string(&header).expect("Failed to serialize header");
98
99        let deserialized: NotificationRequestHeader =
100            serde_json::from_str(&serialized).expect("Failed to deserialize header");
101        assert_eq!(header.queue_id, deserialized.queue_id);
102    }
103}