-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
53 lines (44 loc) · 1.5 KB
/
lib.rs
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
pub mod handler;
pub mod module_runner;
pub mod executor;
pub mod command;
#[cfg(test)]
mod tests {
use std::time::Duration;
use crossbeam_channel::unbounded;
use log::{info, warn};
use crate::executor::Executor;
use crate::handler::Handler;
use crate::module_runner::ModuleRunner;
#[derive(Debug, Clone)]
struct TestExecutor {
name: String
}
impl Executor<u32> for TestExecutor {
fn execute(&self) -> Option<u32> {
info!("{} is currently working....", self.name);
Some(0)
}
fn on_stop(&mut self) {
warn!("Gracefully stopped {}", self.name)
}
}
#[tokio::test]
async fn create_handler() {
tracing_subscriber::fmt::init();
let mut handler = Handler::new();
let (sender, _receiver) = unbounded::<u32>();
let module = ModuleRunner::<u32>::new(Box::new(TestExecutor {name: String::from("Slow worker")}), Duration::from_secs(1), sender.clone());
let module2 = ModuleRunner::<u32>::new(Box::new(TestExecutor {name: String::from("Faster worker")}), Duration::from_secs(2), sender);
handler.spawn(0, module);
for i in 1..20 {
handler.spawn(i, module2.clone());
}
tokio::time::sleep(Duration::from_secs(5)).await;
handler.send_stop_message(0);
tokio::time::sleep(Duration::from_secs(5)).await;
_receiver.try_iter().for_each(|message| {
info!("Received message {message}");
});
}
}