Lines
97.75 %
Functions
95.65 %
Branches
100 %
use sqlx::{Connection, PgConnection};
use super::{
CLUSTER_PREFIX, RAM_DISK, check_socket_path, cluster_root, first_started, parse_major,
port_is_free, ram_disk_has_room, server_options, start,
};
#[tokio::test]
async fn the_cluster_is_utf8_with_c_collation_like_prod() -> anyhow::Result<()> {
let postgres = start().await?;
let mut conn = PgConnection::connect(&postgres.database_url()).await?;
let (encoding, collate): (String, String) = sqlx::query_as(
"SELECT pg_encoding_to_char(encoding), datcollate FROM pg_database WHERE datname = current_database()",
)
.fetch_one(&mut conn)
.await?;
assert_eq!((encoding.as_str(), collate.as_str()), ("UTF8", "C"));
let nbsp: String = sqlx::query_scalar("SELECT btrim(E'\\u00a0x\\u00a0', E'\\u00a0')")
assert_eq!(nbsp, "x");
Ok(())
}
#[test]
fn a_held_port_is_not_free() -> anyhow::Result<()> {
let held = std::net::TcpListener::bind(("127.0.0.1", 0))?;
assert!(!port_is_free(held.local_addr()?.port()));
fn a_port_the_probe_rejects_gets_no_start_attempt() -> anyhow::Result<()> {
let (taken, free) = (54_001, 54_002);
let mut attempts = Vec::new();
let chosen = first_started(
[taken, free].into_iter(),
|port| port != taken,
|port| {
attempts.push(port);
Ok(true)
},
)?;
assert_eq!((attempts, chosen), (vec![free], free));
fn a_failed_start_falls_through_to_the_next_free_port() -> anyhow::Result<()> {
let chosen = first_started(1..4u16, |_| true, |port| Ok(port == 3))?;
assert_eq!(chosen, 3);
fn no_free_port_and_no_working_server_are_told_apart() {
let none_free = first_started(1..4u16, |_| false, |_| Ok(true)).map_err(|e| e.to_string());
let none_start = first_started(1..4u16, |_| true, |_| Ok(false)).map_err(|e| e.to_string());
assert!(none_free.is_err_and(|e| e.starts_with("no free port")));
assert!(none_start.is_err_and(|e| e.starts_with("postgres failed to start")));
fn the_version_banner_yields_its_major() -> anyhow::Result<()> {
assert_eq!(parse_major("pg_ctl (PostgreSQL) 18.6\n")?, 18);
assert_eq!(
parse_major("postgres (PostgreSQL) 17.11 (Debian 17.11-1)\n")?,
17
);
assert!(parse_major("garbage").is_err());
assert!(parse_major("postgres (PostgreSQL) devel\n").is_err());
fn setup_durations_are_requested_only_from_postgres_18() {
let socket = std::path::Path::new("/s");
assert!(server_options(18, 54320, socket).contains("log_connections=setup_durations"));
assert!(!server_options(17, 54320, socket).contains("log_connections"));
fn an_overlong_socket_path_is_refused_up_front() {
let long = std::path::PathBuf::from("/tmp").join("x".repeat(120));
assert!(check_socket_path(&long).is_err_and(|e| e.to_string().contains("shorter TMPDIR")));
assert!(check_socket_path(std::path::Path::new("/tmp/s")).is_ok());
async fn the_cluster_logs_connection_setup_durations() -> anyhow::Result<()> {
let setting: String = sqlx::query_scalar("SHOW log_connections")
let version: i32 = sqlx::query_scalar("SELECT current_setting('server_version_num')::int")
let expected = if version >= 180_000 {
"setup_durations"
} else {
"off"
assert_eq!(setting, expected);
fn the_cluster_root_is_in_ram_exactly_when_the_ram_disk_has_room() -> anyhow::Result<()> {
let root = cluster_root()?;
let name = root
.path()
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default();
assert!(name.starts_with(CLUSTER_PREFIX));
assert_eq!(root.path().starts_with(RAM_DISK), ram_disk_has_room());