#[cfg(feature = "async-api")]
mod async_api;
mod cache_integration;
mod cleanup;
mod connection;
mod delegation;
mod guard;
mod handle;
mod installation;
mod lifecycle;
mod lifecycle_template;
pub(crate) mod panic_utils;
mod runtime;
mod runtime_mode;
mod shutdown;
#[cfg(any(unix, windows))]
mod shutdown_hook;
#[cfg(all(
any(unix, windows),
any(doc, test, feature = "cluster-unit-tests", feature = "dev-worker")
))]
pub use self::shutdown_hook::{
PostmasterPid,
PostmasterProcess,
postmaster_process_is_running,
process_is_running,
read_postmaster_pid,
read_postmaster_process,
};
#[cfg(all(test, feature = "loom-tests"))]
mod lifecycle_loom_tests;
mod startup;
mod temporary_database;
mod worker_invoker;
mod worker_operation;
use std::ops::Deref;
use tracing::info_span;
pub(crate) use self::startup::setup_postgres_only;
#[cfg(any(doc, test, feature = "cluster-unit-tests", feature = "dev-worker"))]
pub use self::worker_invoker::WorkerInvoker;
#[doc(hidden)]
pub use self::worker_operation::WorkerOperation;
pub use self::{
connection::{ConnectionMetadata, TestClusterConnection},
guard::ClusterGuard,
handle::ClusterHandle,
lifecycle::DatabaseName,
temporary_database::TemporaryDatabase,
};
use self::{
runtime::build_runtime,
runtime_mode::ClusterRuntime,
startup::{cache_config_from_bootstrap, start_postgres},
};
use crate::{
bootstrap_for_tests,
env::ScopedEnv,
error::BootstrapResult,
observability::LOG_TARGET,
};
#[derive(Debug)]
pub struct TestCluster {
pub(crate) handle: ClusterHandle,
pub(crate) guard: ClusterGuard,
}
impl TestCluster {
pub fn new() -> BootstrapResult<Self> {
let (handle, guard) = Self::new_split()?;
Ok(Self { handle, guard })
}
pub fn new_split() -> BootstrapResult<(ClusterHandle, ClusterGuard)> {
let span = info_span!(target: LOG_TARGET, "test_cluster");
let (runtime, env_vars, env_guard, outcome) = {
let _entered = span.enter();
let initial_bootstrap = bootstrap_for_tests()?;
let cache_config = cache_config_from_bootstrap(&initial_bootstrap);
let runtime = build_runtime()?;
let env_vars = initial_bootstrap.environment.to_env();
let env_guard = ScopedEnv::apply(&env_vars);
let outcome = start_postgres(&runtime, initial_bootstrap, &env_vars, &cache_config)?;
(runtime, env_vars, env_guard, outcome)
};
let handle = ClusterHandle::new(outcome.bootstrap.clone());
let guard = ClusterGuard {
runtime: ClusterRuntime::Sync(runtime),
postgres: outcome.postgres,
bootstrap: outcome.bootstrap,
is_managed_via_worker: outcome.is_managed_via_worker,
env_vars,
worker_guard: None,
_env_guard: env_guard,
_cluster_span: span,
};
Ok((handle, guard))
}
#[doc(hidden)]
#[must_use]
pub fn with_worker_guard(self, worker_guard: Option<ScopedEnv>) -> Self {
Self {
handle: self.handle,
guard: self.guard.with_worker_guard(worker_guard),
}
}
}
impl Deref for TestCluster {
type Target = ClusterHandle;
fn deref(&self) -> &Self::Target { &self.handle }
}
#[cfg(test)]
mod mod_tests;
#[cfg(all(test, feature = "cluster-unit-tests"))]
mod drop_logging_tests;
#[cfg(all(test, not(feature = "cluster-unit-tests")))]
#[path = "../../tests/test_cluster.rs"]
mod test_cluster_tests;