17
17
//!
18
18
//! ```
19
19
//! # use hyperactor::mailbox::Mailbox;
20
+ //! # use hyperactor::Proc;
20
21
//! # use hyperactor::reference::{ActorId, ProcId, WorldId};
21
22
//! # tokio_test::block_on(async {
23
+ //! # let proc = Proc::local();
24
+ //! # let (client, _) = proc.instance("client").unwrap();
22
25
//! # let proc_id = ProcId::Ranked(WorldId("world".to_string()), 0);
23
26
//! # let actor_id = ActorId(proc_id, "actor".to_string(), 0);
24
27
//! let mbox = Mailbox::new_detached(actor_id);
25
28
//! let (port, mut receiver) = mbox.open_port::<u64>();
26
29
//!
27
- //! port.send(123).unwrap();
30
+ //! port.send(&client, 123).unwrap();
28
31
//! assert_eq!(receiver.recv().await.unwrap(), 123u64);
29
32
//! # })
30
33
//! ```
@@ -1088,7 +1091,9 @@ impl MailboxClient {
1088
1091
tokio:: spawn ( async move {
1089
1092
let result = return_receiver. await ;
1090
1093
if let Ok ( message) = result {
1091
- let _ = return_handle_0. send ( Undeliverable ( message) ) ;
1094
+ // When returning messages, we do not care whether the messages are delivered
1095
+ // out of order.
1096
+ let _ = return_handle_0. anon_send ( Undeliverable ( message) ) ;
1092
1097
} else {
1093
1098
// Sender dropped, this task can end.
1094
1099
}
@@ -1578,9 +1583,24 @@ impl<M: Message> PortHandle<M> {
1578
1583
}
1579
1584
1580
1585
/// Send a message to this port.
1581
- pub fn send ( & self , message : M ) -> Result < ( ) , MailboxSenderError > {
1586
+ pub fn send ( & self , _cx : & impl context :: Actor , message : M ) -> Result < ( ) , MailboxSenderError > {
1582
1587
let mut headers = Attrs :: new ( ) ;
1583
1588
1589
+ crate :: mailbox:: headers:: set_send_timestamp ( & mut headers) ;
1590
+ // TODO(pzhang) Use cx to add SEQ_INFO header.
1591
+
1592
+ self . sender . send ( headers, message) . map_err ( |err| {
1593
+ MailboxSenderError :: new_unbound :: < M > (
1594
+ self . mailbox . actor_id ( ) . clone ( ) ,
1595
+ MailboxSenderErrorKind :: Other ( err) ,
1596
+ )
1597
+ } )
1598
+ }
1599
+
1600
+ /// Send a message to this port without a known sender. This method should
1601
+ /// only be used if you do not care about out-of-ordering delivery.
1602
+ pub fn anon_send ( & self , message : M ) -> Result < ( ) , MailboxSenderError > {
1603
+ let mut headers = Attrs :: new ( ) ;
1584
1604
crate :: mailbox:: headers:: set_send_timestamp ( & mut headers) ;
1585
1605
1586
1606
self . sender . send ( headers, message) . map_err ( |err| {
@@ -2610,6 +2630,7 @@ mod tests {
2610
2630
use crate :: channel:: sim:: SimAddr ;
2611
2631
use crate :: clock:: Clock ;
2612
2632
use crate :: clock:: RealClock ;
2633
+ use crate :: context:: Mailbox as _;
2613
2634
use crate :: data:: Serialized ;
2614
2635
use crate :: id;
2615
2636
use crate :: proc:: Proc ;
@@ -2653,32 +2674,33 @@ mod tests {
2653
2674
2654
2675
#[ tokio:: test]
2655
2676
async fn test_mailbox_accum ( ) {
2656
- let mbox = Mailbox :: new_detached ( id ! ( test[ 0 ] . test) ) ;
2657
- let ( port, mut receiver) = mbox. open_accum_port ( accum:: max :: < i64 > ( ) ) ;
2677
+ let proc = Proc :: local ( ) ;
2678
+ let ( client, _) = proc. instance ( "client" ) . unwrap ( ) ;
2679
+ let ( port, mut receiver) = client. mailbox ( ) . open_accum_port ( accum:: max :: < i64 > ( ) ) ;
2658
2680
2659
2681
for i in -3 ..4 {
2660
- port. send ( i) . unwrap ( ) ;
2682
+ port. send ( & client , i) . unwrap ( ) ;
2661
2683
let received: accum:: Max < i64 > = receiver. recv ( ) . await . unwrap ( ) ;
2662
2684
let msg = received. get ( ) ;
2663
2685
assert_eq ! ( msg, & i) ;
2664
2686
}
2665
2687
// Send a smaller or same value. Should still receive the previous max.
2666
2688
for i in -3 ..4 {
2667
- port. send ( i) . unwrap ( ) ;
2689
+ port. send ( & client , i) . unwrap ( ) ;
2668
2690
assert_eq ! ( receiver. recv( ) . await . unwrap( ) . get( ) , & 3 ) ;
2669
2691
}
2670
2692
// send a larger value. Should receive the new max.
2671
- port. send ( 4 ) . unwrap ( ) ;
2693
+ port. send ( & client , 4 ) . unwrap ( ) ;
2672
2694
assert_eq ! ( receiver. recv( ) . await . unwrap( ) . get( ) , & 4 ) ;
2673
2695
2674
2696
// Send multiple updates. Should only receive the final change.
2675
2697
for i in 5 ..10 {
2676
- port. send ( i) . unwrap ( ) ;
2698
+ port. send ( & client , i) . unwrap ( ) ;
2677
2699
}
2678
2700
assert_eq ! ( receiver. recv( ) . await . unwrap( ) . get( ) , & 9 ) ;
2679
- port. send ( 1 ) . unwrap ( ) ;
2680
- port. send ( 3 ) . unwrap ( ) ;
2681
- port. send ( 2 ) . unwrap ( ) ;
2701
+ port. send ( & client , 1 ) . unwrap ( ) ;
2702
+ port. send ( & client , 3 ) . unwrap ( ) ;
2703
+ port. send ( & client , 2 ) . unwrap ( ) ;
2682
2704
assert_eq ! ( receiver. recv( ) . await . unwrap( ) . get( ) , & 9 ) ;
2683
2705
}
2684
2706
@@ -2706,9 +2728,10 @@ mod tests {
2706
2728
#[ tokio:: test]
2707
2729
#[ ignore] // error behavior changed, but we will bring it back
2708
2730
async fn test_mailbox_once ( ) {
2709
- let mbox = Mailbox :: new_detached ( id ! ( test[ 0 ] . test) ) ;
2731
+ let proc = Proc :: local ( ) ;
2732
+ let ( client, _) = proc. instance ( "client" ) . unwrap ( ) ;
2710
2733
2711
- let ( port, receiver) = mbox . open_once_port :: < u64 > ( ) ;
2734
+ let ( port, receiver) = client . open_once_port :: < u64 > ( ) ;
2712
2735
2713
2736
// let port_id = port.port_id().clone();
2714
2737
@@ -2977,19 +3000,20 @@ mod tests {
2977
3000
2978
3001
#[ tokio:: test]
2979
3002
async fn test_enqueue_port ( ) {
2980
- let mbox = Mailbox :: new_detached ( id ! ( test[ 0 ] . test) ) ;
3003
+ let proc = Proc :: local ( ) ;
3004
+ let ( client, _) = proc. instance ( "client" ) . unwrap ( ) ;
2981
3005
2982
3006
let count = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
2983
3007
let count_clone = count. clone ( ) ;
2984
- let port = mbox . open_enqueue_port ( move |_, n| {
3008
+ let port = client . mailbox ( ) . open_enqueue_port ( move |_, n| {
2985
3009
count_clone. fetch_add ( n, Ordering :: SeqCst ) ;
2986
3010
Ok ( ( ) )
2987
3011
} ) ;
2988
3012
2989
- port. send ( 10 ) . unwrap ( ) ;
2990
- port. send ( 5 ) . unwrap ( ) ;
2991
- port. send ( 1 ) . unwrap ( ) ;
2992
- port. send ( 0 ) . unwrap ( ) ;
3013
+ port. send ( & client , 10 ) . unwrap ( ) ;
3014
+ port. send ( & client , 5 ) . unwrap ( ) ;
3015
+ port. send ( & client , 1 ) . unwrap ( ) ;
3016
+ port. send ( & client , 0 ) . unwrap ( ) ;
2993
3017
2994
3018
assert_eq ! ( count. load( Ordering :: SeqCst ) , 16 ) ;
2995
3019
}
@@ -3054,6 +3078,7 @@ mod tests {
3054
3078
let proc_id = id ! ( quux[ 0 ] ) ;
3055
3079
let mut proc = Proc :: new ( proc_id. clone ( ) , proc_forwarder) ;
3056
3080
ProcSupervisionCoordinator :: set ( & proc) . await . unwrap ( ) ;
3081
+ let ( client, _) = proc. instance ( "client" ) . unwrap ( ) ;
3057
3082
3058
3083
let foo = proc. spawn :: < Foo > ( "foo" , ( ) ) . await . unwrap ( ) ;
3059
3084
let return_handle = foo. port :: < Undeliverable < MessageEnvelope > > ( ) ;
@@ -3063,7 +3088,7 @@ mod tests {
3063
3088
Serialized :: serialize ( & 1u64 ) . unwrap ( ) ,
3064
3089
Attrs :: new ( ) ,
3065
3090
) ;
3066
- return_handle. send ( Undeliverable ( message) ) . unwrap ( ) ;
3091
+ return_handle. send ( & client , Undeliverable ( message) ) . unwrap ( ) ;
3067
3092
3068
3093
RealClock
3069
3094
. sleep ( tokio:: time:: Duration :: from_millis ( 100 ) )
@@ -3095,7 +3120,9 @@ mod tests {
3095
3120
Serialized :: serialize ( & 1u64 ) . unwrap ( ) ,
3096
3121
Attrs :: new ( ) ,
3097
3122
) ;
3098
- return_handle. send ( Undeliverable ( envelope. clone ( ) ) ) . unwrap ( ) ;
3123
+ return_handle
3124
+ . anon_send ( Undeliverable ( envelope. clone ( ) ) )
3125
+ . unwrap ( ) ;
3099
3126
// Check we receive the undelivered message.
3100
3127
assert ! (
3101
3128
RealClock
@@ -3747,12 +3774,13 @@ mod tests {
3747
3774
3748
3775
#[ tokio:: test]
3749
3776
async fn test_port_contramap ( ) {
3750
- let mbox = Mailbox :: new_detached ( id ! ( test[ 0 ] . test) ) ;
3751
- let ( handle, mut rx) = mbox. open_port ( ) ;
3777
+ let proc = Proc :: local ( ) ;
3778
+ let ( client, _) = proc. instance ( "client" ) . unwrap ( ) ;
3779
+ let ( handle, mut rx) = client. open_port ( ) ;
3752
3780
3753
3781
handle
3754
3782
. contramap ( |m| ( 1 , m) )
3755
- . send ( "hello" . to_string ( ) )
3783
+ . send ( & client , "hello" . to_string ( ) )
3756
3784
. unwrap ( ) ;
3757
3785
assert_eq ! ( rx. recv( ) . await . unwrap( ) , ( 1 , "hello" . to_string( ) ) ) ;
3758
3786
}
0 commit comments