diff options
Diffstat (limited to 'third_party/rust/glean/src/test.rs')
-rw-r--r-- | third_party/rust/glean/src/test.rs | 215 |
1 files changed, 87 insertions, 128 deletions
diff --git a/third_party/rust/glean/src/test.rs b/third_party/rust/glean/src/test.rs index cb41b49b66..16d6d05447 100644 --- a/third_party/rust/glean/src/test.rs +++ b/third_party/rust/glean/src/test.rs @@ -22,22 +22,16 @@ use crate::common_test::{lock_test, new_glean, GLOBAL_APPLICATION_ID}; fn send_a_ping() { let _lock = lock_test(); - let (s, r) = crossbeam_channel::bounded::<String>(1); + let (s, r) = crossbeam_channel::bounded::<net::PingUploadRequest>(1); - // Define a fake uploader that reports back the submission URL - // using a crossbeam channel. + // Define a fake uploader that reports back the ping upload request. #[derive(Debug)] pub struct FakeUploader { - sender: crossbeam_channel::Sender<String>, + sender: crossbeam_channel::Sender<net::PingUploadRequest>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(url).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request).unwrap(); net::UploadResult::http_status(200) } } @@ -55,12 +49,54 @@ fn send_a_ping() { // Define a new ping and submit it. const PING_NAME: &str = "test-ping"; - let custom_ping = private::PingType::new(PING_NAME, true, true, true, vec![]); + let custom_ping = private::PingType::new(PING_NAME, true, true, true, true, vec![]); custom_ping.submit(None); // Wait for the ping to arrive. - let url = r.recv().unwrap(); - assert!(url.contains(PING_NAME)); + let upload_request = r.recv().unwrap(); + assert!(upload_request.body_has_info_sections); + assert_eq!(upload_request.ping_name, PING_NAME); + assert!(upload_request.url.contains(PING_NAME)); +} + +#[test] +fn send_a_ping_without_info_sections() { + let _lock = lock_test(); + + let (s, r) = crossbeam_channel::bounded::<net::PingUploadRequest>(1); + + // Define a fake uploader that reports back the ping upload request. + #[derive(Debug)] + pub struct FakeUploader { + sender: crossbeam_channel::Sender<net::PingUploadRequest>, + } + impl net::PingUploader for FakeUploader { + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request).unwrap(); + net::UploadResult::http_status(200) + } + } + + // Create a custom configuration to use a fake uploader. + let dir = tempfile::tempdir().unwrap(); + let tmpname = dir.path().to_path_buf(); + + let cfg = ConfigurationBuilder::new(true, tmpname, GLOBAL_APPLICATION_ID) + .with_server_endpoint("invalid-test-host") + .with_uploader(FakeUploader { sender: s }) + .build(); + + let _t = new_glean(Some(cfg), true); + + // Define a new ping and submit it. + const PING_NAME: &str = "noinfo-ping"; + let custom_ping = private::PingType::new(PING_NAME, true, true, true, false, vec![]); + custom_ping.submit(None); + + // Wait for the ping to arrive. + let upload_request = r.recv().unwrap(); + assert!(!upload_request.body_has_info_sections); + assert_eq!(upload_request.ping_name, PING_NAME); } #[test] @@ -190,13 +226,8 @@ fn sending_of_foreground_background_pings() { sender: crossbeam_channel::Sender<String>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(url).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.url).unwrap(); net::UploadResult::http_status(200) } } @@ -263,13 +294,8 @@ fn sending_of_startup_baseline_ping() { sender: crossbeam_channel::Sender<String>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(url).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.url).unwrap(); net::UploadResult::http_status(200) } } @@ -315,13 +341,8 @@ fn no_dirty_baseline_on_clean_shutdowns() { sender: crossbeam_channel::Sender<String>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(url).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.url).unwrap(); net::UploadResult::http_status(200) } } @@ -543,12 +564,8 @@ fn ping_collection_must_happen_after_concurrently_scheduled_metrics_recordings() sender: crossbeam_channel::Sender<(String, JsonValue)>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + let net::PingUploadRequest { body, url, .. } = upload_request; // Decode the gzipped body. let mut gzip_decoder = GzDecoder::new(&body[..]); let mut s = String::with_capacity(body.len()); @@ -577,7 +594,7 @@ fn ping_collection_must_happen_after_concurrently_scheduled_metrics_recordings() ); let ping_name = "custom_ping_1"; - let ping = private::PingType::new(ping_name, true, false, true, vec![]); + let ping = private::PingType::new(ping_name, true, false, true, true, vec![]); let metric = private::StringMetric::new(CommonMetricData { name: "string_metric".into(), category: "telemetry".into(), @@ -681,13 +698,8 @@ fn sending_deletion_ping_if_disabled_outside_of_run() { sender: crossbeam_channel::Sender<String>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(url).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.url).unwrap(); net::UploadResult::http_status(200) } } @@ -731,13 +743,8 @@ fn no_sending_of_deletion_ping_if_unchanged_outside_of_run() { sender: crossbeam_channel::Sender<String>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(url).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.url).unwrap(); net::UploadResult::http_status(200) } } @@ -779,12 +786,8 @@ fn deletion_request_ping_contains_experimentation_id() { sender: crossbeam_channel::Sender<JsonValue>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - _url: String, - body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + let body = upload_request.body; let mut gzip_decoder = GzDecoder::new(&body[..]); let mut body_str = String::with_capacity(body.len()); let data: JsonValue = gzip_decoder @@ -847,12 +850,8 @@ fn test_sending_of_startup_baseline_ping_with_application_lifetime_metric() { sender: crossbeam_channel::Sender<(String, JsonValue)>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + let net::PingUploadRequest { url, body, .. } = upload_request; // Decode the gzipped body. let mut gzip_decoder = GzDecoder::new(&body[..]); let mut s = String::with_capacity(body.len()); @@ -932,13 +931,8 @@ fn setting_debug_view_tag_before_initialization_should_not_crash() { sender: crossbeam_channel::Sender<Vec<(String, String)>>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - _url: String, - _body: Vec<u8>, - headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(headers).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.headers).unwrap(); net::UploadResult::http_status(200) } } @@ -983,13 +977,8 @@ fn setting_source_tags_before_initialization_should_not_crash() { sender: crossbeam_channel::Sender<Vec<(String, String)>>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - _url: String, - _body: Vec<u8>, - headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(headers).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.headers).unwrap(); net::UploadResult::http_status(200) } } @@ -1033,13 +1022,8 @@ fn setting_source_tags_after_initialization_should_not_crash() { sender: crossbeam_channel::Sender<Vec<(String, String)>>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - _url: String, - _body: Vec<u8>, - headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(headers).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.headers).unwrap(); net::UploadResult::http_status(200) } } @@ -1097,13 +1081,8 @@ fn flipping_upload_enabled_respects_order_of_events() { sender: crossbeam_channel::Sender<String>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(url).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.url).unwrap(); net::UploadResult::http_status(200) } } @@ -1118,7 +1097,7 @@ fn flipping_upload_enabled_respects_order_of_events() { .build(); // We create a ping and a metric before we initialize Glean - let sample_ping = PingType::new("sample-ping-1", true, false, true, vec![]); + let sample_ping = PingType::new("sample-ping-1", true, false, true, true, vec![]); let metric = private::StringMetric::new(CommonMetricData { name: "string_metric".into(), category: "telemetry".into(), @@ -1155,19 +1134,14 @@ fn registering_pings_before_init_must_work() { sender: crossbeam_channel::Sender<String>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(url).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.url).unwrap(); net::UploadResult::http_status(200) } } // Create a custom ping and attempt its registration. - let sample_ping = PingType::new("pre-register", true, true, true, vec![]); + let sample_ping = PingType::new("pre-register", true, true, true, true, vec![]); // Create a custom configuration to use a fake uploader. let dir = tempfile::tempdir().unwrap(); @@ -1201,13 +1175,8 @@ fn test_a_ping_before_submission() { sender: crossbeam_channel::Sender<String>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { - self.sender.send(url).unwrap(); + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { + self.sender.send(upload_request.url).unwrap(); net::UploadResult::http_status(200) } } @@ -1224,7 +1193,7 @@ fn test_a_ping_before_submission() { let _t = new_glean(Some(cfg), true); // Create a custom ping and register it. - let sample_ping = PingType::new("custom1", true, true, true, vec![]); + let sample_ping = PingType::new("custom1", true, true, true, true, vec![]); let metric = CounterMetric::new(CommonMetricData { name: "counter_metric".into(), @@ -1308,12 +1277,7 @@ fn signaling_done() { counter: Arc<Mutex<HashMap<ThreadId, u32>>>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - _url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { + fn upload(&self, _upload_request: net::PingUploadRequest) -> net::UploadResult { let mut map = self.counter.lock().unwrap(); *map.entry(thread::current().id()).or_insert(0) += 1; @@ -1346,7 +1310,7 @@ fn signaling_done() { // Define a new ping and submit it. const PING_NAME: &str = "test-ping"; - let custom_ping = private::PingType::new(PING_NAME, true, true, true, vec![]); + let custom_ping = private::PingType::new(PING_NAME, true, true, true, true, vec![]); custom_ping.submit(None); custom_ping.submit(None); @@ -1385,17 +1349,12 @@ fn configure_ping_throttling() { done: Arc<std::sync::atomic::AtomicBool>, } impl net::PingUploader for FakeUploader { - fn upload( - &self, - url: String, - _body: Vec<u8>, - _headers: Vec<(String, String)>, - ) -> net::UploadResult { + fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult { if self.done.load(std::sync::atomic::Ordering::SeqCst) { // If we've outlived the test, just lie. return net::UploadResult::http_status(200); } - self.sender.send(url).unwrap(); + self.sender.send(upload_request.url).unwrap(); net::UploadResult::http_status(200) } } @@ -1422,7 +1381,7 @@ fn configure_ping_throttling() { // Define a new ping. const PING_NAME: &str = "test-ping"; - let custom_ping = private::PingType::new(PING_NAME, true, true, true, vec![]); + let custom_ping = private::PingType::new(PING_NAME, true, true, true, true, vec![]); // Submit and receive it `pings_per_interval` times. for _ in 0..pings_per_interval { |