Skip to content

Commit 5a5312a

Browse files
authored
[ISSUE #1305]🚀Add SetMessageRequestModeRequestBody (#1306)
1 parent e0fe4a7 commit 5a5312a

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

rocketmq-remoting/src/protocol/body.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub mod query_assignment_request_body;
3434
pub mod query_assignment_response_body;
3535
pub mod request;
3636
pub mod response;
37+
pub mod set_message_request_mode_request_body;
3738
pub mod topic;
3839
pub mod topic_info_wrapper;
3940
pub mod unlock_batch_request_body;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
*/
17+
use cheetah_string::CheetahString;
18+
use rocketmq_common::common::message::message_enum::MessageRequestMode;
19+
use serde::Deserialize;
20+
use serde::Serialize;
21+
22+
#[derive(Debug, Clone, Serialize, Deserialize)]
23+
#[serde(rename_all = "camelCase")]
24+
pub struct SetMessageRequestModeRequestBody {
25+
pub topic: CheetahString,
26+
pub consumer_group: CheetahString,
27+
pub mode: MessageRequestMode,
28+
/*
29+
consumer working in pop mode could share the MessageQueues assigned to
30+
the N (N = popShareQueueNum) consumers following it in the cid list
31+
*/
32+
pub pop_share_queue_num: i32,
33+
}
34+
35+
impl Default for SetMessageRequestModeRequestBody {
36+
fn default() -> Self {
37+
SetMessageRequestModeRequestBody {
38+
topic: CheetahString::new(),
39+
consumer_group: CheetahString::new(),
40+
mode: MessageRequestMode::Pull,
41+
pop_share_queue_num: 0,
42+
}
43+
}
44+
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use serde_json;
49+
50+
use super::*;
51+
52+
#[test]
53+
fn serialize_set_message_request_mode_request_body() {
54+
let body = SetMessageRequestModeRequestBody {
55+
topic: CheetahString::from("test_topic"),
56+
consumer_group: CheetahString::from("test_group"),
57+
mode: MessageRequestMode::Pop,
58+
pop_share_queue_num: 5,
59+
};
60+
let serialized = serde_json::to_string(&body).unwrap();
61+
assert!(serialized.contains("\"topic\":\"test_topic\""));
62+
assert!(serialized.contains("\"consumerGroup\":\"test_group\""));
63+
assert!(serialized.contains("\"mode\":\"POP\""));
64+
assert!(serialized.contains("\"popShareQueueNum\":5"));
65+
}
66+
67+
#[test]
68+
fn default_set_message_request_mode_request_body() {
69+
let default_body = SetMessageRequestModeRequestBody::default();
70+
assert_eq!(default_body.topic, CheetahString::new());
71+
assert_eq!(default_body.consumer_group, CheetahString::new());
72+
assert_eq!(default_body.mode, MessageRequestMode::Pull);
73+
assert_eq!(default_body.pop_share_queue_num, 0);
74+
}
75+
76+
#[test]
77+
fn deserialize_set_message_request_mode_request_body() {
78+
let json = r#"
79+
{
80+
"topic": "test_topic",
81+
"consumerGroup": "test_group",
82+
"mode": "PULL",
83+
"popShareQueueNum": 3
84+
}"#;
85+
let deserialized: SetMessageRequestModeRequestBody = serde_json::from_str(json).unwrap();
86+
assert_eq!(deserialized.topic, CheetahString::from("test_topic"));
87+
assert_eq!(
88+
deserialized.consumer_group,
89+
CheetahString::from("test_group")
90+
);
91+
assert_eq!(deserialized.mode, MessageRequestMode::Pull);
92+
assert_eq!(deserialized.pop_share_queue_num, 3);
93+
}
94+
95+
#[test]
96+
fn deserialize_set_message_request_mode_request_body_invalid_mode() {
97+
let json = r#"{
98+
"topic": "test_topic",
99+
"consumerGroup": "test_group",
100+
"mode": "INVALID",
101+
"popShareQueueNum": 3
102+
}"#;
103+
let deserialized: Result<SetMessageRequestModeRequestBody, _> = serde_json::from_str(json);
104+
assert!(deserialized.is_err());
105+
}
106+
}

0 commit comments

Comments
 (0)