| 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 std::io::Read; |
| 14 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 15 |
use std::time::Instant; |
| 16 |
|
| 17 |
use crossbeam_channel::{bounded, Sender}; |
| 18 |
use flate2::read::GzDecoder; |
| 19 |
use serde_json::Value as JsonValue; |
| 20 |
|
| 21 |
use glean::net; |
| 22 |
use glean::{ConfigurationBuilder, PingRateLimit}; |
| 23 |
|
| 24 |
mod metrics { |
| 25 |
#![allow(non_upper_case_globals)] |
| 26 |
use glean::{private::BooleanMetric, CommonMetricData, Lifetime}; |
| 27 |
use once_cell::sync::Lazy; |
| 28 |
|
| 29 |
pub static sample_boolean: Lazy<BooleanMetric> = Lazy::new(|| { |
| 30 |
BooleanMetric::new(CommonMetricData { |
| 31 |
name: "sample_boolean".into(), |
| 32 |
category: "test.metrics".into(), |
| 33 |
send_in_pings: vec!["validation".into()], |
| 34 |
disabled: false, |
| 35 |
lifetime: Lifetime::Ping, |
| 36 |
..Default::default() |
| 37 |
}) |
| 38 |
}); |
| 39 |
} |
| 40 |
|
| 41 |
mod pings { |
| 42 |
use super::*; |
| 43 |
use glean::private::PingType; |
| 44 |
use once_cell::sync::Lazy; |
| 45 |
|
| 46 |
#[allow(non_upper_case_globals)] |
| 47 |
pub static validation: Lazy<PingType> = Lazy::new(|| { |
| 48 |
common::PingBuilder::new("validation") |
| 49 |
.with_send_if_empty(true) |
| 50 |
.build() |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
// Define a fake uploader that reports when and what it uploads. |
| 55 |
#[derive(Debug)] |
| 56 |
struct ReportingUploader { |
| 57 |
calls: AtomicUsize, |
| 58 |
sender: Sender<JsonValue>, |
| 59 |
} |
| 60 |
|
| 61 |
impl net::PingUploader for ReportingUploader { |
| 62 |
fn upload(&self, upload_request: net::CapablePingUploadRequest) -> net::UploadResult { |
| 63 |
let upload_request = upload_request.capable(|_| true).unwrap(); |
| 64 |
let calls = self.calls.fetch_add(1, Ordering::SeqCst); |
| 65 |
let body = upload_request.body; |
| 66 |
let decode = |body: Vec<u8>| { |
| 67 |
let mut gzip_decoder = GzDecoder::new(&body[..]); |
| 68 |
let mut s = String::with_capacity(body.len()); |
| 69 |
|
| 70 |
gzip_decoder |
| 71 |
.read_to_string(&mut s) |
| 72 |
.ok() |
| 73 |
.map(|_| &s[..]) |
| 74 |
.or_else(|| std::str::from_utf8(&body).ok()) |
| 75 |
.and_then(|payload| serde_json::from_str(payload).ok()) |
| 76 |
.unwrap() |
| 77 |
}; |
| 78 |
|
| 79 |
match calls { |
| 80 |
// First goes through immediately. |
| 81 |
0 => { |
| 82 |
self.sender.send(decode(body)).unwrap(); |
| 83 |
net::UploadResult::http_status(200) |
| 84 |
} |
| 85 |
// Second SHOULD NEVER SEND |
| 86 |
1 => panic!("We should shutdown before getting to the second ping"), |
| 87 |
// Any others ought to be impossible. |
| 88 |
_ => panic!("Wat."), |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/// Test scenario: We can interrupt a long glean.upload Wait during shutdown. |
| 94 |
/// |
| 95 |
/// The app is initialized, in turn Glean gets initialized without problems. |
| 96 |
/// A custom ping is submitted once, triggering ping throttling. |
| 97 |
/// It is submitted a second time to convince `glean.upload` to be in a `Wait` task. |
| 98 |
/// From this position we ask for Glean to promptly shut down. |
| 99 |
/// We expect this to happen reasonably quickly (within 2s) instead of waiting for the |
| 100 |
/// entire throttling interval _or_ for uploader_shutdown's last-ditch timeout. |
| 101 |
#[test] |
| 102 |
fn interruptible_shutdown() { |
| 103 |
common::enable_test_logging(); |
| 104 |
|
| 105 |
// Create a custom configuration to use our reporting uploader. |
| 106 |
let dir = tempfile::tempdir().unwrap(); |
| 107 |
let tmpname = dir.path().to_path_buf(); |
| 108 |
let (tx, rx) = bounded(1); |
| 109 |
|
| 110 |
let mut cfg = ConfigurationBuilder::new(true, tmpname.clone(), "glean-interruptible-shutdown") |
| 111 |
.with_server_endpoint("invalid-test-host") |
| 112 |
.with_use_core_mps(false) |
| 113 |
.with_uploader(ReportingUploader { |
| 114 |
calls: AtomicUsize::new(0), |
| 115 |
sender: tx, |
| 116 |
}) |
| 117 |
.build(); |
| 118 |
cfg.rate_limit = Some(PingRateLimit { |
| 119 |
seconds_per_interval: 600, // Needs only be longer than the timeout in `glean::uploader_shutdown()`. |
| 120 |
pings_per_interval: 1, // throttle thyself immediately. |
| 121 |
}); |
| 122 |
common::initialize(cfg); |
| 123 |
|
| 124 |
// Wait for init to finish, |
| 125 |
// otherwise we might be to quick with calling `shutdown`. |
| 126 |
let _ = metrics::sample_boolean.test_get_value(None); |
| 127 |
|
| 128 |
// fast |
| 129 |
pings::validation.submit(None); |
| 130 |
// wait for it to be uploaded |
| 131 |
let _body = rx.recv().unwrap(); |
| 132 |
|
| 133 |
// Now we're in throttling territory. |
| 134 |
pings::validation.submit(None); |
| 135 |
|
| 136 |
// Now we shut down. |
| 137 |
// This should complete really fast because we'll interrupt the `glean.upload` thread |
| 138 |
// from its 600-second Wait task. |
| 139 |
// ...so long as everything's working properly. So let's time it. |
| 140 |
|
| 141 |
let pre_shutdown = Instant::now(); |
| 142 |
|
| 143 |
glean::shutdown(); |
| 144 |
|
| 145 |
let shutdown_elapsed = pre_shutdown.elapsed(); |
| 146 |
assert!( |
| 147 |
shutdown_elapsed.as_secs() < 2, |
| 148 |
"We shouldn't have been slow on shutdown." |
| 149 |
); |
| 150 |
} |