From 2d82fbed923bc2ddfd4cb88387cf1ed22adc926f Mon Sep 17 00:00:00 2001 From: James Hugman Date: Tue, 7 Nov 2023 19:57:12 +0000 Subject: [PATCH] Update following rebase --- components/nimbus/src/tests/helpers.rs | 80 +++++++++++++------ .../nimbus/src/tests/stateful/helpers.rs | 71 ---------------- .../src/tests/stateless/test_cirrus_client.rs | 3 +- 3 files changed, 57 insertions(+), 97 deletions(-) diff --git a/components/nimbus/src/tests/helpers.rs b/components/nimbus/src/tests/helpers.rs index 7ef2b1bd91..b0cc7b8f6b 100644 --- a/components/nimbus/src/tests/helpers.rs +++ b/components/nimbus/src/tests/helpers.rs @@ -8,9 +8,13 @@ use crate::{ AppContext, EnrollmentStatus, Experiment, FeatureConfig, NimbusTargetingHelper, TargetingAttributes, }; + +#[cfg(feature = "stateful")] +use crate::metrics::{FeatureExposureExtraDef, MalformedFeatureConfigExtraDef}; + use serde::Serialize; use serde_json::{json, Value}; -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use std::sync::{Arc, Mutex}; cfg_if::cfg_if! { @@ -47,13 +51,24 @@ impl Default for NimbusTargetingHelper { } } +#[derive(Default)] +struct MetricState { + enrollment_statuses: Vec, + #[cfg(feature = "stateful")] + activations: Vec, + #[cfg(feature = "stateful")] + exposures: Vec, + #[cfg(feature = "stateful")] + malformeds: Vec, +} + /// A Rust implementation of the MetricsHandler trait /// Used to test recording of Glean metrics across the FFI within Rust /// /// *NOTE: Use this struct's `new` method when instantiating it to lock the Glean store* #[derive(Clone)] pub struct TestMetrics { - state: Arc>>, + state: Arc>, } impl TestMetrics { @@ -63,34 +78,51 @@ impl TestMetrics { } } - pub fn assert_get_vec_value(&self, key: &str) -> serde_json::Value { - self.state.lock().unwrap().get(key).unwrap().clone() + pub fn get_enrollment_statuses(&self) -> Vec { + self.state.lock().unwrap().enrollment_statuses.clone() + } +} + +#[cfg(feature = "stateful")] +impl TestMetrics { + pub fn clear(&self) { + let mut state = self.state.lock().unwrap(); + state.activations.clear(); + state.enrollment_statuses.clear(); + state.malformeds.clear(); + } + + pub fn get_activations(&self) -> Vec { + self.state.lock().unwrap().activations.clone() + } + + pub fn get_malformeds(&self) -> Vec { + self.state.lock().unwrap().malformeds.clone() } } impl MetricsHandler for TestMetrics { - /// In actual implementations of the MetricsHandler trait, this method would record the - /// supplied EnrollmentStatusExtraDefs into Glean. - /// - /// This implementation is explicitly used for testing, and does the following: - /// 1. It locks the TestMetrics instance's state - /// 2. It looks up the key for `enrollment_status` in the state, extends it if it already - /// exists and inserts it if it does not exist. - /// - /// This then allows for us to use the `assert_get_vec_value` method above in tests to fetch the - /// list of metrics that have been recorded during a given test. fn record_enrollment_statuses(&self, enrollment_status_extras: Vec) { - let key = "enrollment_status".to_string(); let mut state = self.state.lock().unwrap(); - let new = serde_json::to_value(enrollment_status_extras).unwrap(); - state - .entry(key) - .and_modify(|v| { - v.as_array_mut() - .unwrap() - .extend(new.as_array().unwrap().iter().cloned()); - }) - .or_insert(new); + state.enrollment_statuses.extend(enrollment_status_extras); + } + + #[cfg(feature = "stateful")] + fn record_feature_activation(&self, event: FeatureExposureExtraDef) { + let mut state = self.state.lock().unwrap(); + state.activations.push(event); + } + + #[cfg(feature = "stateful")] + fn record_feature_exposure(&self, event: FeatureExposureExtraDef) { + let mut state = self.state.lock().unwrap(); + state.exposures.push(event); + } + + #[cfg(feature = "stateful")] + fn record_malformed_feature_config(&self, event: MalformedFeatureConfigExtraDef) { + let mut state = self.state.lock().unwrap(); + state.malformeds.push(event); } } diff --git a/components/nimbus/src/tests/stateful/helpers.rs b/components/nimbus/src/tests/stateful/helpers.rs index 09e62c4e36..7137d02bf2 100644 --- a/components/nimbus/src/tests/stateful/helpers.rs +++ b/components/nimbus/src/tests/stateful/helpers.rs @@ -1,74 +1,3 @@ /* 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/. */ -use crate::metrics::{ - EnrollmentStatusExtraDef, FeatureExposureExtraDef, MalformedFeatureConfigExtraDef, - MetricsHandler, -}; -use std::sync::{Arc, Mutex}; - -#[derive(Default)] -struct MetricState { - enrollment_statuses: Vec, - activations: Vec, - exposures: Vec, - malformeds: Vec, -} - -/// A Rust implementation of the MetricsHandler trait -/// Used to test recording of Glean metrics across the FFI within Rust -/// -/// *NOTE: Use this struct's `new` method when instantiating it to lock the Glean store* -#[derive(Clone)] -pub struct TestMetrics { - state: Arc>, -} - -impl TestMetrics { - pub fn new() -> Self { - TestMetrics { - state: Default::default(), - } - } - - pub fn clear(&self) { - let mut state = self.state.lock().unwrap(); - state.activations.clear(); - state.enrollment_statuses.clear(); - state.malformeds.clear(); - } - - pub fn get_enrollment_statuses(&self) -> Vec { - self.state.lock().unwrap().enrollment_statuses.clone() - } - - pub fn get_activations(&self) -> Vec { - self.state.lock().unwrap().activations.clone() - } - - pub fn get_malformeds(&self) -> Vec { - self.state.lock().unwrap().malformeds.clone() - } -} - -impl MetricsHandler for TestMetrics { - fn record_enrollment_statuses(&self, enrollment_status_extras: Vec) { - let mut state = self.state.lock().unwrap(); - state.enrollment_statuses.extend(enrollment_status_extras); - } - - fn record_feature_activation(&self, event: FeatureExposureExtraDef) { - let mut state = self.state.lock().unwrap(); - state.activations.push(event); - } - - fn record_feature_exposure(&self, event: FeatureExposureExtraDef) { - let mut state = self.state.lock().unwrap(); - state.exposures.push(event); - } - - fn record_malformed_feature_config(&self, event: MalformedFeatureConfigExtraDef) { - let mut state = self.state.lock().unwrap(); - state.malformeds.push(event); - } -} diff --git a/components/nimbus/src/tests/stateless/test_cirrus_client.rs b/components/nimbus/src/tests/stateless/test_cirrus_client.rs index b11b222d9c..9392dacf97 100644 --- a/components/nimbus/src/tests/stateless/test_cirrus_client.rs +++ b/components/nimbus/src/tests/stateless/test_cirrus_client.rs @@ -198,8 +198,7 @@ fn test_sends_metrics_on_enrollment() -> Result<()> { .unwrap(); client.enroll("test".to_string(), Default::default(), true, &[])?; - let metric_records: Vec = - serde_json::from_value(metrics_handler.assert_get_vec_value("enrollment_status"))?; + let metric_records: Vec = metrics_handler.get_enrollment_statuses(); assert_eq!(metric_records.len(), 1); assert_eq!(metric_records[0].slug(), exp.slug); assert_eq!(metric_records[0].status(), "Enrolled");