-
Notifications
You must be signed in to change notification settings - Fork 14
/
monitor.rs
36 lines (32 loc) · 844 Bytes
/
monitor.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
#[tokio::main]
async fn main() {
eprintln!("all positive {:?}", run(&vec![1, 2, 10]).await);
eprintln!("some negative {:?}", run(&vec![1, 2, -3, 10]).await);
}
/// Run the simulated journal.
pub async fn run(inputs: &Vec<i32>) -> anyhow::Result<()> {
moro::async_scope!(|scope| {
for input in inputs {
let _ = scope.spawn(validate(input)).or_cancel(scope);
}
Ok(())
})
.await
}
pub async fn validate(input: &i32) -> anyhow::Result<()> {
if *input < 0 {
anyhow::bail!("input out of range: {input}");
}
Ok(())
}
#[tokio::test]
async fn all_positive() {
assert!(run(&vec![1, 2, 10]).await.is_ok());
}
#[tokio::test]
async fn some_negative() {
match run(&vec![1, 2, -3, 10]).await {
Err(_) => (),
Ok(()) => panic!("expected an error"),
}
}