wae-testing 0.0.2

WAE Testing - 测试工具集,断言、Mock、Fixture
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! 测试环境管理模块
//!
//! 提供完整的测试生命周期管理、多服务集成测试支持和容器化测试环境。

use parking_lot::RwLock;
use std::{
    future::Future,
    sync::Arc,
    time::{Duration, Instant},
};
use wae_types::{WaeError, WaeErrorKind, WaeResult as TestingResult};

mod builder;
mod config;
mod hooks;
mod state;

pub use builder::TestEnvBuilder;
pub use config::{TestEnvConfig, TestServiceConfig};
pub use hooks::{AsyncTestLifecycleHook, TestLifecycleHook};
pub use state::TestEnvState;

/// 测试环境管理器
///
/// 提供完整的测试生命周期管理,支持同步和异步钩子函数,多服务集成测试,以及容器化测试环境。
pub struct TestEnv {
    /// 配置
    config: TestEnvConfig,
    /// 状态
    state: Arc<RwLock<TestEnvState>>,
    /// 创建时间
    created_at: Instant,
    /// 初始化完成时间
    initialized_at: Arc<RwLock<Option<Instant>>>,
    /// 同步生命周期钩子
    lifecycle_hooks: Arc<RwLock<Vec<Box<dyn TestLifecycleHook>>>>,
    /// 异步生命周期钩子
    async_lifecycle_hooks: Arc<RwLock<Vec<Box<dyn AsyncTestLifecycleHook>>>>,
    /// 清理函数列表
    #[allow(clippy::type_complexity)]
    cleanup_handlers: Arc<RwLock<Vec<Box<dyn Fn() + Send + Sync>>>>,
    /// 异步清理函数列表
    #[allow(clippy::type_complexity)]
    async_cleanup_handlers: Arc<RwLock<Vec<Box<dyn Fn() -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>>>>,
    /// 存储的数据
    storage: Arc<RwLock<std::collections::HashMap<String, Box<dyn std::any::Any + Send + Sync>>>>,
    /// 测试服务配置
    services: Arc<RwLock<std::collections::HashMap<String, TestServiceConfig>>>,
}

impl std::fmt::Debug for TestEnv {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TestEnv")
            .field("config", &self.config)
            .field("state", &self.state)
            .field("created_at", &self.created_at)
            .field("initialized_at", &self.initialized_at)
            .field("services", &self.services)
            .finish()
    }
}

impl TestEnv {
    /// 创建新的测试环境
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::{TestEnv, TestEnvConfig};
    ///
    /// let config = TestEnvConfig::default();
    /// let env = TestEnv::new(config);
    /// ```
    pub fn new(config: TestEnvConfig) -> Self {
        Self {
            config,
            state: Arc::new(RwLock::new(TestEnvState::Uninitialized)),
            created_at: Instant::now(),
            initialized_at: Arc::new(RwLock::new(None)),
            lifecycle_hooks: Arc::new(RwLock::new(Vec::new())),
            async_lifecycle_hooks: Arc::new(RwLock::new(Vec::new())),
            cleanup_handlers: Arc::new(RwLock::new(Vec::new())),
            async_cleanup_handlers: Arc::new(RwLock::new(Vec::new())),
            storage: Arc::new(RwLock::new(std::collections::HashMap::new())),
            services: Arc::new(RwLock::new(std::collections::HashMap::new())),
        }
    }

    /// 创建默认测试环境
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::TestEnv;
    ///
    /// let env = TestEnv::default_env();
    /// ```
    pub fn default_env() -> Self {
        Self::new(TestEnvConfig::default())
    }

    /// 初始化测试环境
    ///
    /// 按顺序执行所有 `before_setup` 钩子、初始化环境、然后执行所有 `after_setup` 钩子。
    ///
    /// # Errors
    ///
    /// 如果环境已初始化或任何钩子执行失败,将返回 [`WaeError`] 错误。
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::TestEnv;
    ///
    /// let env = TestEnv::default_env();
    /// env.setup().unwrap();
    /// ```
    pub fn setup(&self) -> TestingResult<()> {
        {
            let mut state = self.state.write();
            if *state != TestEnvState::Uninitialized {
                return Err(WaeError::new(WaeErrorKind::EnvironmentError {
                    reason: "Environment already initialized".to_string(),
                }));
            }
            *state = TestEnvState::Initializing;
        }

        let result = (|| {
            for hook in self.lifecycle_hooks.read().iter() {
                hook.before_setup(self)?;
            }

            for hook in self.lifecycle_hooks.read().iter() {
                hook.after_setup(self)?;
            }

            Ok(())
        })();

        let mut state = self.state.write();
        match result {
            Ok(_) => {
                *state = TestEnvState::Initialized;
                *self.initialized_at.write() = Some(Instant::now());
                Ok(())
            }
            Err(e) => {
                *state = TestEnvState::Uninitialized;
                Err(e)
            }
        }
    }

