2021-01-29 05:08:14 +01:00
|
|
|
use std::{
|
2021-01-31 04:34:07 +01:00
|
|
|
sync::mpsc::channel,
|
2021-01-29 05:08:14 +01:00
|
|
|
thread,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
use actix_rt::{Arbiter, System};
|
|
|
|
use tokio::sync::oneshot;
|
2021-01-29 05:08:14 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn await_for_timer() {
|
|
|
|
let time = Duration::from_secs(1);
|
|
|
|
let instant = Instant::now();
|
2021-01-31 04:34:07 +01:00
|
|
|
System::new().block_on(async move {
|
2021-01-29 05:08:14 +01:00
|
|
|
tokio::time::sleep(time).await;
|
|
|
|
});
|
|
|
|
assert!(
|
|
|
|
instant.elapsed() >= time,
|
|
|
|
"Block on should poll awaited future to completion"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-31 04:34:07 +01:00
|
|
|
fn join_another_arbiter() {
|
2021-01-29 05:08:14 +01:00
|
|
|
let time = Duration::from_secs(1);
|
|
|
|
let instant = Instant::now();
|
2021-01-31 04:34:07 +01:00
|
|
|
System::new().block_on(async move {
|
|
|
|
let arbiter = Arbiter::new();
|
|
|
|
arbiter.spawn(Box::pin(async move {
|
2021-01-29 05:08:14 +01:00
|
|
|
tokio::time::sleep(time).await;
|
2021-01-31 04:34:07 +01:00
|
|
|
Arbiter::handle().stop();
|
2021-01-29 05:08:14 +01:00
|
|
|
}));
|
2021-01-31 04:34:07 +01:00
|
|
|
arbiter.join().unwrap();
|
2021-01-29 05:08:14 +01:00
|
|
|
});
|
|
|
|
assert!(
|
|
|
|
instant.elapsed() >= time,
|
2021-01-31 04:34:07 +01:00
|
|
|
"Join on another arbiter should complete only when it calls stop"
|
2021-01-29 05:08:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let instant = Instant::now();
|
2021-01-31 04:34:07 +01:00
|
|
|
System::new().block_on(async move {
|
|
|
|
let arbiter = Arbiter::new();
|
|
|
|
arbiter.spawn_fn(move || {
|
2021-01-29 05:08:14 +01:00
|
|
|
actix_rt::spawn(async move {
|
|
|
|
tokio::time::sleep(time).await;
|
2021-01-31 04:34:07 +01:00
|
|
|
Arbiter::handle().stop();
|
2021-01-29 05:08:14 +01:00
|
|
|
});
|
|
|
|
});
|
2021-01-31 04:34:07 +01:00
|
|
|
arbiter.join().unwrap();
|
2021-01-29 05:08:14 +01:00
|
|
|
});
|
|
|
|
assert!(
|
|
|
|
instant.elapsed() >= time,
|
2021-01-31 04:34:07 +01:00
|
|
|
"Join on an arbiter that has used actix_rt::spawn should wait for said future"
|
2021-01-29 05:08:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let instant = Instant::now();
|
2021-01-31 04:34:07 +01:00
|
|
|
System::new().block_on(async move {
|
|
|
|
let arbiter = Arbiter::new();
|
|
|
|
arbiter.spawn(Box::pin(async move {
|
2021-01-29 05:08:14 +01:00
|
|
|
tokio::time::sleep(time).await;
|
2021-01-31 04:34:07 +01:00
|
|
|
Arbiter::handle().stop();
|
2021-01-29 05:08:14 +01:00
|
|
|
}));
|
2021-01-31 04:34:07 +01:00
|
|
|
arbiter.stop();
|
|
|
|
arbiter.join().unwrap();
|
2021-01-29 05:08:14 +01:00
|
|
|
});
|
|
|
|
assert!(
|
|
|
|
instant.elapsed() < time,
|
2021-01-31 04:34:07 +01:00
|
|
|
"Premature stop of arbiter should conclude regardless of it's current state"
|
2021-01-29 05:08:14 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn non_static_block_on() {
|
|
|
|
let string = String::from("test_str");
|
2021-01-31 04:34:07 +01:00
|
|
|
let string = string.as_str();
|
2021-01-29 05:08:14 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
let sys = System::new();
|
2021-01-29 05:08:14 +01:00
|
|
|
|
|
|
|
sys.block_on(async {
|
|
|
|
actix_rt::time::sleep(Duration::from_millis(1)).await;
|
2021-01-31 04:34:07 +01:00
|
|
|
assert_eq!("test_str", string);
|
2021-01-29 05:08:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let rt = actix_rt::Runtime::new().unwrap();
|
|
|
|
|
|
|
|
rt.block_on(async {
|
|
|
|
actix_rt::time::sleep(Duration::from_millis(1)).await;
|
2021-01-31 04:34:07 +01:00
|
|
|
assert_eq!("test_str", string);
|
2021-01-29 05:08:14 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn wait_for_spawns() {
|
|
|
|
let rt = actix_rt::Runtime::new().unwrap();
|
|
|
|
|
|
|
|
let handle = rt.spawn(async {
|
|
|
|
println!("running on the runtime");
|
|
|
|
// assertion panic is caught at task boundary
|
|
|
|
assert_eq!(1, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(rt.block_on(handle).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-31 04:34:07 +01:00
|
|
|
fn arbiter_spawn_fn_runs() {
|
|
|
|
let _ = System::new();
|
2021-01-29 15:16:10 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
let (tx, rx) = channel::<u32>();
|
2021-01-29 15:16:10 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
let arbiter = Arbiter::new();
|
|
|
|
arbiter.spawn_fn(move || tx.send(42).unwrap());
|
2021-01-29 15:16:10 +01:00
|
|
|
|
|
|
|
let num = rx.recv().unwrap();
|
|
|
|
assert_eq!(num, 42);
|
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
arbiter.stop();
|
|
|
|
arbiter.join().unwrap();
|
2021-01-29 15:16:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-31 04:34:07 +01:00
|
|
|
fn arbiter_drop_no_panic_fn() {
|
|
|
|
let _ = System::new();
|
2021-01-29 05:08:14 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
let arbiter = Arbiter::new();
|
|
|
|
arbiter.spawn_fn(|| panic!("test"));
|
2021-01-29 05:08:14 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
arbiter.stop();
|
|
|
|
arbiter.join().unwrap();
|
2021-01-29 05:08:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-31 04:34:07 +01:00
|
|
|
fn arbiter_drop_no_panic_fut() {
|
|
|
|
let _ = System::new();
|
2021-01-29 05:08:14 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
let arbiter = Arbiter::new();
|
|
|
|
arbiter.spawn(async { panic!("test") });
|
2021-01-29 05:08:14 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
arbiter.stop();
|
|
|
|
arbiter.join().unwrap();
|
2021-01-29 05:08:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-31 04:34:07 +01:00
|
|
|
fn arbiter_item_storage() {
|
|
|
|
let _ = System::new();
|
2021-01-29 05:08:14 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
let arbiter = Arbiter::new();
|
2021-01-29 05:08:14 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
assert!(!Arbiter::contains_item::<u32>());
|
|
|
|
Arbiter::set_item(42u32);
|
|
|
|
assert!(Arbiter::contains_item::<u32>());
|
2021-01-29 05:08:14 +01:00
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
Arbiter::get_item(|&item: &u32| assert_eq!(item, 42));
|
|
|
|
Arbiter::get_mut_item(|&mut item: &mut u32| assert_eq!(item, 42));
|
2021-01-29 05:08:14 +01:00
|
|
|
|
|
|
|
let thread = thread::spawn(move || {
|
2021-01-31 04:34:07 +01:00
|
|
|
Arbiter::get_item(|&_item: &u32| unreachable!("u32 not in this thread"));
|
2021-01-29 05:08:14 +01:00
|
|
|
})
|
|
|
|
.join();
|
|
|
|
assert!(thread.is_err());
|
|
|
|
|
|
|
|
let thread = thread::spawn(move || {
|
2021-01-31 04:34:07 +01:00
|
|
|
Arbiter::get_mut_item(|&mut _item: &mut i8| unreachable!("i8 not in this thread"));
|
2021-01-29 05:08:14 +01:00
|
|
|
})
|
|
|
|
.join();
|
|
|
|
assert!(thread.is_err());
|
|
|
|
|
2021-01-31 04:34:07 +01:00
|
|
|
arbiter.stop();
|
|
|
|
arbiter.join().unwrap();
|
2021-01-29 15:16:10 +01:00
|
|
|
}
|
2021-01-29 16:16:30 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn no_system_current_panic() {
|
|
|
|
System::current();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
2021-01-31 04:34:07 +01:00
|
|
|
fn no_system_arbiter_new_panic() {
|
|
|
|
Arbiter::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn system_arbiter_spawn() {
|
|
|
|
let runner = System::new();
|
|
|
|
|
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
let sys = System::current();
|
|
|
|
|
|
|
|
thread::spawn(|| {
|
|
|
|
// this thread will have no arbiter in it's thread local so call will panic
|
|
|
|
Arbiter::handle();
|
|
|
|
})
|
|
|
|
.join()
|
|
|
|
.unwrap_err();
|
|
|
|
|
|
|
|
let thread = thread::spawn(|| {
|
|
|
|
// this thread will have no arbiter in it's thread local so use the system handle instead
|
|
|
|
System::set_current(sys);
|
|
|
|
let sys = System::current();
|
|
|
|
|
|
|
|
let wrk = sys.arbiter();
|
|
|
|
wrk.spawn(async move {
|
|
|
|
tx.send(42u32).unwrap();
|
|
|
|
System::current().stop();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(runner.block_on(rx).unwrap(), 42);
|
|
|
|
thread.join().unwrap();
|
2021-01-29 16:16:30 +01:00
|
|
|
}
|