Skip to content

Commit

Permalink
fix: memory leak in unit test of telemetry (#3897)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia authored May 9, 2024
1 parent b8a325d commit 66aa08d
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/common/greptimedb-telemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ mod tests {
use common_test_util::ports;
use hyper::service::{make_service_fn, service_fn};
use hyper::Server;
use reqwest::Client;
use reqwest::{Client, Response};
use tokio::spawn;

use crate::{default_get_uuid, Collector, GreptimeDBTelemetry, Mode, StatisticData};
Expand Down Expand Up @@ -395,21 +395,38 @@ mod tests {
}
}

async fn get_telemetry_report(
mut report: GreptimeDBTelemetry,
url: &'static str,
) -> Option<Response> {
report.telemetry_url = url;
report.report_telemetry_info().await
}

fn contravariance<'a>(x: &'a str) -> &'static str
where
'static: 'a,
{
unsafe { std::mem::transmute(x) }
}

let working_home_temp = tempfile::Builder::new()
.prefix("greptimedb_telemetry")
.tempdir()
.unwrap();
let working_home = working_home_temp.path().to_str().unwrap().to_string();

let test_statistic = Box::new(TestStatistic);
let mut test_report = GreptimeDBTelemetry::new(
let test_report = GreptimeDBTelemetry::new(
Some(working_home.clone()),
test_statistic,
Arc::new(AtomicBool::new(true)),
);
let url = Box::leak(format!("{}:{}", "http://localhost", port).into_boxed_str());
test_report.telemetry_url = url;
let response = test_report.report_telemetry_info().await.unwrap();
let url = format!("http://localhost:{}", port);
let response = {
let url = contravariance(url.as_str());
get_telemetry_report(test_report, url).await.unwrap()
};

let body = response.json::<StatisticData>().await.unwrap();
assert_eq!(env::consts::ARCH, body.arch);
Expand All @@ -420,13 +437,15 @@ mod tests {
assert_eq!(1, body.nodes.unwrap());

let failed_statistic = Box::new(FailedStatistic);
let mut failed_report = GreptimeDBTelemetry::new(
let failed_report = GreptimeDBTelemetry::new(
Some(working_home),
failed_statistic,
Arc::new(AtomicBool::new(true)),
);
failed_report.telemetry_url = url;
let response = failed_report.report_telemetry_info().await;
let response = {
let url = contravariance(url.as_str());
get_telemetry_report(failed_report, url).await
};
assert!(response.is_none());

let client = Client::builder()
Expand Down

0 comments on commit 66aa08d

Please sign in to comment.