Debian Codegraph @

third_party/rust/glean/tests/uploader_capabilities.rs

raw
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 https://mozilla.org/MPL/2.0/.
4
5 //! This integration test should model how the RLB is used when embedded in another Rust application
6 //! (e.g. FOG/Firefox Desktop).
7 //!
8 //! We write a single test scenario per file to avoid any state keeping across runs
9 //! (different files run as different processes).
10
11 mod common;
12
13 use crossbeam_channel::{bounded, Sender};
14
15 use glean::net;
16 use glean::ConfigurationBuilder;
17
18 mod pings {
19 use super::*;
20 use glean::private::PingType;
21 use once_cell::sync::Lazy;
22
23 #[allow(non_upper_case_globals)]
24 pub static no_capabilities: Lazy<PingType> = Lazy::new(|| {
25 common::PingBuilder::new("no-capabilities")
26 .with_send_if_empty(true)
27 .build()
28 });
29
30 #[allow(non_upper_case_globals)]
31 pub static one_capability: Lazy<PingType> = Lazy::new(|| {
32 common::PingBuilder::new("one-capability")
33 .with_send_if_empty(true)
34 .with_uploader_capabilities(vec!["capability1".to_string()])
35 .build()
36 });
37
38 #[allow(non_upper_case_globals)]
39 pub static two_capabilities: Lazy<PingType> = Lazy::new(|| {
40 common::PingBuilder::new("two-capabilities")
41 .with_send_if_empty(true)
42 .with_uploader_capabilities(vec!["capability1".to_string(), "capability2".to_string()])
43 .build()
44 });
45 }
46
47 // Define a fake uploader that reports when and what it uploads.
48 #[derive(Debug)]
49 struct ReportingUploader {
50 sender: Sender<net::UploadResult>,
51 }
52
53 impl net::PingUploader for ReportingUploader {
54 fn upload(&self, upload_request: net::CapablePingUploadRequest) -> net::UploadResult {
55 let uploader_capabilities: Vec<String> = vec!["capability1".to_string()];
56
57 let Some(_upload_request) = upload_request.capable(|capabilities| {
58 capabilities.iter().all(|required_capability| {
59 uploader_capabilities
60 .iter()
61 .any(|uploader_capability| uploader_capability == required_capability)
62 })
63 }) else {
64 self.sender.send(net::UploadResult::incapable()).unwrap();
65 return net::UploadResult::incapable();
66 };
67
68 self.sender
69 .send(net::UploadResult::http_status(200))
70 .unwrap();
71 net::UploadResult::http_status(200)
72 }
73 }
74
75 /// Test scenario: We only upload pings we're capable of.
76 #[test]
77 fn interruptible_shutdown() {
78 common::enable_test_logging();
79
80 // Create a custom configuration to use our reporting uploader.
81 let dir = tempfile::tempdir().unwrap();
82 let tmpname = dir.path().to_path_buf();
83 let (tx, rx) = bounded(1);
84
85 let cfg = ConfigurationBuilder::new(true, tmpname.clone(), "glean-interruptible-shutdown")
86 .with_server_endpoint("invalid-test-host")
87 .with_use_core_mps(false)
88 .with_uploader(ReportingUploader { sender: tx })
89 .build();
90 common::initialize(cfg);
91
92 pings::no_capabilities.submit(None);
93 let result = rx.recv().unwrap();
94 assert!(
95 matches!(result, net::UploadResult::HttpStatus { code: 200 }),
96 "Can upload pings requiring no capabilities."
97 );
98
99 pings::one_capability.submit(None);
100 let result = rx.recv().unwrap();
101 assert!(
102 matches!(result, net::UploadResult::HttpStatus { code: 200 }),
103 "Can upload pings with matching capability."
104 );
105
106 pings::two_capabilities.submit(None);
107 let result = rx.recv().unwrap();
108 assert!(
109 matches!(result, net::UploadResult::Incapable { .. }),
110 "Can't upload pings requiring capabilities we don't support."
111 );
112 }