Skip to main content

camel_api/
producer.rs

1//! Producer context for dependency injection.
2//!
3//! This module provides [`ProducerContext`] for holding shared dependencies
4//! that producers need access to, such as the runtime command/query handle.
5
6use crate::runtime::RuntimeHandle;
7use std::sync::Arc;
8
9/// Context provided to producers for dependency injection.
10///
11/// `ProducerContext` holds references to shared infrastructure components
12/// that producers may need access to during message production.
13#[derive(Clone)]
14pub struct ProducerContext {
15    runtime: Option<Arc<dyn RuntimeHandle>>,
16}
17
18impl ProducerContext {
19    /// Creates a new empty `ProducerContext`.
20    pub fn new() -> Self {
21        Self { runtime: None }
22    }
23
24    /// Attaches a runtime command/query handle.
25    pub fn with_runtime(mut self, runtime: Arc<dyn RuntimeHandle>) -> Self {
26        self.runtime = Some(runtime);
27        self
28    }
29
30    /// Returns the runtime command/query handle, if configured.
31    pub fn runtime(&self) -> Option<&Arc<dyn RuntimeHandle>> {
32        self.runtime.as_ref()
33    }
34}
35
36impl Default for ProducerContext {
37    fn default() -> Self {
38        Self::new()
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::CamelError;
46    use crate::runtime::{
47        RuntimeCommand, RuntimeCommandBus, RuntimeCommandResult, RuntimeQuery, RuntimeQueryBus,
48        RuntimeQueryResult,
49    };
50    use async_trait::async_trait;
51
52    struct NoopRuntime;
53
54    #[async_trait]
55    impl RuntimeCommandBus for NoopRuntime {
56        async fn execute(&self, _cmd: RuntimeCommand) -> Result<RuntimeCommandResult, CamelError> {
57            Ok(RuntimeCommandResult::Accepted)
58        }
59    }
60
61    #[async_trait]
62    impl RuntimeQueryBus for NoopRuntime {
63        async fn ask(&self, _query: RuntimeQuery) -> Result<RuntimeQueryResult, CamelError> {
64            Ok(RuntimeQueryResult::Routes { route_ids: vec![] })
65        }
66    }
67
68    #[test]
69    fn producer_context_new_is_empty() {
70        let ctx = ProducerContext::new();
71        assert!(ctx.runtime().is_none());
72    }
73
74    #[test]
75    fn producer_context_default_is_empty() {
76        let ctx = ProducerContext::default();
77        assert!(ctx.runtime().is_none());
78    }
79
80    #[test]
81    fn producer_context_with_runtime_sets_handle() {
82        let runtime: Arc<dyn RuntimeHandle> = Arc::new(NoopRuntime);
83        let ctx = ProducerContext::new().with_runtime(runtime.clone());
84
85        let attached = ctx.runtime().expect("runtime should be set");
86        assert!(Arc::ptr_eq(attached, &runtime));
87    }
88
89    #[test]
90    fn producer_context_clone_keeps_same_runtime_handle() {
91        let runtime: Arc<dyn RuntimeHandle> = Arc::new(NoopRuntime);
92        let ctx = ProducerContext::new().with_runtime(runtime.clone());
93        let cloned = ctx.clone();
94
95        assert!(Arc::ptr_eq(cloned.runtime().unwrap(), &runtime));
96    }
97}