pub(crate) mod scale_utils;
use std::panic::AssertUnwindSafe;
use clickhouse_arrow::test_utils::{
ClickHouseContainer, get_or_create_benchmark_container, get_or_create_container, init_tracing,
};
use futures_util::FutureExt;
#[allow(unused)]
pub(crate) const DB_NAME: &str = "example_insert_test";
#[allow(unused)]
pub(crate) const ROWS: usize = 100_000;
const DISABLE_CLEANUP_ENV: &str = "DISABLE_CLEANUP";
#[allow(unused)]
pub(crate) fn print_banner(text: &str, width: Option<usize>) {
let content_width = width.unwrap_or_else(|| text.len() + 4);
let top = format!("╔{}╗", "═".repeat(content_width));
let bottom = format!("╚{}╝", "═".repeat(content_width));
let text_len = text.len();
let total_padding = content_width.saturating_sub(text_len);
let left_padding = total_padding / 2;
let right_padding = total_padding - left_padding;
let middle = format!("║{}{}{}║", " ".repeat(left_padding), text, " ".repeat(right_padding));
eprintln!("{top}");
eprintln!("{middle}");
eprintln!("{bottom}");
}
pub(crate) fn init(directives: Option<&[(&str, &str)]>) {
if let Ok(l) = std::env::var("RUST_LOG")
&& !l.is_empty()
{
init_tracing(directives);
}
}
#[allow(dead_code)] pub(crate) async fn setup(directives: Option<&[(&str, &str)]>) -> &'static ClickHouseContainer {
init(directives);
get_or_create_container(None).await
}
#[allow(dead_code)] pub(crate) async fn setup_benchmark(
directives: Option<&[(&str, &str)]>,
) -> &'static ClickHouseContainer {
init(directives);
get_or_create_benchmark_container(None).await
}
#[allow(dead_code)] pub(crate) async fn run_example_with_cleanup<F, Fut>(
example: F,
directives: Option<&[(&str, &str)]>,
) -> Result<(), Box<dyn std::any::Any + Send>>
where
F: FnOnce(&'static ClickHouseContainer) -> Fut + Send + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
let ch = setup(directives).await;
let result = AssertUnwindSafe(example(ch)).catch_unwind().await;
if std::env::var(DISABLE_CLEANUP_ENV).is_ok_and(|e| e.eq_ignore_ascii_case("true")) {
return result;
}
ch.shutdown().await.expect("Shutting down container");
result
}
#[allow(dead_code)] pub(crate) async fn run_benchmark_with_cleanup<F, Fut>(
example: F,
directives: Option<&[(&str, &str)]>,
) -> Result<(), Box<dyn std::any::Any + Send>>
where
F: FnOnce(&'static ClickHouseContainer) -> Fut + Send + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
let ch = setup_benchmark(directives).await;
let result = AssertUnwindSafe(example(ch)).catch_unwind().await;
if std::env::var(DISABLE_CLEANUP_ENV).is_ok_and(|e| e.eq_ignore_ascii_case("true")) {
return result;
}
ch.shutdown().await.expect("Shutting down container");
result
}