    /// 异步初始化测试环境
    ///
    /// 异步执行所有生命周期钩子函数,适合需要异步初始化的场景。
    ///
    /// # Errors
    ///
    /// 如果环境已初始化或任何钩子执行失败,将返回 [`WaeError`] 错误。
    pub async fn setup_async(&self) -> TestingResult<()> {
        {
            let mut state = self.state.write();
            if *state != TestEnvState::Uninitialized {
                return Err(WaeError::new(WaeErrorKind::EnvironmentError {
                    reason: "Environment already initialized".to_string(),
                }));
            }
            *state = TestEnvState::Initializing;
        }

        let result = (async {
            for hook in self.lifecycle_hooks.read().iter() {
                hook.before_setup(self)?;
            }

            #[allow(clippy::await_holding_lock)]
            for hook in self.async_lifecycle_hooks.read().iter() {
                hook.before_setup_async(self).await?;
            }

            #[allow(clippy::await_holding_lock)]
            for hook in self.async_lifecycle_hooks.read().iter() {
                hook.after_setup_async(self).await?;
            }

            for hook in self.lifecycle_hooks.read().iter() {
                hook.after_setup(self)?;
            }

            Ok(())
        })
        .await;

        let mut state = self.state.write();
        match result {
            Ok(_) => {
                *state = TestEnvState::Initialized;
                *self.initialized_at.write() = Some(Instant::now());
                Ok(())
            }
            Err(e) => {
                *state = TestEnvState::Uninitialized;
                Err(e)
            }
        }
    }

    /// 清理测试环境
    ///
    /// 按顺序执行所有 `before_teardown` 钩子、清理资源、然后执行所有 `after_teardown` 钩子。
    ///
    /// # Errors
    ///
    /// 如果环境未初始化或任何钩子执行失败,将返回 [`WaeError`] 错误。
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::TestEnv;
    ///
    /// let env = TestEnv::default_env();
    /// env.setup().unwrap();
    /// env.teardown().unwrap();
    /// ```
    pub fn teardown(&self) -> TestingResult<()> {
        {
            let mut state = self.state.write();
            if *state != TestEnvState::Initialized {
                return Err(WaeError::new(WaeErrorKind::EnvironmentError {
                    reason: "Environment not initialized".to_string(),
                }));
            }
            *state = TestEnvState::Destroying;
        }

        let result = (|| {
            for hook in self.lifecycle_hooks.read().iter() {
                hook.before_teardown(self)?;
            }

            let handlers = self.cleanup_handlers.write();
            for handler in handlers.iter().rev() {
                handler();
            }

            self.storage.write().clear();

            for hook in self.lifecycle_hooks.read().iter() {
                hook.after_teardown(self)?;
            }

            Ok(())
        })();

        let mut state = self.state.write();
        *state = TestEnvState::Destroyed;
        result
    }

    /// 异步清理测试环境
    ///
    /// 异步执行所有清理操作,适合需要异步清理的场景。
    ///
    /// # Errors
    ///
    /// 如果环境未初始化或任何钩子执行失败,将返回 [`WaeError`] 错误。
    pub async fn teardown_async(&self) -> TestingResult<()> {
        {
            let mut state = self.state.write();
            if *state != TestEnvState::Initialized {
                return Err(WaeError::new(WaeErrorKind::EnvironmentError {
                    reason: "Environment not initialized".to_string(),
                }));
            }
            *state = TestEnvState::Destroying;
        }

        let result = (async {
            for hook in self.lifecycle_hooks.read().iter() {
                hook.before_teardown(self)?;
            }

            #[allow(clippy::await_holding_lock)]
            for hook in self.async_lifecycle_hooks.read().iter() {
                hook.before_teardown_async(self).await?;
            }

            #[allow(clippy::await_holding_lock)]
            {
                let handlers = self.async_cleanup_handlers.write();
                for handler in handlers.iter().rev() {
                    handler().await;
                }
            }

            {
                let handlers = self.cleanup_handlers.write();
                for handler in handlers.iter().rev() {
                    handler();
                }
            }

            self.storage.write().clear();

            #[allow(clippy::await_holding_lock)]
            for hook in self.async_lifecycle_hooks.read().iter() {
                hook.after_teardown_async(self).await?;
            }

            for hook in self.lifecycle_hooks.read().iter() {
                hook.after_teardown(self)?;
            }

            Ok(())
        })
        .await;

        let mut state = self.state.write();
        *state = TestEnvState::Destroyed;
        result
    }

