#[cfg(feature = "sqlite")]
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use alkstore::StopToken;
use alkgit::{RepoRecord, Visibility};
use crate::reactor::Reactor;
pub(crate) async fn wait_until(pred: impl Fn() -> bool) {
let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
while !pred() && tokio::time::Instant::now() < deadline {
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert!(
pred(),
"the awaited condition did not land inside the deadline"
);
}
pub(crate) fn record(id: &str) -> RepoRecord {
RepoRecord {
repo_id: id.to_owned(),
visibility: Visibility::Public,
grants: Default::default(),
}
}
pub(crate) fn private_record(id: &str) -> RepoRecord {
RepoRecord {
repo_id: id.to_owned(),
visibility: Visibility::Private,
grants: Default::default(),
}
}
pub(crate) fn spawn_wake_runner(
reactor: std::sync::Arc<Reactor>,
) -> (StopToken, tokio::task::JoinHandle<()>) {
let stop = StopToken::new();
let join = tokio::spawn({
let stop = stop.clone();
async move {
reactor
.run_wake_runner(stop)
.await
.expect("the wake runner ran clean");
}
});
(stop, join)
}
pub(crate) async fn stop_and_join(stop: StopToken, join: tokio::task::JoinHandle<()>) {
stop.cancel();
tokio::time::timeout(Duration::from_secs(5), join)
.await
.expect("the run ended inside the deadline")
.expect("the run ended clean");
}
#[cfg(all(test, feature = "sqlite"))]
pub(crate) fn unique_db_path(fixture: &str, tag: &str) -> std::path::PathBuf {
static COUNTER: AtomicU64 = AtomicU64::new(0);
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
std::env::temp_dir().join(format!(
"alkgit-substrate-{fixture}-{}-{n}-{tag}.db",
std::process::id()
))
}
pub(crate) fn scratch_dir(fixture: &str, tag: &str) -> std::path::PathBuf {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos();
let dir = std::env::temp_dir().join(format!(
"alkgit-substrate-{fixture}-{tag}-{}-{nanos}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap_or_default();
dir
}
pub(crate) fn mem_registry_pair() -> (
std::sync::Arc<dyn crate::registry::RegistryWrite>,
std::sync::Arc<dyn alkgit::GitRegistryStore>,
) {
use crate::registry::{CachedRegistryProvider, InMemoryRegistry, RegistryBackend};
let registry = std::sync::Arc::new(InMemoryRegistry::new());
let provider = std::sync::Arc::new(CachedRegistryProvider::new());
let store = RegistryBackend::with_cached_provider(registry.clone(), provider);
(registry, std::sync::Arc::new(store))
}