Add another integration test

This commit is contained in:
Lennart K
2026-01-29 10:25:00 +01:00
parent 233cf2ea37
commit 0703b7b470
8 changed files with 133 additions and 45 deletions

View File

@@ -3,27 +3,38 @@ use rustical::{
config::{Config, DataStoreConfig, HttpConfig, SqliteDataStoreConfig},
};
use std::{
collections::HashSet,
net::{Ipv4Addr, SocketAddrV4, TcpListener},
sync::Arc,
sync::{Arc, Mutex, OnceLock},
thread::{self, JoinHandle},
};
use tokio::sync::Notify;
use tokio_util::sync::CancellationToken;
// When running multiple integration tests we need to make sure that they don't get the same port
static BOUND_PORTS: OnceLock<Mutex<HashSet<u16>>> = OnceLock::new();
pub fn find_free_port() -> Option<u16> {
let bound_ports = BOUND_PORTS.get_or_init(Mutex::default);
let mut bound_ports_write = bound_ports.lock().unwrap();
let mut port = 15000;
// Frees the socket on drop such that this function returns a free port
while TcpListener::bind(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port)).is_err() {
while TcpListener::bind(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port)).is_err()
|| bound_ports_write.contains(&port)
{
port += 1;
if port >= 16000 {
return None;
}
}
bound_ports_write.insert(port);
Some(port)
}
pub fn rustical_process() -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify>) {
pub fn rustical_process(
db_url: Option<String>,
) -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify>) {
let port = find_free_port().unwrap();
let token = CancellationToken::new();
let cloned_token = token.clone();
@@ -41,7 +52,7 @@ pub fn rustical_process() -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify
},
Config {
data_store: DataStoreConfig::Sqlite(SqliteDataStoreConfig {
db_url: ":memory:".to_owned(),
db_url: db_url.unwrap_or(":memory:".to_owned()),
run_repairs: true,
skip_broken: false,
}),
@@ -58,6 +69,7 @@ pub fn rustical_process() -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify
caldav: Default::default(),
},
Some(cloned_start_notify),
false,
)
.await
};