1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-26 19:47:43 +02:00

add rt tests and doc tests

This commit is contained in:
Rob Ede
2021-01-26 09:46:14 +00:00
parent cff9deb729
commit 45edff625e
7 changed files with 247 additions and 159 deletions

View File

@ -1,8 +1,10 @@
use std::time::{Duration, Instant};
use futures_util::future::try_join_all;
#[test]
fn await_for_timer() {
let time = Duration::from_secs(2);
let time = Duration::from_secs(1);
let instant = Instant::now();
actix_rt::System::new("test_wait_timer").block_on(async move {
tokio::time::sleep(time).await;
@ -15,7 +17,7 @@ fn await_for_timer() {
#[test]
fn join_another_arbiter() {
let time = Duration::from_secs(2);
let time = Duration::from_secs(1);
let instant = Instant::now();
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
let mut arbiter = actix_rt::Arbiter::new();
@ -87,3 +89,100 @@ fn non_static_block_on() {
})
.unwrap();
}
#[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]
fn run_in_existing_tokio() {
use actix_rt::System;
use futures_util::future::try_join_all;
use tokio::task::LocalSet;
async fn run_application() {
let first_task = tokio::spawn(async {
println!("One task");
Ok::<(), ()>(())
});
let second_task = tokio::spawn(async {
println!("Another task");
Ok::<(), ()>(())
});
try_join_all(vec![first_task, second_task])
.await
.expect("Some of the futures finished unexpectedly");
}
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.unwrap();
let actix_local_set = LocalSet::new();
let sys = System::run_in_tokio("actix-main-system", &actix_local_set);
actix_local_set.spawn_local(sys);
let rest_operations = run_application();
runtime.block_on(actix_local_set.run_until(rest_operations));
}
async fn run_application() -> usize {
let first_task = tokio::spawn(async {
println!("One task");
Ok::<(), ()>(())
});
let second_task = tokio::spawn(async {
println!("Another task");
Ok::<(), ()>(())
});
let tasks = try_join_all(vec![first_task, second_task])
.await
.expect("Some of the futures finished unexpectedly");
tasks.len()
}
#[test]
fn attack_to_tokio() {
use actix_rt::System;
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.unwrap();
let rest_operations = run_application();
let res = System::attach_to_tokio("actix-main-system", runtime, rest_operations);
assert_eq!(res, 2);
}
#[tokio::test]
async fn attack_to_tokio_macro() {
use actix_rt::System;
let rest_operations = run_application();
let res = System::attach_to_tokio(
"actix-main-system",
tokio::runtime::Runtime::handle(&self),
rest_operations,
);
assert_eq!(res, 2);
}