Skip to main content

wae_testing/environment/
config.rs

1//! 测试环境配置模块
2
3use std::{collections::HashMap, time::Duration};
4
5/// 测试环境配置
6#[derive(Debug, Clone)]
7pub struct TestEnvConfig {
8    /// 环境名称
9    pub name: String,
10    /// 是否启用日志
11    pub enable_logging: bool,
12    /// 是否启用追踪
13    pub enable_tracing: bool,
14    /// 超时时间
15    pub default_timeout: Duration,
16    /// 自定义配置
17    pub custom: HashMap<String, String>,
18}
19
20impl Default for TestEnvConfig {
21    fn default() -> Self {
22        Self {
23            name: "test".to_string(),
24            enable_logging: true,
25            enable_tracing: false,
26            default_timeout: Duration::from_secs(30),
27            custom: HashMap::new(),
28        }
29    }
30}
31
32impl TestEnvConfig {
33    /// 创建新的测试环境配置
34    pub fn new() -> Self {
35        Self::default()
36    }
37
38    /// 设置环境名称
39    pub fn name(mut self, name: impl Into<String>) -> Self {
40        self.name = name.into();
41        self
42    }
43
44    /// 启用日志
45    pub fn with_logging(mut self, enable: bool) -> Self {
46        self.enable_logging = enable;
47        self
48    }
49
50    /// 启用追踪
51    pub fn with_tracing(mut self, enable: bool) -> Self {
52        self.enable_tracing = enable;
53        self
54    }
55
56    /// 设置超时时间
57    pub fn timeout(mut self, timeout: Duration) -> Self {
58        self.default_timeout = timeout;
59        self
60    }
61
62    /// 添加自定义配置
63    pub fn custom(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
64        self.custom.insert(key.into(), value.into());
65        self
66    }
67}
68
69/// 测试服务配置
70///
71/// 定义测试环境中需要启动的服务配置,支持多服务集成测试。
72#[derive(Debug, Clone)]
73pub struct TestServiceConfig {
74    /// 服务名称
75    pub name: String,
76    /// 服务是否启用
77    pub enabled: bool,
78    /// 服务启动超时时间
79    pub startup_timeout: Duration,
80    /// 服务特定配置
81    pub config: HashMap<String, String>,
82}
83
84impl TestServiceConfig {
85    /// 创建新的测试服务配置
86    pub fn new(name: impl Into<String>) -> Self {
87        Self { name: name.into(), enabled: true, startup_timeout: Duration::from_secs(30), config: HashMap::new() }
88    }
89
90    /// 设置服务启用状态
91    pub fn enabled(mut self, enabled: bool) -> Self {
92        self.enabled = enabled;
93        self
94    }
95
96    /// 设置服务启动超时时间
97    pub fn startup_timeout(mut self, timeout: Duration) -> Self {
98        self.startup_timeout = timeout;
99        self
100    }
101
102    /// 添加服务配置项
103    pub fn config(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
104        self.config.insert(key.into(), value.into());
105        self
106    }
107}