use orcher_sdk_core::interceptor::{InterceptorContext, InterceptorHook};
use std::sync::Arc;
#[derive(Clone)]
pub struct InterceptorChain {
interceptors: Vec<Arc<dyn InterceptorHook>>,
}
impl InterceptorChain {
pub fn new() -> Self {
Self {
interceptors: Vec::new(),
}
}
pub fn add(&mut self, interceptor: impl InterceptorHook + 'static) {
self.interceptors.push(Arc::new(interceptor));
}
pub fn add_shared(&mut self, interceptor: Arc<dyn InterceptorHook>) {
self.interceptors.push(interceptor);
}
pub fn is_empty(&self) -> bool {
self.interceptors.is_empty()
}
pub fn len(&self) -> usize {
self.interceptors.len()
}
pub fn before_execution(&self, ctx: &InterceptorContext) {
for interceptor in &self.interceptors {
interceptor.before_execution(ctx);
}
}
pub fn after_execution(&self, ctx: &InterceptorContext, duration_ms: u64) {
for interceptor in self.interceptors.iter().rev() {
interceptor.after_execution(ctx, duration_ms);
}
}
pub fn on_error(&self, ctx: &InterceptorContext, error: &str, duration_ms: u64) {
for interceptor in self.interceptors.iter().rev() {
interceptor.on_error(ctx, error, duration_ms);
}
}
}
impl Default for InterceptorChain {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for InterceptorChain {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let names: Vec<&str> = self.interceptors.iter().map(|i| i.name()).collect();
f.debug_struct("InterceptorChain")
.field("interceptors", &names)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicU32, Ordering};
struct CountingInterceptor {
name: &'static str,
before_count: AtomicU32,
after_count: AtomicU32,
error_count: AtomicU32,
}
impl CountingInterceptor {
fn new(name: &'static str) -> Self {
Self {
name,
before_count: AtomicU32::new(0),
after_count: AtomicU32::new(0),
error_count: AtomicU32::new(0),
}
}
}
impl InterceptorHook for CountingInterceptor {
fn name(&self) -> &str {
self.name
}
fn before_execution(&self, _ctx: &InterceptorContext) {
self.before_count.fetch_add(1, Ordering::SeqCst);
}
fn after_execution(&self, _ctx: &InterceptorContext, _duration_ms: u64) {
self.after_count.fetch_add(1, Ordering::SeqCst);
}
fn on_error(&self, _ctx: &InterceptorContext, _error: &str, _duration_ms: u64) {
self.error_count.fetch_add(1, Ordering::SeqCst);
}
}
#[test]
fn test_chain_empty() {
let chain = InterceptorChain::new();
assert!(chain.is_empty());
assert_eq!(chain.len(), 0);
}
#[test]
fn test_chain_calls_all_interceptors() {
let i1 = Arc::new(CountingInterceptor::new("i1"));
let i2 = Arc::new(CountingInterceptor::new("i2"));
let mut chain = InterceptorChain::new();
chain.add_shared(i1.clone());
chain.add_shared(i2.clone());
let ctx = InterceptorContext::workflow("wf-1", "Test", 1, "default", "q");
chain.before_execution(&ctx);
assert_eq!(i1.before_count.load(Ordering::SeqCst), 1);
assert_eq!(i2.before_count.load(Ordering::SeqCst), 1);
chain.after_execution(&ctx, 100);
assert_eq!(i1.after_count.load(Ordering::SeqCst), 1);
assert_eq!(i2.after_count.load(Ordering::SeqCst), 1);
chain.on_error(&ctx, "test", 50);
assert_eq!(i1.error_count.load(Ordering::SeqCst), 1);
assert_eq!(i2.error_count.load(Ordering::SeqCst), 1);
}
#[test]
fn test_chain_debug() {
let mut chain = InterceptorChain::new();
chain.add(CountingInterceptor::new("logging"));
chain.add(CountingInterceptor::new("metrics"));
let debug = format!("{:?}", chain);
assert!(debug.contains("logging"));
assert!(debug.contains("metrics"));
}
}