use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParallelOrchestratorConfig {
pub max_concurrent_tasks: Option<usize>,
pub step_timeout: Option<Duration>,
pub max_step_remediations: usize,
#[serde(default = "default_true")]
pub enable_validation: bool,
}
fn default_true() -> bool {
true
}
impl Default for ParallelOrchestratorConfig {
fn default() -> Self {
Self::new()
}
}
impl ParallelOrchestratorConfig {
pub fn new() -> Self {
Self {
max_concurrent_tasks: None,
step_timeout: None,
max_step_remediations: 3,
enable_validation: true,
}
}
pub fn with_max_concurrent_tasks(mut self, max: usize) -> Self {
self.max_concurrent_tasks = Some(max);
self
}
pub fn with_step_timeout(mut self, timeout: Duration) -> Self {
self.step_timeout = Some(timeout);
self
}
pub fn with_unlimited_concurrency(mut self) -> Self {
self.max_concurrent_tasks = None;
self
}
pub fn with_no_timeout(mut self) -> Self {
self.step_timeout = None;
self
}
pub fn with_max_step_remediations(mut self, max: usize) -> Self {
self.max_step_remediations = max;
self
}
pub fn with_validation(mut self, enable: bool) -> Self {
self.enable_validation = enable;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = ParallelOrchestratorConfig::default();
assert!(config.max_concurrent_tasks.is_none());
assert!(config.step_timeout.is_none());
}
#[test]
fn test_new_config() {
let config = ParallelOrchestratorConfig::new();
assert!(config.max_concurrent_tasks.is_none());
assert!(config.step_timeout.is_none());
}
#[test]
fn test_with_concurrency_limit() {
let config = ParallelOrchestratorConfig::new().with_max_concurrent_tasks(5);
assert_eq!(config.max_concurrent_tasks, Some(5));
}
#[test]
fn test_with_timeout() {
let timeout = Duration::from_secs(300);
let config = ParallelOrchestratorConfig::new().with_step_timeout(timeout);
assert_eq!(config.step_timeout, Some(timeout));
}
#[test]
fn test_builder_chain() {
let config = ParallelOrchestratorConfig::new()
.with_max_concurrent_tasks(10)
.with_step_timeout(Duration::from_secs(600));
assert_eq!(config.max_concurrent_tasks, Some(10));
assert_eq!(config.step_timeout, Some(Duration::from_secs(600)));
}
#[test]
fn test_with_unlimited_concurrency() {
let config = ParallelOrchestratorConfig::new()
.with_max_concurrent_tasks(5)
.with_unlimited_concurrency();
assert!(config.max_concurrent_tasks.is_none());
}
#[test]
fn test_with_no_timeout() {
let config = ParallelOrchestratorConfig::new()
.with_step_timeout(Duration::from_secs(300))
.with_no_timeout();
assert!(config.step_timeout.is_none());
}
#[test]
fn test_config_clone() {
let config1 = ParallelOrchestratorConfig::new()
.with_max_concurrent_tasks(5)
.with_step_timeout(Duration::from_secs(300));
let config2 = config1.clone();
assert_eq!(config2.max_concurrent_tasks, Some(5));
assert_eq!(config2.step_timeout, Some(Duration::from_secs(300)));
}
#[test]
fn test_config_debug() {
let config = ParallelOrchestratorConfig::new().with_max_concurrent_tasks(5);
let debug_str = format!("{:?}", config);
assert!(debug_str.contains("max_concurrent_tasks"));
assert!(debug_str.contains("5"));
}
#[test]
fn test_enable_validation_default() {
let config = ParallelOrchestratorConfig::new();
assert!(config.enable_validation); }
#[test]
fn test_with_validation() {
let config = ParallelOrchestratorConfig::new().with_validation(false);
assert!(!config.enable_validation);
let config2 = ParallelOrchestratorConfig::new().with_validation(true);
assert!(config2.enable_validation);
}
#[test]
fn test_validation_in_builder_chain() {
let config = ParallelOrchestratorConfig::new()
.with_max_concurrent_tasks(10)
.with_validation(false)
.with_step_timeout(Duration::from_secs(600));
assert_eq!(config.max_concurrent_tasks, Some(10));
assert!(!config.enable_validation);
assert_eq!(config.step_timeout, Some(Duration::from_secs(600)));
}
}