use std::sync::Arc;
use async_lock::OnceCell;
use heel::HeelBinary;
use crate::{HostProgramPath, ToolRuntimeError};
#[derive(Clone, Default)]
pub struct SharedHeelBinary(Arc<OnceCell<HeelBinary>>);
impl SharedHeelBinary {
pub(crate) async fn provide(&self, heel: &HostProgramPath) -> Result<(), ToolRuntimeError> {
let path = heel.path().to_path_buf();
self.get_with(move || HeelBinary::verify(path))
.await
.map(drop)
.map_err(ToolRuntimeError::Heel)
}
#[cfg(not(any(test, feature = "test-heel")))]
pub(crate) fn get(&self) -> std::future::Ready<Result<HeelBinary, ToolRuntimeError>> {
std::future::ready(self.0.get().cloned().ok_or_else(|| {
ToolRuntimeError::NotInitialized(
"the heel binary: initialize the terminal's tools".into(),
)
}))
}
#[cfg(any(test, feature = "test-heel"))]
pub(crate) async fn get(&self) -> Result<HeelBinary, ToolRuntimeError> {
self.0
.get_or_try_init(crate::test_heel::pinned)
.await
.cloned()
}
async fn get_with(
&self,
verify: impl FnOnce() -> heel::Result<HeelBinary> + Send + 'static,
) -> heel::Result<HeelBinary> {
self.0
.get_or_try_init(|| blocking::unblock(verify))
.await
.cloned()
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicUsize, Ordering};
use super::*;
fn counted(
calls: &Arc<AtomicUsize>,
pinned: &HeelBinary,
) -> impl FnOnce() -> heel::Result<HeelBinary> + use<> {
let (calls, pinned) = (calls.clone(), pinned.clone());
move || {
calls.fetch_add(1, Ordering::SeqCst);
Ok(pinned)
}
}
#[tokio::test]
async fn verifies_once_for_every_clone() {
let pinned = crate::test_heel::pinned().await.unwrap();
let calls = Arc::new(AtomicUsize::new(0));
let shared = SharedHeelBinary::default();
let first = shared.get_with(counted(&calls, &pinned)).await.unwrap();
let second = shared
.clone()
.get_with(counted(&calls, &pinned))
.await
.unwrap();
let third = shared.get_with(counted(&calls, &pinned)).await.unwrap();
assert_eq!(calls.load(Ordering::SeqCst), 1);
assert_eq!(first, second);
assert_eq!(first, third);
}
#[tokio::test]
async fn concurrent_first_use_verifies_once() {
let pinned = crate::test_heel::pinned().await.unwrap();
let calls = Arc::new(AtomicUsize::new(0));
let shared = SharedHeelBinary::default();
let (a, (b, c)) = futures_lite::future::zip(
shared.get_with(counted(&calls, &pinned)),
futures_lite::future::zip(
shared.clone().get_with(counted(&calls, &pinned)),
shared.clone().get_with(counted(&calls, &pinned)),
),
)
.await;
a.unwrap();
b.unwrap();
c.unwrap();
assert_eq!(calls.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn a_failure_is_reported_and_not_cached() {
let pinned = crate::test_heel::pinned().await.unwrap();
let calls = Arc::new(AtomicUsize::new(0));
let shared = SharedHeelBinary::default();
let failing = |calls: Arc<AtomicUsize>| {
move || {
calls.fetch_add(1, Ordering::SeqCst);
HeelBinary::verify("/nonexistent/heel")
}
};
let error = shared.get_with(failing(calls.clone())).await.unwrap_err();
assert!(error.to_string().contains("the heel binary is missing"));
shared.get_with(failing(calls.clone())).await.unwrap_err();
assert_eq!(calls.load(Ordering::SeqCst), 2);
shared.get_with(counted(&calls, &pinned)).await.unwrap();
shared.get_with(counted(&calls, &pinned)).await.unwrap();
assert_eq!(calls.load(Ordering::SeqCst), 3);
}
#[tokio::test]
async fn the_distributions_heel_is_provided_and_shared() {
let distribution = crate::test_heel::distribution();
let target = crate::ToolTarget::native().unwrap();
let cache = tempfile::tempdir().unwrap();
let heel = distribution
.prepare_host_program(crate::HostProgram::Heel, &target, cache.path())
.await
.unwrap();
let shared = SharedHeelBinary::default();
shared.provide(&heel).await.unwrap();
assert_eq!(shared.clone().get().await.unwrap().as_path(), heel.path());
}
}