openlark_core/testing/
mock_context.rs1use tokio::runtime::Runtime;
6
7#[derive(Debug)]
18pub struct TestRuntime {
19 rt: Runtime,
20}
21
22impl TestRuntime {
23 pub fn new() -> Self {
27 Self {
28 rt: Runtime::new().expect(
29 "Failed to create test runtime. This may indicate insufficient system resources.",
30 ),
31 }
32 }
33
34 pub fn block_on<F>(&self, future: F) -> F::Output
36 where
37 F: std::future::Future,
38 {
39 self.rt.block_on(future)
40 }
41}
42
43impl Default for TestRuntime {
44 fn default() -> Self {
45 Self::new()
46 }
47}
48
49pub fn test_runtime() -> Runtime {
57 Runtime::new()
58 .expect("Failed to create test runtime. This may indicate insufficient system resources.")
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_test_runtime_creation() {
67 let _rt = TestRuntime::new();
68 }
70
71 #[test]
72 fn test_test_runtime_block_on() {
73 let rt = TestRuntime::new();
74 let result = rt.block_on(async { 42 });
75 assert_eq!(result, 42);
76 }
77
78 #[test]
79 fn test_test_runtime_default() {
80 let rt = TestRuntime::default();
81 let result = rt.block_on(async { "test".to_string() });
82 assert_eq!(result, "test");
83 }
84}