From 6f7508c77162afee7c57161b214de35014434c3c Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 29 Jan 2021 02:05:34 +0000 Subject: [PATCH] describe panicking behavior in tests --- actix-rt/CHANGES.md | 5 ++-- actix-rt/src/arbiter.rs | 11 +++------ actix-rt/src/system.rs | 10 ++++---- actix-rt/tests/integration_tests.rs | 37 ++++++++++++++++++++++++++--- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/actix-rt/CHANGES.md b/actix-rt/CHANGES.md index 031ef4bc..c38373dd 100644 --- a/actix-rt/CHANGES.md +++ b/actix-rt/CHANGES.md @@ -2,13 +2,12 @@ ## Unreleased - 2021-xx-xx -* Remove `run_in_tokio` and `attach_to_tokio`. [#253] -* Remove `AsyncSystemRunner`. [#253] +* Remove `run_in_tokio`, `attach_to_tokio` and `AsyncSystemRunner`. [#253] * Return `JoinHandle` from `actix_rt::spawn`. [#253] * Remove old `Arbiter::spawn`. Implementation is now inlined into `actix_rt::spawn`. [#253] * Rename `Arbiter::{send => spawn}` and `Arbiter::{exec_fn => spawn_fn}`. [#253] * Remove `Arbiter::exec`. [#253] -* Remove deprecated `Arbiter::local_join`. [#253] +* Remove deprecated `Arbiter::local_join` and `Arbiter::is_running`. [#253] [#253]: https://github.com/actix/actix-net/pull/253 diff --git a/actix-rt/src/arbiter.rs b/actix-rt/src/arbiter.rs index cbf38397..fde3cd1c 100644 --- a/actix-rt/src/arbiter.rs +++ b/actix-rt/src/arbiter.rs @@ -87,12 +87,6 @@ impl Arbiter { }) } - /// Check if current arbiter is running. - #[deprecated(note = "Thread local variables for running state of Arbiter is removed")] - pub fn is_running() -> bool { - false - } - /// Stop arbiter from continuing it's event loop. pub fn stop(&self) { let _ = self.sender.send(ArbiterCommand::Stop); @@ -122,7 +116,7 @@ impl Arbiter { // register arbiter let _ = System::current() - .sys() + .tx() .send(SystemCommand::RegisterArbiter(id, arb)); // start arbiter controller @@ -131,7 +125,7 @@ impl Arbiter { // deregister arbiter let _ = System::current() - .sys() + .tx() .send(SystemCommand::DeregisterArbiter(id)); } }) @@ -243,6 +237,7 @@ struct ArbiterController { impl Drop for ArbiterController { fn drop(&mut self) { + // panics can only occur with spawn_fn calls if thread::panicking() { if System::current().stop_on_panic() { eprintln!("Panic in Arbiter thread, shutting down system."); diff --git a/actix-rt/src/system.rs b/actix-rt/src/system.rs index df0ab57f..64080a63 100644 --- a/actix-rt/src/system.rs +++ b/actix-rt/src/system.rs @@ -17,7 +17,7 @@ static SYSTEM_COUNT: AtomicUsize = AtomicUsize::new(0); #[derive(Clone, Debug)] pub struct System { id: usize, - sys: UnboundedSender, + tx: UnboundedSender, arbiter: Arbiter, stop_on_panic: bool, } @@ -34,7 +34,7 @@ impl System { stop_on_panic: bool, ) -> Self { let sys = System { - sys, + tx: sys, arbiter, stop_on_panic, id: SYSTEM_COUNT.fetch_add(1, Ordering::SeqCst), @@ -102,11 +102,11 @@ impl System { /// Stop the system with a particular exit code. pub fn stop_with_code(&self, code: i32) { - let _ = self.sys.send(SystemCommand::Exit(code)); + let _ = self.tx.send(SystemCommand::Exit(code)); } - pub(crate) fn sys(&self) -> &UnboundedSender { - &self.sys + pub(crate) fn tx(&self) -> &UnboundedSender { + &self.tx } /// Return status of 'stop_on_panic' option which controls whether the System is stopped when an diff --git a/actix-rt/tests/integration_tests.rs b/actix-rt/tests/integration_tests.rs index 5bfd0733..abaff1c9 100644 --- a/actix-rt/tests/integration_tests.rs +++ b/actix-rt/tests/integration_tests.rs @@ -1,8 +1,9 @@ -use std::time::{Duration, Instant}; +use std::{ + thread, + time::{Duration, Instant}, +}; use actix_rt::{Arbiter, System}; -use futures_util::future::lazy; -use tokio::task::LocalSet; #[test] fn await_for_timer() { @@ -104,3 +105,33 @@ fn wait_for_spawns() { assert!(rt.block_on(handle).is_err()); } + +#[test] +#[should_panic] +fn arbiter_drop_panic_fn() { + let _ = System::new("test-system"); + + let mut arbiter = Arbiter::new(); + arbiter.spawn_fn(|| panic!("test")); + + arbiter.join().unwrap(); +} + +#[test] +fn arbiter_drop_no_panic_fut() { + use futures_util::future::lazy; + + let _ = System::new("test-system"); + + let mut arbiter = Arbiter::new(); + arbiter.spawn(lazy(|_| panic!("test"))); + + let arb = arbiter.clone(); + let thread = thread::spawn(move || { + thread::sleep(Duration::from_millis(200)); + arb.stop(); + }); + + arbiter.join().unwrap(); + thread.join().unwrap(); +}