2021-01-29 02:21:06 +00:00
|
|
|
use std::{
|
|
|
|
thread,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2020-02-05 22:59:45 -03:00
|
|
|
|
2021-01-29 02:21:06 +00:00
|
|
|
use actix_rt::{Arbiter, System};
|
2021-01-26 09:46:14 +00:00
|
|
|
|
2020-02-05 22:59:45 -03:00
|
|
|
#[test]
|
|
|
|
fn await_for_timer() {
|
2021-01-26 09:46:14 +00:00
|
|
|
let time = Duration::from_secs(1);
|
2020-02-05 22:59:45 -03:00
|
|
|
let instant = Instant::now();
|
|
|
|
actix_rt::System::new("test_wait_timer").block_on(async move {
|
2020-12-28 09:40:22 +08:00
|
|
|
tokio::time::sleep(time).await;
|
2020-02-05 22:59:45 -03:00
|
|
|
});
|
|
|
|
assert!(
|
|
|
|
instant.elapsed() >= time,
|
|
|
|
"Block on should poll awaited future to completion"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn join_another_arbiter() {
|
2021-01-26 09:46:14 +00:00
|
|
|
let time = Duration::from_secs(1);
|
2020-02-05 22:59:45 -03:00
|
|
|
let instant = Instant::now();
|
|
|
|
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
|
|
|
|
let mut arbiter = actix_rt::Arbiter::new();
|
2021-01-29 02:21:06 +00:00
|
|
|
arbiter.spawn(Box::pin(async move {
|
2020-12-28 09:40:22 +08:00
|
|
|
tokio::time::sleep(time).await;
|
2020-02-05 22:59:45 -03:00
|
|
|
actix_rt::Arbiter::current().stop();
|
|
|
|
}));
|
|
|
|
arbiter.join().unwrap();
|
|
|
|
});
|
|
|
|
assert!(
|
|
|
|
instant.elapsed() >= time,
|
|
|
|
"Join on another arbiter should complete only when it calls stop"
|
|
|
|
);
|
|
|
|
|
|
|
|
let instant = Instant::now();
|
|
|
|
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
|
|
|
|
let mut arbiter = actix_rt::Arbiter::new();
|
2021-01-29 02:21:06 +00:00
|
|
|
arbiter.spawn_fn(move || {
|
2020-02-05 22:59:45 -03:00
|
|
|
actix_rt::spawn(async move {
|
2020-12-28 09:40:22 +08:00
|
|
|
tokio::time::sleep(time).await;
|
2020-02-05 22:59:45 -03:00
|
|
|
actix_rt::Arbiter::current().stop();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
arbiter.join().unwrap();
|
|
|
|
});
|
|
|
|
assert!(
|
|
|
|
instant.elapsed() >= time,
|
|
|
|
"Join on a arbiter that has used actix_rt::spawn should wait for said future"
|
|
|
|
);
|
|
|
|
|
|
|
|
let instant = Instant::now();
|
|
|
|
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
|
|
|
|
let mut arbiter = actix_rt::Arbiter::new();
|
2021-01-29 02:21:06 +00:00
|
|
|
arbiter.spawn(Box::pin(async move {
|
2020-12-28 09:40:22 +08:00
|
|
|
tokio::time::sleep(time).await;
|
2020-02-05 22:59:45 -03:00
|
|
|
actix_rt::Arbiter::current().stop();
|
|
|
|
}));
|
|
|
|
arbiter.stop();
|
|
|
|
arbiter.join().unwrap();
|
|
|
|
});
|
|
|
|
assert!(
|
|
|
|
instant.elapsed() < time,
|
|
|
|
"Premature stop of arbiter should conclude regardless of it's current state"
|
|
|
|
);
|
|
|
|
}
|
2020-02-06 00:36:00 -03:00
|
|
|
|
|
|
|
#[test]
|
2020-12-27 07:26:02 +08:00
|
|
|
fn non_static_block_on() {
|
|
|
|
let string = String::from("test_str");
|
|
|
|
let str = string.as_str();
|
2020-02-06 00:36:00 -03:00
|
|
|
|
2020-12-28 09:40:22 +08:00
|
|
|
let sys = actix_rt::System::new("borrow some");
|
2020-12-27 07:26:02 +08:00
|
|
|
|
|
|
|
sys.block_on(async {
|
2020-12-28 09:40:22 +08:00
|
|
|
actix_rt::time::sleep(Duration::from_millis(1)).await;
|
2020-12-27 07:26:02 +08:00
|
|
|
assert_eq!("test_str", str);
|
2020-02-06 00:36:00 -03:00
|
|
|
});
|
|
|
|
|
2020-12-28 09:40:22 +08:00
|
|
|
let rt = actix_rt::Runtime::new().unwrap();
|
2020-12-27 07:26:02 +08:00
|
|
|
|
|
|
|
rt.block_on(async {
|
2020-12-28 09:40:22 +08:00
|
|
|
actix_rt::time::sleep(Duration::from_millis(1)).await;
|
2020-12-27 07:26:02 +08:00
|
|
|
assert_eq!("test_str", str);
|
2020-02-06 00:36:00 -03:00
|
|
|
});
|
2020-12-27 07:26:02 +08:00
|
|
|
|
|
|
|
actix_rt::System::run(|| {
|
|
|
|
assert_eq!("test_str", str);
|
|
|
|
actix_rt::System::current().stop();
|
|
|
|
})
|
|
|
|
.unwrap();
|
2020-02-06 00:36:00 -03:00
|
|
|
}
|
2021-01-26 09:46:14 +00: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-29 02:21:06 +00:00
|
|
|
#[should_panic]
|
|
|
|
fn arbiter_drop_panic_fn() {
|
|
|
|
let _ = System::new("test-system");
|
2021-01-26 09:46:14 +00:00
|
|
|
|
2021-01-29 02:21:06 +00:00
|
|
|
let mut arbiter = Arbiter::new();
|
|
|
|
arbiter.spawn_fn(|| panic!("test"));
|
2021-01-26 09:46:14 +00:00
|
|
|
|
2021-01-29 02:21:06 +00:00
|
|
|
arbiter.join().unwrap();
|
2021-01-26 09:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-29 02:21:06 +00:00
|
|
|
fn arbiter_drop_no_panic_fut() {
|
|
|
|
use futures_util::future::lazy;
|
|
|
|
|
|
|
|
let _ = System::new("test-system");
|
2021-01-26 09:46:14 +00:00
|
|
|
|
2021-01-29 02:21:06 +00:00
|
|
|
let mut arbiter = Arbiter::new();
|
|
|
|
arbiter.spawn(lazy(|_| panic!("test")));
|
2021-01-26 09:46:14 +00:00
|
|
|
|
2021-01-29 02:21:06 +00:00
|
|
|
let arb = arbiter.clone();
|
|
|
|
let thread = thread::spawn(move || {
|
|
|
|
thread::sleep(Duration::from_millis(200));
|
|
|
|
arb.stop();
|
|
|
|
});
|
2021-01-26 09:46:14 +00:00
|
|
|
|
2021-01-29 02:21:06 +00:00
|
|
|
arbiter.join().unwrap();
|
|
|
|
thread.join().unwrap();
|
2021-01-26 09:46:14 +00:00
|
|
|
}
|