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

remove tokio runners (#253)

This commit is contained in:
Rob Ede
2021-01-29 02:21:06 +00:00
committed by GitHub
parent feac376c17
commit ba39c8436d
10 changed files with 103 additions and 337 deletions

View File

@ -1,6 +1,9 @@
use std::time::{Duration, Instant};
use std::{
thread,
time::{Duration, Instant},
};
use futures_util::future::try_join_all;
use actix_rt::{Arbiter, System};
#[test]
fn await_for_timer() {
@ -21,7 +24,7 @@ fn join_another_arbiter() {
let instant = Instant::now();
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
let mut arbiter = actix_rt::Arbiter::new();
arbiter.send(Box::pin(async move {
arbiter.spawn(Box::pin(async move {
tokio::time::sleep(time).await;
actix_rt::Arbiter::current().stop();
}));
@ -35,7 +38,7 @@ fn join_another_arbiter() {
let instant = Instant::now();
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
let mut arbiter = actix_rt::Arbiter::new();
arbiter.exec_fn(move || {
arbiter.spawn_fn(move || {
actix_rt::spawn(async move {
tokio::time::sleep(time).await;
actix_rt::Arbiter::current().stop();
@ -51,7 +54,7 @@ fn join_another_arbiter() {
let instant = Instant::now();
actix_rt::System::new("test_join_another_arbiter").block_on(async move {
let mut arbiter = actix_rt::Arbiter::new();
arbiter.send(Box::pin(async move {
arbiter.spawn(Box::pin(async move {
tokio::time::sleep(time).await;
actix_rt::Arbiter::current().stop();
}));
@ -104,71 +107,31 @@ fn wait_for_spawns() {
}
#[test]
fn run_in_existing_tokio() {
use actix_rt::System;
use futures_util::future::try_join_all;
use tokio::task::LocalSet;
#[should_panic]
fn arbiter_drop_panic_fn() {
let _ = System::new("test-system");
async fn run_application() {
let first_task = tokio::spawn(async {
println!("One task");
Ok::<(), ()>(())
});
let mut arbiter = Arbiter::new();
arbiter.spawn_fn(|| panic!("test"));
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()
arbiter.join().unwrap();
}
#[test]
fn attack_to_tokio() {
use actix_rt::System;
fn arbiter_drop_no_panic_fut() {
use futures_util::future::lazy;
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.unwrap();
let _ = System::new("test-system");
let rest_operations = run_application();
let res = System::attach_to_tokio("actix-main-system", runtime, rest_operations);
let mut arbiter = Arbiter::new();
arbiter.spawn(lazy(|_| panic!("test")));
assert_eq!(res, 2);
let arb = arbiter.clone();
let thread = thread::spawn(move || {
thread::sleep(Duration::from_millis(200));
arb.stop();
});
arbiter.join().unwrap();
thread.join().unwrap();
}