summaryrefslogtreecommitdiffstats
path: root/third_party/rust/glean-core/tests/string_list.rs
blob: 7dd5acfbb52dd08374b0184aa5ee9b0b45dfc35f (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// 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 serde_json::json;

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

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

    let list: StringListMetric = StringListMetric::new(CommonMetricData {
        name: "list".into(),
        category: "local".into(),
        send_in_pings: vec!["core".into()],
        ..Default::default()
    });

    list.add_sync(&glean, "first");
    assert_eq!(list.get_value(&glean, "core").unwrap(), vec!["first"]);

    list.add_sync(&glean, "second");
    assert_eq!(
        list.get_value(&glean, "core").unwrap(),
        vec!["first", "second"]
    );

    list.set_sync(&glean, vec!["third".into()]);
    assert_eq!(list.get_value(&glean, "core").unwrap(), vec!["third"]);

    list.add_sync(&glean, "fourth");
    assert_eq!(
        list.get_value(&glean, "core").unwrap(),
        vec!["third", "fourth"]
    );
}

#[test]
fn stringlist_serializer_should_correctly_serialize_stringlists() {
    let (mut tempdir, _) = tempdir();

    {
        // We give tempdir to the `new_glean` function...
        let (glean, dir) = new_glean(Some(tempdir));
        // And then we get it back once that function returns.
        tempdir = dir;

        let metric = StringListMetric::new(CommonMetricData {
            name: "string_list_metric".into(),
            category: "telemetry.test".into(),
            send_in_pings: vec!["store1".into()],
            disabled: false,
            lifetime: Lifetime::User,
            ..Default::default()
        });
        metric.set_sync(&glean, vec!["test_string_1".into(), "test_string_2".into()]);
    }

    {
        let (glean, _t) = new_glean(Some(tempdir));

        let snapshot = StorageManager
            .snapshot_as_json(glean.storage(), "store1", true)
            .unwrap();
        assert_eq!(
            json!({"string_list": {"telemetry.test.string_list_metric": ["test_string_1", "test_string_2"]}}),
            snapshot
        );
    }
}

#[test]
fn set_properly_sets_the_value_in_all_stores() {
    let (glean, _t) = new_glean(None);
    let store_names: Vec<String> = vec!["store1".into(), "store2".into()];

    let metric = StringListMetric::new(CommonMetricData {
        name: "string_list_metric".into(),
        category: "telemetry.test".into(),
        send_in_pings: store_names.clone(),
        disabled: false,
        lifetime: Lifetime::Ping,
        ..Default::default()
    });

    metric.set_sync(&glean, vec!["test_string_1".into(), "test_string_2".into()]);

    for store_name in store_names {
        let snapshot = StorageManager
            .snapshot_as_json(glean.storage(), &store_name, true)
            .unwrap();

        assert_eq!(
            json!({"string_list": {"telemetry.test.string_list_metric": ["test_string_1", "test_string_2"]}}),
            snapshot
        );
    }
}

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

    let metric = StringListMetric::new(CommonMetricData {
        name: "string_list_metric".into(),
        category: "telemetry.test".into(),
        send_in_pings: vec!["store1".into()],
        disabled: false,
        lifetime: Lifetime::Ping,
        ..Default::default()
    });

    let test_string = "0123456789".repeat(20);
    metric.add_sync(&glean, test_string.clone());

    // Ensure the string was truncated to the proper length.
    assert_eq!(
        vec![test_string[..100].to_string()],
        metric.get_value(&glean, "store1").unwrap()
    );

    // Ensure the error has been recorded.
    assert_eq!(
        Ok(1),
        test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidOverflow)
    );

    metric.set_sync(&glean, vec![test_string.clone()]);

    // Ensure the string was truncated to the proper length.
    assert_eq!(
        vec![test_string[..100].to_string()],
        metric.get_value(&glean, "store1").unwrap()
    );

    // Ensure the error has been recorded.
    assert_eq!(
        Ok(2),
        test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidOverflow)
    );
}

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

    let metric = StringListMetric::new(CommonMetricData {
        name: "string_list_metric".into(),
        category: "telemetry.test".into(),
        send_in_pings: vec!["store1".into()],
        disabled: true,
        lifetime: Lifetime::Ping,
        ..Default::default()
    });

    metric.add_sync(&glean, "test_string".repeat(20));

    // Ensure the string was not added.
    assert_eq!(None, metric.get_value(&glean, "store1"));

    metric.set_sync(&glean, vec!["test_string_2".repeat(20)]);

    // Ensure the stringlist was not set.
    assert_eq!(None, metric.get_value(&glean, "store1"));

    // Ensure no error was recorded.
    assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err());
}

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

    let metric = StringListMetric::new(CommonMetricData {
        name: "string_list_metric".into(),
        category: "telemetry.test".into(),
        send_in_pings: vec!["store1".into()],
        disabled: false,
        lifetime: Lifetime::Ping,
        ..Default::default()
    });

    for _n in 1..101 {
        metric.add_sync(&glean, "test_string");
    }

    let expected: Vec<String> = "test_string "
        .repeat(100)
        .split_whitespace()
        .map(|s| s.to_string())
        .collect();
    assert_eq!(expected, metric.get_value(&glean, "store1").unwrap());

    // Ensure the 101st string wasn't added.
    metric.add_sync(&glean, "test_string");
    assert_eq!(expected, metric.get_value(&glean, "store1").unwrap());

    // Ensure we recorded the error.
    assert_eq!(
        Ok(1),
        test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue)
    );

    // Try to set it to a list that's too long. Ensure it cuts off at 100 elements.
    let too_many: Vec<String> = "test_string "
        .repeat(101)
        .split_whitespace()
        .map(|s| s.to_string())
        .collect();
    metric.set_sync(&glean, too_many);
    assert_eq!(expected, metric.get_value(&glean, "store1").unwrap());

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

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

    let metric = StringListMetric::new(CommonMetricData {
        name: "string_list_metric".into(),
        category: "telemetry.test".into(),
        send_in_pings: vec!["store1".into()],
        disabled: false,
        lifetime: Lifetime::Ping,
        ..Default::default()
    });

    metric.set_sync(&glean, vec![]);

    // Ensure the empty list was added
    assert_eq!(Some(vec![]), metric.get_value(&glean, "store1"));

    // Ensure we didn't record an error.
    assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err());
}