Skip to main content

rocketmq_protocol/protocol/
heartbeat_facade.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
9use std::time::SystemTime;
10use std::time::UNIX_EPOCH;
11
12use crate::protocol::heartbeat::subscription_data::SubscriptionData;
13
14pub fn default_subscription_data() -> SubscriptionData {
15    SubscriptionData {
16        sub_version: current_millis(),
17        ..SubscriptionData::default()
18    }
19}
20
21fn current_millis() -> i64 {
22    SystemTime::now()
23        .duration_since(UNIX_EPOCH)
24        .map(|duration| duration.as_millis() as i64)
25        .unwrap_or(0)
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    fn now_millis() -> i64 {
33        SystemTime::now()
34            .duration_since(UNIX_EPOCH)
35            .expect("system time should be later than the unix epoch")
36            .as_millis() as i64
37    }
38
39    #[test]
40    fn default_subscription_data_uses_wall_clock_sub_version() {
41        let before = now_millis();
42        let data = default_subscription_data();
43
44        assert!(data.sub_version > 0);
45        assert!(data.sub_version >= before);
46    }
47
48    #[test]
49    fn default_subscription_data_keeps_remaining_defaults() {
50        let data = default_subscription_data();
51        let expected = SubscriptionData::default();
52
53        assert_eq!(data.class_filter_mode, expected.class_filter_mode);
54        assert_eq!(data.topic, expected.topic);
55        assert_eq!(data.sub_string, expected.sub_string);
56        assert_eq!(data.tags_set, expected.tags_set);
57        assert_eq!(data.code_set, expected.code_set);
58        assert_eq!(data.expression_type, expected.expression_type);
59        assert_eq!(data.filter_class_source, expected.filter_class_source);
60    }
61}