Skip to main content

rocketmq_model/common/message/
message_id.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::net::SocketAddr;
16
17#[derive(Debug, Clone, PartialEq, Eq, Copy)]
18pub struct MessageId {
19    pub address: SocketAddr,
20    pub offset: i64,
21}
22
23#[cfg(test)]
24mod test {
25    use super::*;
26    use std::net::IpAddr;
27    use std::net::Ipv4Addr;
28    use std::net::SocketAddr;
29
30    #[test]
31    fn creates_message_id_correctly() {
32        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
33        let msg_id = MessageId {
34            address: addr,
35            offset: 1,
36        };
37
38        assert_eq!(msg_id.address, addr);
39        assert_eq!(msg_id.offset, 1);
40    }
41
42    #[test]
43    fn message_id_is_copy() {
44        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 1234);
45        let original = MessageId {
46            address: addr,
47            offset: 10,
48        };
49
50        let copied = original;
51
52        assert_eq!(original, copied);
53    }
54
55    #[test]
56    fn message_id_equality_works() {
57        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 9000);
58
59        let a = MessageId {
60            address: addr,
61            offset: 1,
62        };
63
64        let b = MessageId {
65            address: addr,
66            offset: 1,
67        };
68
69        let c = MessageId {
70            address: addr,
71            offset: 2,
72        };
73
74        assert_eq!(a, b);
75        assert_ne!(a, c);
76    }
77}