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

remove builder and introduce worker handle (#257)

This commit is contained in:
Rob Ede
2021-01-31 03:34:07 +00:00
committed by GitHub
parent 1b35ff8ee6
commit b75254403a
13 changed files with 545 additions and 585 deletions

View File

@ -1,16 +1,17 @@
use std::{
sync::mpsc::sync_channel,
sync::mpsc::channel,
thread,
time::{Duration, Instant},
};
use actix_rt::{System, Worker};
use actix_rt::{Arbiter, System};
use tokio::sync::oneshot;
#[test]
fn await_for_timer() {
let time = Duration::from_secs(1);
let instant = Instant::now();
System::new("test_wait_timer").block_on(async move {
System::new().block_on(async move {
tokio::time::sleep(time).await;
});
assert!(
@ -20,78 +21,72 @@ fn await_for_timer() {
}
#[test]
fn join_another_worker() {
fn join_another_arbiter() {
let time = Duration::from_secs(1);
let instant = Instant::now();
System::new("test_join_another_worker").block_on(async move {
let mut worker = Worker::new();
worker.spawn(Box::pin(async move {
System::new().block_on(async move {
let arbiter = Arbiter::new();
arbiter.spawn(Box::pin(async move {
tokio::time::sleep(time).await;
Worker::current().stop();
Arbiter::handle().stop();
}));
worker.join().unwrap();
arbiter.join().unwrap();
});
assert!(
instant.elapsed() >= time,
"Join on another worker should complete only when it calls stop"
"Join on another arbiter should complete only when it calls stop"
);
let instant = Instant::now();
System::new("test_join_another_worker").block_on(async move {
let mut worker = Worker::new();
worker.spawn_fn(move || {
System::new().block_on(async move {
let arbiter = Arbiter::new();
arbiter.spawn_fn(move || {
actix_rt::spawn(async move {
tokio::time::sleep(time).await;
Worker::current().stop();
Arbiter::handle().stop();
});
});
worker.join().unwrap();
arbiter.join().unwrap();
});
assert!(
instant.elapsed() >= time,
"Join on a worker that has used actix_rt::spawn should wait for said future"
"Join on an arbiter that has used actix_rt::spawn should wait for said future"
);
let instant = Instant::now();
System::new("test_join_another_worker").block_on(async move {
let mut worker = Worker::new();
worker.spawn(Box::pin(async move {
System::new().block_on(async move {
let arbiter = Arbiter::new();
arbiter.spawn(Box::pin(async move {
tokio::time::sleep(time).await;
Worker::current().stop();
Arbiter::handle().stop();
}));
worker.stop();
worker.join().unwrap();
arbiter.stop();
arbiter.join().unwrap();
});
assert!(
instant.elapsed() < time,
"Premature stop of worker should conclude regardless of it's current state"
"Premature stop of arbiter should conclude regardless of it's current state"
);
}
#[test]
fn non_static_block_on() {
let string = String::from("test_str");
let str = string.as_str();
let string = string.as_str();
let sys = System::new("borrow some");
let sys = System::new();
sys.block_on(async {
actix_rt::time::sleep(Duration::from_millis(1)).await;
assert_eq!("test_str", str);
assert_eq!("test_str", string);
});
let rt = actix_rt::Runtime::new().unwrap();
rt.block_on(async {
actix_rt::time::sleep(Duration::from_millis(1)).await;
assert_eq!("test_str", str);
assert_eq!("test_str", string);
});
System::run(|| {
assert_eq!("test_str", str);
System::current().stop();
})
.unwrap();
}
#[test]
@ -108,82 +103,70 @@ fn wait_for_spawns() {
}
#[test]
fn worker_spawn_fn_runs() {
let _ = System::new("test-system");
fn arbiter_spawn_fn_runs() {
let _ = System::new();
let (tx, rx) = sync_channel::<u32>(1);
let (tx, rx) = channel::<u32>();
let mut worker = Worker::new();
worker.spawn_fn(move || tx.send(42).unwrap());
let arbiter = Arbiter::new();
arbiter.spawn_fn(move || tx.send(42).unwrap());
let num = rx.recv().unwrap();
assert_eq!(num, 42);
worker.stop();
worker.join().unwrap();
arbiter.stop();
arbiter.join().unwrap();
}
#[test]
fn worker_drop_no_panic_fn() {
let _ = System::new("test-system");
fn arbiter_drop_no_panic_fn() {
let _ = System::new();
let mut worker = Worker::new();
worker.spawn_fn(|| panic!("test"));
let arbiter = Arbiter::new();
arbiter.spawn_fn(|| panic!("test"));
worker.stop();
worker.join().unwrap();
arbiter.stop();
arbiter.join().unwrap();
}
#[test]
fn worker_drop_no_panic_fut() {
let _ = System::new("test-system");
fn arbiter_drop_no_panic_fut() {
let _ = System::new();
let mut worker = Worker::new();
worker.spawn(async { panic!("test") });
let arbiter = Arbiter::new();
arbiter.spawn(async { panic!("test") });
worker.stop();
worker.join().unwrap();
arbiter.stop();
arbiter.join().unwrap();
}
#[test]
fn worker_item_storage() {
let _ = System::new("test-system");
fn arbiter_item_storage() {
let _ = System::new();
let mut worker = Worker::new();
let arbiter = Arbiter::new();
assert!(!Worker::contains_item::<u32>());
Worker::set_item(42u32);
assert!(Worker::contains_item::<u32>());
assert!(!Arbiter::contains_item::<u32>());
Arbiter::set_item(42u32);
assert!(Arbiter::contains_item::<u32>());
Worker::get_item(|&item: &u32| assert_eq!(item, 42));
Worker::get_mut_item(|&mut item: &mut u32| assert_eq!(item, 42));
Arbiter::get_item(|&item: &u32| assert_eq!(item, 42));
Arbiter::get_mut_item(|&mut item: &mut u32| assert_eq!(item, 42));
let thread = thread::spawn(move || {
Worker::get_item(|&_item: &u32| unreachable!("u32 not in this thread"));
Arbiter::get_item(|&_item: &u32| unreachable!("u32 not in this thread"));
})
.join();
assert!(thread.is_err());
let thread = thread::spawn(move || {
Worker::get_mut_item(|&mut _item: &mut i8| unreachable!("i8 not in this thread"));
Arbiter::get_mut_item(|&mut _item: &mut i8| unreachable!("i8 not in this thread"));
})
.join();
assert!(thread.is_err());
worker.stop();
worker.join().unwrap();
}
#[test]
fn system_name_cow_str() {
let _ = System::new("test-system");
System::current().stop();
}
#[test]
fn system_name_cow_string() {
let _ = System::new("test-system".to_owned());
System::current().stop();
arbiter.stop();
arbiter.join().unwrap();
}
#[test]
@ -194,6 +177,36 @@ fn no_system_current_panic() {
#[test]
#[should_panic]
fn no_system_worker_new_panic() {
Worker::new();
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();
}