use tokio::runtime::Runtime;
#[derive(Debug)]
pub struct TestRuntime {
rt: Runtime,
}
impl TestRuntime {
pub fn new() -> Self {
Self {
rt: Runtime::new().expect(
"Failed to create test runtime. This may indicate insufficient system resources.",
),
}
}
pub fn block_on<F>(&self, future: F) -> F::Output
where
F: std::future::Future,
{
self.rt.block_on(future)
}
}
impl Default for TestRuntime {
fn default() -> Self {
Self::new()
}
}
pub fn test_runtime() -> Runtime {
Runtime::new()
.expect("Failed to create test runtime. This may indicate insufficient system resources.")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_test_runtime_creation() {
let _rt = TestRuntime::new();
}
#[test]
fn test_test_runtime_block_on() {
let rt = TestRuntime::new();
let result = rt.block_on(async { 42 });
assert_eq!(result, 42);
}
#[test]
fn test_test_runtime_default() {
let rt = TestRuntime::default();
let result = rt.block_on(async { "test".to_string() });
assert_eq!(result, "test");
}
}