1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-02 22:12:22 +01:00

add arbiter handle assoc fn (#274)

* add arbiter handle assoc fn
This commit is contained in:
Rob Ede 2021-02-06 22:27:56 +00:00 committed by GitHub
parent 7ee42b50b4
commit eb4d29e15e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View File

@ -1,6 +1,9 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
* Add `Arbiter::handle` to get a handle of an owned Arbiter. [#274]
[#274]: https://github.com/actix/actix-net/pull/274
## 2.0.1 - 2021-02-06 ## 2.0.1 - 2021-02-06

View File

@ -172,13 +172,18 @@ impl Arbiter {
hnd hnd
} }
/// Return a handle to the this Arbiter's message sender.
pub fn handle(&self) -> ArbiterHandle {
ArbiterHandle::new(self.tx.clone())
}
/// Return a handle to the current thread's Arbiter's message sender. /// Return a handle to the current thread's Arbiter's message sender.
/// ///
/// # Panics /// # Panics
/// Panics if no Arbiter is running on the current thread. /// Panics if no Arbiter is running on the current thread.
pub fn current() -> ArbiterHandle { pub fn current() -> ArbiterHandle {
HANDLE.with(|cell| match *cell.borrow() { HANDLE.with(|cell| match *cell.borrow() {
Some(ref addr) => addr.clone(), Some(ref hnd) => hnd.clone(),
None => panic!("Arbiter is not running."), None => panic!("Arbiter is not running."),
}) })
} }

View File

@ -122,6 +122,28 @@ fn arbiter_spawn_fn_runs() {
arbiter.join().unwrap(); arbiter.join().unwrap();
} }
#[test]
fn arbiter_handle_spawn_fn_runs() {
let sys = System::new();
let (tx, rx) = channel::<u32>();
let arbiter = Arbiter::new();
let handle = arbiter.handle();
drop(arbiter);
handle.spawn_fn(move || {
tx.send(42).unwrap();
System::current().stop()
});
let num = rx.recv_timeout(Duration::from_secs(2)).unwrap();
assert_eq!(num, 42);
handle.stop();
sys.run().unwrap();
}
#[test] #[test]
fn arbiter_drop_no_panic_fn() { fn arbiter_drop_no_panic_fn() {
let _ = System::new(); let _ = System::new();