|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +*/ |
| 5 | + |
| 6 | +//! Nimbus experiment integration for ads-client. |
| 7 | +
|
| 8 | +use nimbus::NimbusClient; |
| 9 | +use std::sync::Arc; |
| 10 | + |
| 11 | +const FEATURE_ID: &str = "ads-client"; |
| 12 | + |
| 13 | +/// Query NimbusClient to check if HTTP cache is enabled for this user. |
| 14 | +/// Returns true (cache enabled) by default if no NimbusClient or no experiment is active. |
| 15 | +pub fn is_http_cache_enabled(nimbus: Option<Arc<NimbusClient>>) -> bool { |
| 16 | + let Some(nimbus) = nimbus else { |
| 17 | + return true; // Default: cache enabled |
| 18 | + }; |
| 19 | + |
| 20 | + let Ok(Some(json)) = nimbus.get_feature_config_variables(FEATURE_ID.to_string()) else { |
| 21 | + return true; // Default: cache enabled |
| 22 | + }; |
| 23 | + |
| 24 | + serde_json::from_str::<serde_json::Value>(&json) |
| 25 | + .ok() |
| 26 | + .and_then(|v| v.get("http-cache-enabled")?.as_bool()) |
| 27 | + .unwrap_or(true) |
| 28 | +} |
| 29 | + |
| 30 | +#[cfg(test)] |
| 31 | +mod tests { |
| 32 | + use super::*; |
| 33 | + use nimbus::AppContext; |
| 34 | + use serde_json::json; |
| 35 | + use uuid::Uuid; |
| 36 | + |
| 37 | + struct NoopMetricsHandler; |
| 38 | + impl nimbus::metrics::MetricsHandler for NoopMetricsHandler { |
| 39 | + fn record_enrollment_statuses(&self, _: Vec<nimbus::metrics::EnrollmentStatusExtraDef>) {} |
| 40 | + fn record_feature_activation(&self, _: nimbus::metrics::FeatureExposureExtraDef) {} |
| 41 | + fn record_feature_exposure(&self, _: nimbus::metrics::FeatureExposureExtraDef) {} |
| 42 | + fn record_malformed_feature_config( |
| 43 | + &self, |
| 44 | + _: nimbus::metrics::MalformedFeatureConfigExtraDef, |
| 45 | + ) { |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + #[test] |
| 50 | + fn test_fifty_fifty_experiment() { |
| 51 | + let experiment = json!({ |
| 52 | + "data": [{ |
| 53 | + "schemaVersion": "1.0.0", |
| 54 | + "slug": "ads-cache-test", |
| 55 | + "featureIds": ["ads-client"], |
| 56 | + "branches": [ |
| 57 | + { "slug": "control", "ratio": 1, "feature": { "featureId": "ads-client", "value": { "http-cache-enabled": true } } }, |
| 58 | + { "slug": "treatment", "ratio": 1, "feature": { "featureId": "ads-client", "value": { "http-cache-enabled": false } } } |
| 59 | + ], |
| 60 | + "bucketConfig": { "count": 10000, "start": 0, "total": 10000, "namespace": "test", "randomizationUnit": "nimbus_id" }, |
| 61 | + "appName": "test-app", "appId": "org.mozilla.test", "channel": "test", |
| 62 | + "userFacingName": "Test", "userFacingDescription": "Test", |
| 63 | + "isEnrollmentPaused": false, "proposedEnrollment": 7, "referenceBranch": "control" |
| 64 | + }] |
| 65 | + }).to_string(); |
| 66 | + |
| 67 | + let (mut enabled, mut disabled) = (0, 0); |
| 68 | + for i in 0..100 { |
| 69 | + let tmp_dir = tempfile::tempdir().unwrap(); |
| 70 | + let nimbus = Arc::new( |
| 71 | + NimbusClient::new( |
| 72 | + AppContext { |
| 73 | + app_name: "test-app".into(), |
| 74 | + app_id: "org.mozilla.test".into(), |
| 75 | + channel: "test".into(), |
| 76 | + ..Default::default() |
| 77 | + }, |
| 78 | + None, |
| 79 | + vec![], |
| 80 | + tmp_dir.path(), |
| 81 | + Box::new(NoopMetricsHandler), |
| 82 | + None, |
| 83 | + None, |
| 84 | + None, |
| 85 | + ) |
| 86 | + .unwrap(), |
| 87 | + ); |
| 88 | + nimbus.initialize().unwrap(); |
| 89 | + // Create a deterministic UUID from the loop index |
| 90 | + let mut bytes = [0u8; 16]; |
| 91 | + bytes[0] = i as u8; |
| 92 | + nimbus.set_nimbus_id(&Uuid::from_bytes(bytes)).unwrap(); |
| 93 | + nimbus.set_experiments_locally(experiment.clone()).unwrap(); |
| 94 | + nimbus.apply_pending_experiments().unwrap(); |
| 95 | + |
| 96 | + if is_http_cache_enabled(Some(nimbus.clone())) { |
| 97 | + enabled += 1; |
| 98 | + } else { |
| 99 | + disabled += 1; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + assert!( |
| 104 | + (30..=70).contains(&enabled), |
| 105 | + "Expected ~50% enabled, got {enabled}/100" |
| 106 | + ); |
| 107 | + assert!( |
| 108 | + (30..=70).contains(&disabled), |
| 109 | + "Expected ~50% disabled, got {disabled}/100" |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments