summaryrefslogtreecommitdiffstats
path: root/third_party/rust/glean-core/tests/rate.rs
blob: f81e10cb532b88dd78a9688f424acac5c40e8897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

mod common;
use crate::common::*;

use glean_core::metrics::*;
use glean_core::CommonMetricData;
use glean_core::{test_get_num_recorded_errors, ErrorType};

#[test]
fn rate_smoke() {
    let (glean, _t) = new_glean(None);

    let metric: RateMetric = RateMetric::new(CommonMetricData {
        name: "rate".into(),
        category: "test".into(),
        send_in_pings: vec!["test1".into()],
        ..Default::default()
    });

    // Adding 0 doesn't error.
    metric.add_to_numerator_sync(&glean, 0);
    metric.add_to_denominator_sync(&glean, 0);

    assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err());

    // Adding a negative value errors.
    metric.add_to_numerator_sync(&glean, -1);
    metric.add_to_denominator_sync(&glean, -1);

    assert_eq!(
        Ok(2),
        test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue),
    );

    // Getting the value returns 0s if that's all we have.
    assert_eq!(metric.get_value(&glean, None), Some((0, 0).into()));

    // And normal values of course work.
    metric.add_to_numerator_sync(&glean, 22);
    metric.add_to_denominator_sync(&glean, 7);

    assert_eq!(metric.get_value(&glean, None), Some((22, 7).into()));
}

#[test]
fn numerator_smoke() {
    let (glean, _t) = new_glean(None);

    let metric: NumeratorMetric = NumeratorMetric::new(CommonMetricData {
        name: "rate".into(),
        category: "test".into(),
        send_in_pings: vec!["test1".into()],
        ..Default::default()
    });

    // Adding 0 doesn't error.
    metric.add_to_numerator_sync(&glean, 0);

    assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err());

    // Adding a negative value errors.
    metric.add_to_numerator_sync(&glean, -1);

    assert_eq!(
        Ok(1),
        test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue),
    );

    // Getting the value returns 0s if that's all we have.
    let data = metric.get_value(&glean, None).unwrap();
    assert_eq!(0, data.numerator);
    assert_eq!(0, data.denominator);

    // And normal values of course work.
    metric.add_to_numerator_sync(&glean, 22);

    let data = metric.get_value(&glean, None).unwrap();
    assert_eq!(22, data.numerator);
    assert_eq!(0, data.denominator);
}

#[test]
fn denominator_smoke() {
    let (glean, _t) = new_glean(None);

    let meta1 = CommonMetricData {
        name: "rate1".into(),
        category: "test".into(),
        send_in_pings: vec!["test1".into()],
        ..Default::default()
    };

    let meta2 = CommonMetricData {
        name: "rate2".into(),
        category: "test".into(),
        send_in_pings: vec!["test1".into()],
        ..Default::default()
    };

    // This acts like a normal counter.
    let denom: DenominatorMetric = DenominatorMetric::new(
        CommonMetricData {
            name: "counter".into(),
            category: "test".into(),
            send_in_pings: vec!["test1".into()],
            ..Default::default()
        },
        vec![meta1.clone(), meta2.clone()],
    );

    let num1 = NumeratorMetric::new(meta1);
    let num2 = NumeratorMetric::new(meta2);

    num1.add_to_numerator_sync(&glean, 3);
    num2.add_to_numerator_sync(&glean, 5);

    denom.add_sync(&glean, 7);

    // no errors.
    assert!(test_get_num_recorded_errors(&glean, num1.meta(), ErrorType::InvalidValue).is_err());
    assert!(test_get_num_recorded_errors(&glean, num2.meta(), ErrorType::InvalidValue).is_err());

    // Getting the value returns 0s if that's all we have.
    let data = num1.get_value(&glean, None).unwrap();
    assert_eq!(3, data.numerator);
    assert_eq!(7, data.denominator);

    let data = num2.get_value(&glean, None).unwrap();
    assert_eq!(5, data.numerator);
    assert_eq!(7, data.denominator);
}