add cloud requirements error msg

This commit is contained in:
gt-oai
2026-02-03 21:55:26 +00:00
parent 944541e936
commit 20a669c82e
6 changed files with 247 additions and 45 deletions
@@ -9,6 +9,7 @@ use std::future::Future;
pub struct CloudRequirementsLoader {
// TODO(gt): This should return a Result once we can fail-closed.
fut: Shared<BoxFuture<'static, Option<ConfigRequirementsToml>>>,
warning_fut: Option<Shared<BoxFuture<'static, Option<String>>>>,
}
impl CloudRequirementsLoader {
@@ -18,12 +19,31 @@ impl CloudRequirementsLoader {
{
Self {
fut: fut.boxed().shared(),
warning_fut: None,
}
}
pub fn new_with_warning<F, W>(fut: F, warning_fut: W) -> Self
where
F: Future<Output = Option<ConfigRequirementsToml>> + Send + 'static,
W: Future<Output = Option<String>> + Send + 'static,
{
Self {
fut: fut.boxed().shared(),
warning_fut: Some(warning_fut.boxed().shared()),
}
}
pub async fn get(&self) -> Option<ConfigRequirementsToml> {
self.fut.clone().await
}
pub async fn warning(&self) -> Option<String> {
match &self.warning_fut {
Some(warning_fut) => warning_fut.clone().await,
None => None,
}
}
}
impl fmt::Debug for CloudRequirementsLoader {
@@ -59,4 +79,18 @@ mod tests {
assert_eq!(first, second);
assert_eq!(counter.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn shared_warning_future_runs_once() {
let counter = Arc::new(AtomicUsize::new(0));
let counter_clone = Arc::clone(&counter);
let loader = CloudRequirementsLoader::new_with_warning(async { None }, async move {
counter_clone.fetch_add(1, Ordering::SeqCst);
Some("warning".to_string())
});
let (first, second) = tokio::join!(loader.warning(), loader.warning());
assert_eq!(first, second);
assert_eq!(counter.load(Ordering::SeqCst), 1);
}
}