|
5 | 5 |
|
6 | 6 | //! Nimbus experiment integration for ads-client. |
7 | 7 |
|
8 | | -use nimbus::NimbusClient; |
| 8 | +use nimbus::{AppContext, NimbusClient}; |
| 9 | +use serde_json::json; |
9 | 10 | use std::sync::Arc; |
10 | 11 |
|
11 | 12 | const FEATURE_ID: &str = "ads-client"; |
12 | 13 |
|
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) |
| 14 | +struct NoopMetricsHandler; |
| 15 | +impl nimbus::metrics::MetricsHandler for NoopMetricsHandler { |
| 16 | + fn record_enrollment_statuses(&self, _: Vec<nimbus::metrics::EnrollmentStatusExtraDef>) {} |
| 17 | + fn record_feature_activation(&self, _: nimbus::metrics::FeatureExposureExtraDef) {} |
| 18 | + fn record_feature_exposure(&self, _: nimbus::metrics::FeatureExposureExtraDef) {} |
| 19 | + fn record_malformed_feature_config(&self, _: nimbus::metrics::MalformedFeatureConfigExtraDef) {} |
28 | 20 | } |
29 | 21 |
|
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 | | - } |
| 22 | +/// Internal experiment client that manages a NimbusClient with hardcoded experiment. |
| 23 | +pub struct ExperimentClient { |
| 24 | + nimbus: Arc<NimbusClient>, |
| 25 | +} |
48 | 26 |
|
49 | | - #[test] |
50 | | - fn test_fifty_fifty_experiment() { |
| 27 | +impl ExperimentClient { |
| 28 | + /// Create a new ExperimentClient with hardcoded 50/50 experiment. |
| 29 | + pub fn new(db_path: &str) -> Option<Self> { |
| 30 | + let ctx = AppContext { |
| 31 | + app_name: "ads-client".into(), |
| 32 | + app_id: "org.mozilla.ads-client".into(), |
| 33 | + channel: "release".into(), |
| 34 | + ..Default::default() |
| 35 | + }; |
| 36 | + |
| 37 | + // For a real implementation, the app would typically: |
| 38 | + // 1. Pass its own `AppContext` with real app info |
| 39 | + // 2. Pass a `MetricsHandler` that records to Glean |
| 40 | + // 3. Configure remote settings to fetch experiments from Mozilla's experiment server |
| 41 | + // 4. Not hardcode experiments in code |
| 42 | + let nimbus = Arc::new( |
| 43 | + NimbusClient::new( |
| 44 | + ctx, |
| 45 | + None, |
| 46 | + vec![], |
| 47 | + db_path, |
| 48 | + Box::new(NoopMetricsHandler), |
| 49 | + None, |
| 50 | + None, |
| 51 | + None, |
| 52 | + ) |
| 53 | + .ok()?, |
| 54 | + ); |
| 55 | + |
| 56 | + nimbus.initialize().ok()?; |
| 57 | + |
| 58 | + // Hardcoded 50/50 experiment for http-cache-enabled |
51 | 59 | let experiment = json!({ |
52 | 60 | "data": [{ |
53 | 61 | "schemaVersion": "1.0.0", |
54 | | - "slug": "ads-cache-test", |
| 62 | + "slug": "ads-client-cache", |
55 | 63 | "featureIds": ["ads-client"], |
56 | 64 | "branches": [ |
57 | 65 | { "slug": "control", "ratio": 1, "feature": { "featureId": "ads-client", "value": { "http-cache-enabled": true } } }, |
58 | 66 | { "slug": "treatment", "ratio": 1, "feature": { "featureId": "ads-client", "value": { "http-cache-enabled": false } } } |
59 | 67 | ], |
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", |
| 68 | + "bucketConfig": { "count": 10000, "start": 0, "total": 10000, "namespace": "ads-client-cache", "randomizationUnit": "nimbus_id" }, |
| 69 | + "appName": "ads-client", "appId": "org.mozilla.ads-client", "channel": "release", |
| 70 | + "userFacingName": "Ads Client Cache", "userFacingDescription": "50/50 experiment for HTTP cache", |
63 | 71 | "isEnrollmentPaused": false, "proposedEnrollment": 7, "referenceBranch": "control" |
64 | 72 | }] |
65 | 73 | }).to_string(); |
66 | 74 |
|
| 75 | + nimbus.set_experiments_locally(experiment).ok()?; |
| 76 | + nimbus.apply_pending_experiments().ok()?; |
| 77 | + |
| 78 | + Some(Self { nimbus }) |
| 79 | + } |
| 80 | + |
| 81 | + /// Check if HTTP cache is enabled for this user. |
| 82 | + pub fn is_http_cache_enabled(&self) -> bool { |
| 83 | + let Ok(Some(json)) = self |
| 84 | + .nimbus |
| 85 | + .get_feature_config_variables(FEATURE_ID.to_string()) |
| 86 | + else { |
| 87 | + return true; |
| 88 | + }; |
| 89 | + |
| 90 | + serde_json::from_str::<serde_json::Value>(&json) |
| 91 | + .ok() |
| 92 | + .and_then(|v| v.get("http-cache-enabled")?.as_bool()) |
| 93 | + .unwrap_or(true) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod tests { |
| 99 | + use super::*; |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_fifty_fifty_experiment() { |
67 | 103 | let (mut enabled, mut disabled) = (0, 0); |
68 | 104 | for i in 0..100 { |
69 | 105 | 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 |
| 106 | + let db_path = tmp_dir.path().join("nimbus.db"); |
| 107 | + let client = ExperimentClient::new(db_path.to_str().unwrap()).unwrap(); |
| 108 | + |
| 109 | + // Set a unique nimbus_id for each "user" |
90 | 110 | let mut bytes = [0u8; 16]; |
91 | 111 | 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(); |
| 112 | + client |
| 113 | + .nimbus |
| 114 | + .set_nimbus_id(&uuid::Uuid::from_bytes(bytes)) |
| 115 | + .unwrap(); |
| 116 | + client.nimbus.apply_pending_experiments().unwrap(); |
95 | 117 |
|
96 | | - if is_http_cache_enabled(Some(nimbus.clone())) { |
| 118 | + if client.is_http_cache_enabled() { |
97 | 119 | enabled += 1; |
98 | 120 | } else { |
99 | 121 | disabled += 1; |
|
0 commit comments