Skip to main content

rocketmq_client_rust/consumer/
pop_result.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 std::fmt::Display;
16
17use rocketmq_model::common::message::message_ext::MessageExt;
18
19use crate::consumer::pop_status::PopStatus;
20
21#[derive(Default, Clone)]
22pub struct PopResult {
23    pub msg_found_list: Option<Vec<MessageExt>>,
24    pub pop_status: PopStatus,
25    pub pop_time: u64,
26    pub invisible_time: u64,
27    pub rest_num: u64,
28}
29
30impl Display for PopResult {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        write!(
33            f,
34            "PopResult [popStatus={},msgFoundList={},restNum={}]",
35            self.pop_status,
36            self.msg_found_list.as_ref().map_or(0, |value| value.len()),
37            self.rest_num
38        )
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use rocketmq_model::common::message::message_ext::MessageExt;
45
46    use super::*;
47    use crate::consumer::pop_status::PopStatus;
48
49    fn create_message_ext() -> MessageExt {
50        MessageExt::default()
51    }
52
53    #[test]
54    fn display_pop_result_with_empty_msg_list() {
55        let pop_result = PopResult {
56            msg_found_list: Some(vec![]),
57            pop_status: PopStatus::Found,
58            pop_time: 123456789,
59            invisible_time: 1000,
60            rest_num: 10,
61        };
62        assert_eq!(
63            format!("{}", pop_result),
64            "PopResult [popStatus=FOUND,msgFoundList=0,restNum=10]"
65        );
66    }
67
68    #[test]
69    fn display_pop_result_with_non_empty_msg_list() {
70        let pop_result = PopResult {
71            msg_found_list: Some(vec![create_message_ext(), create_message_ext()]),
72            pop_status: PopStatus::NoNewMsg,
73            pop_time: 987654321,
74            invisible_time: 2000,
75            rest_num: 5,
76        };
77        assert_eq!(
78            format!("{}", pop_result),
79            "PopResult [popStatus=NO_NEW_MSG,msgFoundList=2,restNum=5]"
80        );
81    }
82
83    #[test]
84    fn display_pop_result_with_polling_full_status() {
85        let pop_result = PopResult {
86            msg_found_list: Some(vec![create_message_ext()]),
87            pop_status: PopStatus::PollingFull,
88            pop_time: 111111111,
89            invisible_time: 3000,
90            rest_num: 0,
91        };
92        assert_eq!(
93            format!("{}", pop_result),
94            "PopResult [popStatus=POLLING_FULL,msgFoundList=1,restNum=0]"
95        );
96    }
97
98    #[test]
99    fn display_pop_result_with_polling_not_found_status() {
100        let pop_result = PopResult {
101            msg_found_list: Some(vec![]),
102            pop_status: PopStatus::PollingNotFound,
103            pop_time: 222222222,
104            invisible_time: 4000,
105            rest_num: 20,
106        };
107        assert_eq!(
108            format!("{}", pop_result),
109            "PopResult [popStatus=POLLING_NOT_FOUND,msgFoundList=0,restNum=20]"
110        );
111    }
112
113    #[test]
114    fn display_pop_result_with_none_msg_list_counts_zero_like_java_null_list() {
115        let pop_result = PopResult {
116            msg_found_list: None,
117            pop_status: PopStatus::Found,
118            pop_time: 0,
119            invisible_time: 0,
120            rest_num: 3,
121        };
122
123        assert_eq!(
124            format!("{}", pop_result),
125            "PopResult [popStatus=FOUND,msgFoundList=0,restNum=3]"
126        );
127    }
128}