    /// 获取环境状态
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::{TestEnv, TestEnvState};
    ///
    /// let env = TestEnv::default_env();
    /// assert_eq!(env.state(), TestEnvState::Uninitialized);
    /// ```
    pub fn state(&self) -> TestEnvState {
        self.state.read().clone()
    }

    /// 获取环境运行时间
    ///
    /// 返回从环境创建到现在经过的时间。
    pub fn elapsed(&self) -> Duration {
        self.created_at.elapsed()
    }

    /// 获取环境初始化后运行的时间
    ///
    /// 如果环境尚未初始化,返回 [`None`]。
    pub fn initialized_elapsed(&self) -> Option<Duration> {
        self.initialized_at.read().map(|t| t.elapsed())
    }

    /// 注册同步生命周期钩子
    ///
    /// 添加一个同步钩子函数,在测试环境的各个生命周期阶段执行。
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::TestEnv;
    ///
    /// struct MyHook;
    ///
    /// impl wae_testing::TestLifecycleHook for MyHook {
    ///     fn after_setup(&self, _env: &TestEnv) -> wae_testing::TestingResult<()> {
    ///         println!("Environment setup complete!");
    ///         Ok(())
    ///     }
    /// }
    ///
    /// let env = TestEnv::default_env();
    /// env.add_lifecycle_hook(MyHook);
    /// ```
    pub fn add_lifecycle_hook<H>(&self, hook: H)
    where
        H: TestLifecycleHook + 'static,
    {
        self.lifecycle_hooks.write().push(Box::new(hook));
    }

    /// 注册异步生命周期钩子
    ///
    /// 添加一个异步钩子函数,在测试环境的各个生命周期阶段异步执行。
    pub fn add_async_lifecycle_hook<H>(&self, hook: H)
    where
        H: AsyncTestLifecycleHook + 'static,
    {
        self.async_lifecycle_hooks.write().push(Box::new(hook));
    }

    /// 注册清理函数
    ///
    /// 添加一个同步清理函数,在测试环境清理时执行。清理函数按注册的逆序执行。
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::TestEnv;
    ///
    /// let env = TestEnv::default_env();
    /// env.on_cleanup(|| println!("Cleaning up!"));
    /// ```
    pub fn on_cleanup<F>(&self, handler: F)
    where
        F: Fn() + Send + Sync + 'static,
    {
        self.cleanup_handlers.write().push(Box::new(handler));
    }

