use std::sync::Arc;
use crate::config::CassandraConfig;
use crate::connection::CassandraConnection;
use crate::error::CassandraResult;
#[derive(Clone)]
pub struct CassandraPool {
connection: Arc<CassandraConnection>,
}
impl CassandraPool {
pub async fn connect(config: CassandraConfig) -> CassandraResult<Self> {
let connection = CassandraConnection::connect(config).await?;
Ok(Self {
connection: Arc::new(connection),
})
}
pub async fn close(self) -> CassandraResult<()> {
Ok(())
}
pub fn connection(&self) -> &CassandraConnection {
&self.connection
}
pub fn shared(&self) -> Arc<CassandraConnection> {
Arc::clone(&self.connection)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pool_connect_fails_fast_on_empty_nodes() {
let config = CassandraConfig::builder().build();
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let result = rt.block_on(CassandraPool::connect(config));
assert!(result.is_err());
}
}