gecko-dev/toolkit/components/glean/api/tests/string.rs
Jan-Erik Rediger 21cb4da42f Bug 1632150 - Implement the labeled boolean metric type for Project FOG. r=chutten,Dexter
This turned out to be a bit more tricky than anticipated:

* We want users to see the type this crate exposes, so we need to wrap
  it all.
* We don't want to deal with validating labels here, so we need to
  delegate.
* We need it to be thread-safe, so that we can generate globals.
  But we also don't want users to deal with locking.
* We want a nice-ish API.

This now achieves all of this with just one little trick: a sealed
trait.
That way we can easily extend it to more types, without adding much more
code.

Note: It might have been nice to use the index operator (`[]`), but
that's defined by a trait in Rust and returns a reference to existing
data.
We thus can't use it and instead provide a simple `get()` method.

Differential Revision: https://phabricator.services.mozilla.com/D72805
2020-04-29 08:14:11 +00:00

31 lines
862 B
Rust

// 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 common::*;
use glean::metrics::{CommonMetricData, Lifetime, StringMetric};
#[test]
fn sets_string_value() {
let _lock = lock_test();
let _t = setup_glean(None);
let store_names: Vec<String> = vec!["store1".into()];
let metric = StringMetric::new(CommonMetricData {
name: "string_metric".into(),
category: "telemetry".into(),
send_in_pings: store_names.clone(),
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
});
metric.set("test_string_value");
assert_eq!(
"test_string_value",
metric.test_get_value("store1").unwrap()
);
}