    /// 注册异步清理函数
    ///
    /// 添加一个异步清理函数,在测试环境清理时异步执行。
    pub fn on_cleanup_async<F, Fut>(&self, handler: F)
    where
        F: Fn() -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        self.async_cleanup_handlers.write().push(Box::new(move || Box::pin(handler())));
    }

    /// 存储数据
    ///
    /// 在测试环境中存储任意类型的数据。
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::TestEnv;
    ///
    /// let env = TestEnv::default_env();
    /// env.set("test_key", "test_value");
    /// ```
    pub fn set<T: 'static + Send + Sync>(&self, key: &str, value: T) {
        self.storage.write().insert(key.to_string(), Box::new(value));
    }

    /// 获取数据
    ///
    /// 从测试环境中获取之前存储的数据。
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::TestEnv;
    ///
    /// let env = TestEnv::default_env();
    /// env.set("test_key", "test_value".to_string());
    /// let value: Option<String> = env.get("test_key");
    /// assert_eq!(value, Some("test_value".to_string()));
    /// ```
    pub fn get<T: 'static + Clone>(&self, key: &str) -> Option<T> {
        let storage = self.storage.read();
        storage.get(key).and_then(|v| v.downcast_ref::<T>().cloned())
    }

    /// 移除数据
    ///
    /// 从测试环境中移除并返回之前存储的数据。
    pub fn remove<T: 'static>(&self, key: &str) -> Option<T> {
        let mut storage = self.storage.write();
        storage.remove(key).and_then(|v| v.downcast::<T>().ok()).map(|v| *v)
    }

    /// 检查是否存在指定键的数据
    pub fn has(&self, key: &str) -> bool {
        self.storage.read().contains_key(key)
    }

    /// 获取配置
    ///
    /// # Examples
    ///
    /// ```
    /// use wae_testing::TestEnv;
    ///
    /// let env = TestEnv::default_env();
    /// let config = env.config();
    /// assert_eq!(config.name, "test");
    /// ```
    pub fn config(&self) -> &TestEnvConfig {
        &self.config
    }

    /// 添加测试服务配置
    ///
    /// 向测试环境中添加一个服务配置,用于多服务集成测试。
    pub fn add_service(&self, service_config: TestServiceConfig) {
        self.services.write().insert(service_config.name.clone(), service_config);
    }

    /// 获取测试服务配置
    ///
    /// 根据服务名称获取服务配置。
    pub fn get_service(&self, name: &str) -> Option<TestServiceConfig> {
        self.services.read().get(name).cloned()
    }

    /// 获取所有启用的服务
    ///
    /// 返回所有已启用的服务配置列表。
    pub fn enabled_services(&self) -> Vec<TestServiceConfig> {
        self.services.read().values().filter(|s| s.enabled).cloned().collect()
    }

    /// 使用 fixture 运行测试
    ///
    /// 自动管理测试环境的初始化和清理,执行测试函数。
    ///
    /// # Errors
    ///
    /// 如果环境初始化、测试执行或清理失败,将返回 [`WaeError`] 错误。
    pub async fn with_fixture<F, R>(&self, fixture: F) -> TestingResult<R>
    where
        F: FnOnce() -> TestingResult<R>,
    {
        self.setup()?;

        let result = fixture();

        self.teardown()?;

        result
    }

    /// 运行异步测试
    ///
    /// 自动管理测试环境的初始化和清理,执行异步测试函数。
    ///
    /// # Errors
    ///
    /// 如果环境初始化、测试执行或清理失败,将返回 [`WaeError`] 错误。
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use wae_testing::TestEnv;
    ///
    /// let env = TestEnv::default_env();
    /// env.run_test(|| async { Ok(()) }).await.unwrap();
    /// ```
    pub async fn run_test<F, Fut>(&self, test: F) -> TestingResult<()>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = TestingResult<()>>,
    {
        self.setup()?;

        let result = test().await;

        self.teardown()?;

        result
    }

    /// 运行带异步生命周期的测试
    ///
    /// 使用异步初始化和清理运行测试,适合需要异步操作的测试场景。
    ///
    /// # Errors
    ///
    /// 如果环境初始化、测试执行或清理失败,将返回 [`WaeError`] 错误。
    pub async fn run_test_async<F, Fut>(&self, test: F) -> TestingResult<()>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = TestingResult<()>>,
    {
        self.setup_async().await?;

        let result = test().await;

        self.teardown_async().await?;

        result
    }
}

impl Drop for TestEnv {
    fn drop(&mut self) {
        let state = self.state.read().clone();
        if state == TestEnvState::Initialized {
            let _ = self.teardown();
        }
    }
}

/// 创建测试环境
///
/// 便捷函数,创建一个默认配置的测试环境。
///
/// # Examples
///
/// ```
/// use wae_testing::create_test_env;
///
/// let env = create_test_env();
/// ```
pub fn create_test_env() -> TestEnv {
    TestEnv::default_env()
}

/// 使用配置创建测试环境
///
/// 便捷函数,使用指定配置创建测试环境。
///
/// # Examples
///
/// ```
/// use wae_testing::{TestEnvConfig, create_test_env_with_config};
///
/// let config = TestEnvConfig::default();
/// let env = create_test_env_with_config(config);
/// ```
pub fn create_test_env_with_config(config: TestEnvConfig) -> TestEnv {
    TestEnv::new(config)
}