#[allow(dead_code)]
mod support;
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
use support::server::{destination_or_skip, on_server};
use support::Sandbox;
static LIVE: AtomicUsize = AtomicUsize::new(0);
struct Counting;
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let pointer = unsafe { System.alloc(layout) };
if !pointer.is_null() {
LIVE.fetch_add(layout.size(), Ordering::Relaxed);
}
pointer
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
unsafe { System.dealloc(pointer, layout) };
}
unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let moved = unsafe { System.realloc(pointer, layout, new_size) };
if !moved.is_null() {
LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
LIVE.fetch_add(new_size, Ordering::Relaxed);
}
moved
}
}
#[global_allocator]
static ALLOCATOR: Counting = Counting;
fn live() -> usize {
LIVE.load(Ordering::Relaxed)
}
const A_READ: &str = "SELECT 1 AS a UNION ALL SELECT 2 UNION ALL SELECT 3";
const WIDER_THAN_ANY_CEILING: &str =
"SELECT TABLE_NAME AS t, COLUMN_NAME AS c FROM information_schema.COLUMNS";
const DECLARED_BY_THE_ORDER: &str = r#","accumulated_for":["sort"]"#;
#[test]
fn one_read_answers_with_the_same_bytes_whichever_path_carried_it() {
let Some(dsn) = destination_or_skip("one read answered by both paths") else {
return;
};
let sandbox = Sandbox::new();
let flowing = on_server(&sandbox, &dsn, &["--limit", "10", "query", "run", A_READ]);
assert_eq!(flowing.code, 0, "the flow: {}", flowing.err);
let gathered = on_server(
&sandbox,
&dsn,
&["--limit", "10", "--sort", "a", "query", "run", A_READ],
);
assert_eq!(gathered.code, 0, "the gathered answer: {}", gathered.err);
assert!(
streams(&sandbox, &["--limit", "10", "query", "run", A_READ]),
"the plain read must reach the flow, or this proof compares one path with itself"
);
assert!(
!streams(
&sandbox,
&["--limit", "10", "--sort", "a", "query", "run", A_READ]
),
"asking for an order must reach the gathered path"
);
assert!(
gathered.out.contains(DECLARED_BY_THE_ORDER),
"the order must declare itself, or the discount below is hiding something else: {}",
gathered.out
);
let discounted = gathered.out.replace(DECLARED_BY_THE_ORDER, "");
assert_eq!(
flowing.out, discounted,
"one read answered two ways: flowing `{}` against gathered `{}`",
flowing.out, gathered.out
);
assert!(
flowing.out.contains(r#""rows":[{"a":1},{"a":2},{"a":3}]"#),
"the rows themselves must be there: {}",
flowing.out
);
}
#[test]
fn the_single_document_costs_the_same_at_the_last_row_as_at_the_first() {
use mariadb_cli::cli::globals::OutputFormat;
use mariadb_cli::output::format::for_format;
use mariadb_cli::output::stream::Stream;
let rows = 200_000;
let tail = mariadb_cli::output::Envelope::new("query run", serde_json::json!({}));
let columns = vec!["id".to_owned(), "name".to_owned()];
let before = live();
let mut flow = Stream::open(
std::io::sink(),
for_format(OutputFormat::Json),
&tail,
&columns,
)
.expect("the flow opens");
let mut settled = 0;
let mut peak = 0;
for index in 0..rows {
flow.write(&wire_row(index)).expect("the row is written");
match index.cmp(&1_000) {
std::cmp::Ordering::Less => {}
std::cmp::Ordering::Equal => settled = live().saturating_sub(before),
std::cmp::Ordering::Greater => peak = peak.max(live().saturating_sub(before)),
}
}
assert_eq!(
flow.finish(&tail, &[], None).expect("the flow closes"),
rows,
"every row reached the channel"
);
let before_gathering = live();
let held: Vec<mariadb_cli::output::Row> = (0..rows).map(wire_row).collect();
let holding_them_all = live().saturating_sub(before_gathering);
assert_eq!(held.len() as u64, rows);
drop(held);
let room = 4 * 1024 * 1024;
assert!(
peak <= settled + room,
"the flow settled at {settled} bytes and peaked at {peak}, so it is holding rows \
rather than writing them"
);
assert!(
peak * 4 < holding_them_all,
"the flow peaked at {peak} bytes and the same rows gathered hold {holding_them_all}; \
a flow that costs what gathering costs is not a flow"
);
}
#[test]
fn a_document_cut_part_way_through_is_still_a_document() {
let Some(dsn) = destination_or_skip("a cut document that never closes") else {
return;
};
let sandbox = Sandbox::new();
let cut = on_server(
&sandbox,
&dsn,
&[
"--max-output-bytes",
"2000",
"query",
"run",
WIDER_THAN_ANY_CEILING,
],
);
assert_eq!(cut.code, 0, "a cut flow is still an answer: {}", cut.err);
assert!(
cut.out.starts_with(r#"{"command":"query run""#),
"a cut flow still carries what it managed to write: {}",
cut.out
);
let read: serde_json::Value = serde_json::from_str(cut.out.trim_end())
.unwrap_or_else(|why| panic!("a cut answer must still be readable: {why}"));
assert_eq!(
read["applied"]["output_truncated"],
serde_json::json!(true),
"the cut is no longer visible in the structure, so it has to be \
visible in the field, and it is not"
);
assert!(
read["applied"]["dropped"].as_u64().is_some_and(|n| n > 0),
"an answer that dropped rows must say how many it dropped"
);
let whole = on_server(&sandbox, &dsn, &["--limit", "5", "query", "run", A_READ]);
assert_eq!(whole.code, 0, "the whole answer: {}", whole.err);
let parsed = whole.answer();
assert_eq!(parsed["data"]["rows"].as_array().expect("rows").len(), 3);
}
#[test]
fn asking_for_a_ceiling_of_bytes_still_reaches_the_flow() {
let sandbox = Sandbox::new();
assert!(
streams(
&sandbox,
&["--max-output-bytes", "2000", "query", "run", A_READ]
),
"a ceiling of bytes sent this read to the path that holds every row \
first, which costs the flow the one thing a flow is for"
);
assert!(
streams(
&sandbox,
&[
"--output-format",
"ndjson",
"--max-output-bytes",
"2000",
"query",
"run",
A_READ
]
),
"a ceiling of bytes sent the line-oriented read to the gathered path"
);
}
const AT_THE_GATE: &[(&str, &[&str], bool)] = &[
("a plain read", &["query", "run", A_READ], true),
(
"a ceiling of rows",
&["--limit", "10", "query", "run", A_READ],
true,
),
(
"a ceiling of bytes",
&["--max-output-bytes", "2000", "query", "run", A_READ],
true,
),
(
"a projection",
&["--select", "a", "query", "run", A_READ],
true,
),
("an order", &["--sort", "a", "query", "run", A_READ], false),
("a count", &["--count-only", "query", "run", A_READ], false),
("a summary", &["--summary", "query", "run", A_READ], false),
(
"a signature",
&["--checksum", "query", "run", A_READ],
false,
),
];
#[test]
fn every_condition_the_gate_weighs_is_read_back() {
let sandbox = Sandbox::new();
let mut wrong = Vec::new();
for (what, argv, expected) in AT_THE_GATE {
let reached = streams(&sandbox, argv);
if reached != *expected {
let went = if reached { "flowed" } else { "gathered" };
let meant = if *expected { "flow" } else { "gather" };
wrong.push(format!("{what}: {went}, and it must {meant}"));
}
}
assert!(
wrong.is_empty(),
"the gate sent these down the wrong path: {wrong:?}"
);
}
fn wire_row(index: u64) -> mariadb_cli::output::Row {
let mut row = mariadb_cli::output::Row::new();
row.insert("id".to_owned(), serde_json::json!(index));
row.insert("name".to_owned(), serde_json::json!(format!("row-{index}")));
row
}
fn streams(sandbox: &Sandbox, args: &[&str]) -> bool {
use clap::Parser as _;
let command_line: Vec<String> = ["mariadb-cli", "--config-dir"]
.into_iter()
.map(ToOwned::to_owned)
.chain(std::iter::once(
sandbox.path().to_string_lossy().into_owned(),
))
.chain(args.iter().map(|piece| (*piece).to_owned()))
.collect();
let cli = mariadb_cli::cli::Cli::try_parse_from(command_line).expect("the invocation parses");
let config =
mariadb_cli::config::Config::load(Some(sandbox.path())).expect("the location loads");
mariadb_cli::dispatch::flow::applies(&cli, &config)
}