2021-01-29 05:08:14 +01:00
|
|
|
use std::{
|
2021-09-01 22:51:03 +02:00
|
|
|
future::Future,
|
2021-01-29 05:08:14 +01:00
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2021-09-01 22:51:03 +02:00
|
|
|
use actix_rt::{task::JoinError, Arbiter, System};
|
2021-10-21 12:04:51 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "io-uring"))]
|
|
|
|
use {
|
|
|
|
std::{sync::mpsc::channel, thread},
|
|
|
|
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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-15 19:49:02 +01:00
|
|
|
#[cfg(not(feature = "io-uring"))]
|
|
|
|
#[test]
|
|
|
|
fn run_with_code() {
|
|
|
|
let sys = System::new();
|
|
|
|
System::current().stop_with_code(42);
|
|
|
|
let exit_code = sys.run_with_code().expect("system stop should not error");
|
|
|
|
assert_eq!(exit_code, 42);
|
|
|
|
}
|
|
|
|
|
2021-01-29 05:08:14 +01:00
|
|
|
#[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 05:41:28 +01:00
|
|
|
Arbiter::current().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 05:41:28 +01:00
|
|
|
Arbiter::current().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 05:41:28 +01:00
|
|
|
Arbiter::current().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");
|
2021-11-15 19:49:02 +01:00
|
|
|
// panic is caught at task boundary
|
|
|
|
panic!("intentional test panic");
|
2021-01-29 05:08:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert!(rt.block_on(handle).is_err());
|
|
|
|
}
|
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
// Temporary disabled tests for io-uring feature.
|
|
|
|
// They should be enabled when possible.
|
|
|
|
|
|
|
|
#[cfg(not(feature = "io-uring"))]
|
2021-01-29 05:08:14 +01:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
#[cfg(not(feature = "io-uring"))]
|
2021-02-06 23:27:56 +01:00
|
|
|
#[test]
|
|
|
|
fn arbiter_handle_spawn_fn_runs() {
|
|
|
|
let sys = System::new();
|
|
|
|
|
|
|
|
let (tx, rx) = channel::<u32>();
|
|
|
|
|
|
|
|
let arbiter = Arbiter::new();
|
|
|
|
let handle = arbiter.handle();
|
|
|
|
drop(arbiter);
|
|
|
|
|
|
|
|
handle.spawn_fn(move || {
|
|
|
|
tx.send(42).unwrap();
|
|
|
|
System::current().stop()
|
|
|
|
});
|
|
|
|
|
|
|
|
let num = rx.recv_timeout(Duration::from_secs(2)).unwrap();
|
|
|
|
assert_eq!(num, 42);
|
|
|
|
|
|
|
|
handle.stop();
|
|
|
|
sys.run().unwrap();
|
|
|
|
}
|
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
#[cfg(not(feature = "io-uring"))]
|
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
|
|
|
}
|
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
#[cfg(not(feature = "io-uring"))]
|
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
|
|
|
}
|
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
#[cfg(not(feature = "io-uring"))]
|
2021-01-31 04:34:07 +01:00
|
|
|
#[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
|
2021-01-31 05:41:28 +01:00
|
|
|
Arbiter::current();
|
2021-01-31 04:34:07 +01:00
|
|
|
})
|
|
|
|
.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();
|
|
|
|
|
2021-01-31 05:41:28 +01:00
|
|
|
let arb = sys.arbiter();
|
|
|
|
arb.spawn(async move {
|
2021-01-31 04:34:07 +01:00
|
|
|
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
|
|
|
}
|
2021-01-31 05:41:28 +01:00
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
#[cfg(not(feature = "io-uring"))]
|
2021-01-31 05:41:28 +01:00
|
|
|
#[test]
|
|
|
|
fn system_stop_stops_arbiters() {
|
|
|
|
let sys = System::new();
|
|
|
|
let arb = Arbiter::new();
|
|
|
|
|
|
|
|
// arbiter should be alive to receive spawn msg
|
|
|
|
assert!(Arbiter::current().spawn_fn(|| {}));
|
|
|
|
assert!(arb.spawn_fn(|| {}));
|
|
|
|
|
|
|
|
System::current().stop();
|
|
|
|
sys.run().unwrap();
|
|
|
|
|
2021-10-11 03:58:11 +02:00
|
|
|
// account for slightly slow thread de-spawns
|
|
|
|
thread::sleep(Duration::from_millis(500));
|
2021-02-03 11:25:31 +01:00
|
|
|
|
2021-01-31 05:41:28 +01:00
|
|
|
// arbiter should be dead and return false
|
|
|
|
assert!(!Arbiter::current().spawn_fn(|| {}));
|
|
|
|
assert!(!arb.spawn_fn(|| {}));
|
|
|
|
|
|
|
|
arb.join().unwrap();
|
|
|
|
}
|
2021-02-03 11:25:31 +01:00
|
|
|
|
2021-10-11 03:58:11 +02:00
|
|
|
#[cfg(not(feature = "io-uring"))]
|
2021-02-03 11:25:31 +01:00
|
|
|
#[test]
|
|
|
|
fn new_system_with_tokio() {
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
|
|
|
let res = System::with_tokio_rt(move || {
|
|
|
|
tokio::runtime::Builder::new_multi_thread()
|
|
|
|
.enable_io()
|
|
|
|
.enable_time()
|
|
|
|
.thread_keep_alive(Duration::from_millis(1000))
|
|
|
|
.worker_threads(2)
|
|
|
|
.max_blocking_threads(2)
|
|
|
|
.on_thread_start(|| {})
|
|
|
|
.on_thread_stop(|| {})
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.block_on(async {
|
|
|
|
actix_rt::time::sleep(Duration::from_millis(1)).await;
|
|
|
|
|
|
|
|
tokio::task::spawn(async move {
|
|
|
|
tx.send(42).unwrap();
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
123usize
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(res, 123);
|
|
|
|
assert_eq!(rx.recv().unwrap(), 42);
|
|
|
|
}
|
|
|
|
|
2021-10-11 03:58:11 +02:00
|
|
|
#[cfg(not(feature = "io-uring"))]
|
2021-02-03 11:25:31 +01:00
|
|
|
#[test]
|
|
|
|
fn new_arbiter_with_tokio() {
|
2021-10-11 03:58:11 +02:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
|
|
|
|
2021-02-03 11:25:31 +01:00
|
|
|
let _ = System::new();
|
|
|
|
|
|
|
|
let arb = Arbiter::with_tokio_rt(|| {
|
|
|
|
tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
});
|
|
|
|
|
|
|
|
let counter = Arc::new(AtomicBool::new(true));
|
|
|
|
|
|
|
|
let counter1 = counter.clone();
|
|
|
|
let did_spawn = arb.spawn(async move {
|
|
|
|
actix_rt::time::sleep(Duration::from_millis(1)).await;
|
|
|
|
counter1.store(false, Ordering::SeqCst);
|
|
|
|
Arbiter::current().stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(did_spawn);
|
|
|
|
|
|
|
|
arb.join().unwrap();
|
|
|
|
|
2021-07-16 22:46:32 +02:00
|
|
|
assert!(!counter.load(Ordering::SeqCst));
|
2021-02-03 11:25:31 +01:00
|
|
|
}
|
2021-02-06 23:45:03 +01:00
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn no_system_current_panic() {
|
|
|
|
System::current();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn no_system_arbiter_new_panic() {
|
|
|
|
Arbiter::new();
|
|
|
|
}
|
|
|
|
|
2021-02-06 23:45:03 +01:00
|
|
|
#[test]
|
|
|
|
fn try_current_no_system() {
|
|
|
|
assert!(System::try_current().is_none())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn try_current_with_system() {
|
|
|
|
System::new().block_on(async { assert!(System::try_current().is_some()) });
|
|
|
|
}
|
2021-09-01 22:51:03 +02:00
|
|
|
|
|
|
|
#[allow(clippy::unit_cmp)]
|
|
|
|
#[test]
|
|
|
|
fn spawn_local() {
|
|
|
|
System::new().block_on(async {
|
|
|
|
// demonstrate that spawn -> R is strictly more capable than spawn -> ()
|
|
|
|
|
|
|
|
assert_eq!(actix_rt::spawn(async {}).await.unwrap(), ());
|
|
|
|
assert_eq!(actix_rt::spawn(async { 1 }).await.unwrap(), 1);
|
|
|
|
assert!(actix_rt::spawn(async { panic!("") }).await.is_err());
|
|
|
|
|
|
|
|
actix_rt::spawn(async { tokio::time::sleep(Duration::from_millis(50)).await })
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
fn g<F: Future<Output = Result<(), JoinError>>>(_f: F) {}
|
|
|
|
g(actix_rt::spawn(async {}));
|
|
|
|
// g(actix_rt::spawn(async { 1 })); // compile err
|
|
|
|
|
|
|
|
fn h<F: Future<Output = Result<R, JoinError>>, R>(_f: F) {}
|
|
|
|
h(actix_rt::spawn(async {}));
|
|
|
|
h(actix_rt::spawn(async { 1 }));
|
|
|
|
})
|
|
|
|
}
|
2021-10-11 03:58:11 +02:00
|
|
|
|
|
|
|
#[cfg(all(target_os = "linux", feature = "io-uring"))]
|
|
|
|
#[test]
|
|
|
|
fn tokio_uring_arbiter() {
|
2021-10-21 12:04:51 +02:00
|
|
|
System::new().block_on(async {
|
|
|
|
let (tx, rx) = std::sync::mpsc::channel();
|
2021-10-11 03:58:11 +02:00
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
Arbiter::new().spawn(async move {
|
|
|
|
let handle = actix_rt::spawn(async move {
|
|
|
|
let f = tokio_uring::fs::File::create("test.txt").await.unwrap();
|
|
|
|
let buf = b"Hello World!";
|
2021-10-11 03:58:11 +02:00
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
let (res, _) = f.write_at(&buf[..], 0).await;
|
|
|
|
assert!(res.is_ok());
|
2021-10-11 03:58:11 +02:00
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
f.sync_all().await.unwrap();
|
|
|
|
f.close().await.unwrap();
|
2021-10-11 03:58:11 +02:00
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
std::fs::remove_file("test.txt").unwrap();
|
|
|
|
});
|
2021-10-11 03:58:11 +02:00
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
handle.await.unwrap();
|
|
|
|
tx.send(true).unwrap();
|
|
|
|
});
|
2021-10-11 03:58:11 +02:00
|
|
|
|
2021-10-21 12:04:51 +02:00
|
|
|
assert!(rx.recv().unwrap());
|
|
|
|
})
|
2021-10-11 03:58:11 +02:00
|
|
|
}
|