Skip to content

Commit eb4d29e

Browse files
authored
add arbiter handle assoc fn (#274)
* add arbiter handle assoc fn
1 parent 7ee42b5 commit eb4d29e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

actix-rt/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changes
22

33
## Unreleased - 2021-xx-xx
4+
* Add `Arbiter::handle` to get a handle of an owned Arbiter. [#274]
5+
6+
[#274]: https://github.com/actix/actix-net/pull/274
47

58

69
## 2.0.1 - 2021-02-06

actix-rt/src/arbiter.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,18 @@ impl Arbiter {
172172
hnd
173173
}
174174

175+
/// Return a handle to the this Arbiter's message sender.
176+
pub fn handle(&self) -> ArbiterHandle {
177+
ArbiterHandle::new(self.tx.clone())
178+
}
179+
175180
/// Return a handle to the current thread's Arbiter's message sender.
176181
///
177182
/// # Panics
178183
/// Panics if no Arbiter is running on the current thread.
179184
pub fn current() -> ArbiterHandle {
180185
HANDLE.with(|cell| match *cell.borrow() {
181-
Some(ref addr) => addr.clone(),
186+
Some(ref hnd) => hnd.clone(),
182187
None => panic!("Arbiter is not running."),
183188
})
184189
}

actix-rt/tests/tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@ fn arbiter_spawn_fn_runs() {
122122
arbiter.join().unwrap();
123123
}
124124

125+
#[test]
126+
fn arbiter_handle_spawn_fn_runs() {
127+
let sys = System::new();
128+
129+
let (tx, rx) = channel::<u32>();
130+
131+
let arbiter = Arbiter::new();
132+
let handle = arbiter.handle();
133+
drop(arbiter);
134+
135+
handle.spawn_fn(move || {
136+
tx.send(42).unwrap();
137+
System::current().stop()
138+
});
139+
140+
let num = rx.recv_timeout(Duration::from_secs(2)).unwrap();
141+
assert_eq!(num, 42);
142+
143+
handle.stop();
144+
sys.run().unwrap();
145+
}
146+
125147
#[test]
126148
fn arbiter_drop_no_panic_fn() {
127149
let _ = System::new();

0 commit comments

Comments
 (0)