use std::time::Duration;
use camel_api::{CamelError, Exchange};
use camel_test::CamelTestContext;
use tower::ServiceExt;
pub fn test_rt() -> std::sync::Arc<dyn camel_component_api::RuntimeObservability> {
std::sync::Arc::new(camel_component_api::NoOpComponentContext)
}
pub async fn send_to_direct(
h: &CamelTestContext,
endpoint_uri: &str,
exchange: Exchange,
timeout: Duration,
) {
let deadline = tokio::time::Instant::now() + timeout;
loop {
let producer = {
let ctx = h.ctx().lock().await;
let producer_ctx = ctx.producer_context();
let registry = ctx.registry();
let component = registry
.get("direct")
.expect("direct component not registered");
let endpoint = component
.create_endpoint(endpoint_uri, &*ctx)
.expect("failed to create direct endpoint");
endpoint
.create_producer(test_rt(), &producer_ctx)
.expect("failed to create direct producer")
};
match producer.oneshot(exchange.clone()).await {
Ok(_) => return,
Err(_) if tokio::time::Instant::now() < deadline => {
tokio::time::sleep(Duration::from_millis(20)).await;
}
Err(e) => panic!("failed to send exchange within {timeout:?}: {e}"),
}
}
}
pub async fn send_to_direct_result(
h: &CamelTestContext,
endpoint_uri: &str,
exchange: Exchange,
timeout: Duration,
) -> Result<(), CamelError> {
let deadline = tokio::time::Instant::now() + timeout;
loop {
let producer = {
let ctx = h.ctx().lock().await;
let producer_ctx = ctx.producer_context();
let registry = ctx.registry();
let component = registry
.get("direct")
.expect("direct component not registered");
let endpoint = component
.create_endpoint(endpoint_uri, &*ctx)
.expect("failed to create direct endpoint");
endpoint
.create_producer(test_rt(), &producer_ctx)
.expect("failed to create direct producer")
};
match producer.oneshot(exchange.clone()).await {
Ok(_) => return Ok(()),
Err(e) => {
let is_startup_race = matches!(e, CamelError::EndpointCreationFailed(_))
|| e.to_string().contains("not registered");
if is_startup_race && tokio::time::Instant::now() < deadline {
tokio::time::sleep(Duration::from_millis(20)).await;
continue;
}
return Err(e);
}
}
}
}