From 783880bb0a053515864dcecf593f1564f8c0c1b2 Mon Sep 17 00:00:00 2001 From: Jonathas-Conceicao Date: Sat, 4 Apr 2020 19:05:08 -0300 Subject: [PATCH] actix-rt: Add Arbiter::is_running helper and fix System::is_set doc `Arbiter::is_running` can be used to check if the current even-loop is currently running; which should also work after the system has stopped. `System::is_set` was updated to reflect what it actually does, it tells if the event loop has started, which alone can't tell if it has stopped. Signed-off-by: Jonathas-Conceicao --- actix-rt/CHANGES.md | 6 +++++- actix-rt/src/arbiter.rs | 5 +++++ actix-rt/src/system.rs | 2 +- .../{wait_spawned.rs => integration_tests.rs} | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) rename actix-rt/tests/{wait_spawned.rs => integration_tests.rs} (90%) diff --git a/actix-rt/CHANGES.md b/actix-rt/CHANGES.md index a95057c2..08497c97 100644 --- a/actix-rt/CHANGES.md +++ b/actix-rt/CHANGES.md @@ -2,7 +2,11 @@ ## [TBD] - [TBD] -- Expose `System::is_set` to check if current system is running +Added + +- Expose `System::is_set` to check if current system has ben started + +- Add `Arbiter::is_running` to check if event loop is running - Add `Arbiter::local_join` associated function to get be able to `await` for spawned futures diff --git a/actix-rt/src/arbiter.rs b/actix-rt/src/arbiter.rs index b9fbc51a..124a5580 100644 --- a/actix-rt/src/arbiter.rs +++ b/actix-rt/src/arbiter.rs @@ -89,6 +89,11 @@ impl Arbiter { }) } + /// Check if current arbiter is running. + pub fn is_running() -> bool { + RUNNING.with(|cell| cell.get()) + } + /// Stop arbiter from continuing it's event loop. pub fn stop(&self) { let _ = self.sender.unbounded_send(ArbiterCommand::Stop); diff --git a/actix-rt/src/system.rs b/actix-rt/src/system.rs index f89fe9f3..21264669 100644 --- a/actix-rt/src/system.rs +++ b/actix-rt/src/system.rs @@ -79,7 +79,7 @@ impl System { }) } - /// Check if current system is running. + /// Check if current system is set, i.e., as already been started. pub fn is_set() -> bool { CURRENT.with(|cell| cell.borrow().is_some()) } diff --git a/actix-rt/tests/wait_spawned.rs b/actix-rt/tests/integration_tests.rs similarity index 90% rename from actix-rt/tests/wait_spawned.rs rename to actix-rt/tests/integration_tests.rs index af5d0224..8e775bab 100644 --- a/actix-rt/tests/wait_spawned.rs +++ b/actix-rt/tests/integration_tests.rs @@ -1,5 +1,19 @@ use std::time::{Duration, Instant}; +#[test] +fn start_and_stop() { + actix_rt::System::new("start_and_stop").block_on(async move { + assert!( + actix_rt::Arbiter::is_running(), + "System doesn't seem to have started" + ); + }); + assert!( + !actix_rt::Arbiter::is_running(), + "System doesn't seem to have stopped" + ); +} + #[test] fn await_for_timer() { let time = Duration::from_secs(2);