use mecha10_runtime::runtime::*;
use anyhow::Result;
use async_trait::async_trait;
use mecha10_runtime::node::NodeRunner;
use mecha10_runtime::{LogFormat, RestartPolicy};
use std::time::Duration;
struct TestNode {
name: String,
}
#[async_trait]
impl NodeRunner for TestNode {
fn name(&self) -> &str {
&self.name
}
async fn run(&mut self) -> Result<()> {
tokio::time::sleep(Duration::from_millis(100)).await;
Ok(())
}
}
#[tokio::test]
async fn test_runtime_builder() {
let runtime = Runtime::builder()
.log_level("debug")
.log_format(LogFormat::Compact)
.health_check_interval(Duration::from_secs(60))
.shutdown_timeout(Duration::from_secs(5))
.restart_policy(RestartPolicy::Never)
.build();
assert!(runtime.launcher().is_none());
}
#[tokio::test]
async fn test_runtime_with_launcher() {
let runtime = Runtime::builder().with_launcher(true).build();
assert!(runtime.launcher().is_some());
}
#[tokio::test]
async fn test_runtime_run_node() {
let mut runtime = Runtime::builder().shutdown_timeout(Duration::from_millis(500)).build();
let node = Box::new(TestNode {
name: "test_node".to_string(),
});
let result = tokio::time::timeout(Duration::from_secs(1), runtime.run_node("test_node", node)).await;
assert!(result.is_ok());
assert!(result.unwrap().is_ok());
}
#[tokio::test]
async fn test_runtime_run_multiple_nodes() {
let mut runtime = Runtime::builder().shutdown_timeout(Duration::from_millis(500)).build();
let nodes: Vec<Box<dyn NodeRunner>> = vec![
Box::new(TestNode {
name: "node1".to_string(),
}),
Box::new(TestNode {
name: "node2".to_string(),
}),
Box::new(TestNode {
name: "node3".to_string(),
}),
];
let result = tokio::time::timeout(Duration::from_secs(1), runtime.run_nodes(nodes)).await;
assert!(result.is_ok());
assert!(result.unwrap().is_ok());
}