@@ -10,17 +10,14 @@ use wasm_bindgen_test::wasm_bindgen_test as maybe_tokio_test;
10
10
#[ cfg( not( all( target_arch = "wasm32" , not( target_os = "wasi" ) ) ) ) ]
11
11
use tokio:: test as maybe_tokio_test;
12
12
13
- use tokio:: join;
14
13
use tokio:: sync:: mpsc:: error:: { TryRecvError , TrySendError } ;
15
14
use tokio:: sync:: mpsc:: { self , channel} ;
16
15
use tokio:: sync:: oneshot;
17
- use tokio:: time;
18
16
use tokio_test:: * ;
19
17
20
18
use std:: sync:: atomic:: AtomicUsize ;
21
19
use std:: sync:: atomic:: Ordering :: { Acquire , Release } ;
22
20
use std:: sync:: Arc ;
23
- use std:: time:: Duration ;
24
21
25
22
#[ cfg( not( target_arch = "wasm32" ) ) ]
26
23
mod support {
@@ -838,47 +835,36 @@ impl Drop for Msg {
838
835
// `Sender` was dropped while more than one `WeakSender` remains, we want to
839
836
// ensure that no messages are kept in the channel, which were sent after
840
837
// the receiver was dropped.
841
- #[ tokio:: test( start_paused = true ) ]
838
+ #[ tokio:: test]
842
839
async fn test_msgs_dropped_on_rx_drop ( ) {
843
- fn ms ( millis : u64 ) -> Duration {
844
- Duration :: from_millis ( millis)
845
- }
846
-
847
840
let ( tx, mut rx) = mpsc:: channel ( 3 ) ;
848
841
849
- let rx_handle = tokio:: spawn ( async move {
850
- let _ = rx. recv ( ) . await . unwrap ( ) ;
851
- let _ = rx. recv ( ) . await . unwrap ( ) ;
852
- time:: sleep ( ms ( 1 ) ) . await ;
853
- drop ( rx) ;
842
+ let _ = tx. send ( Msg { } ) . await . unwrap ( ) ;
843
+ let _ = tx. send ( Msg { } ) . await . unwrap ( ) ;
854
844
855
- time :: advance ( ms ( 1 ) ) . await ;
856
- } ) ;
845
+ // This msg will be pending and should be dropped when `rx` is dropped
846
+ let sent_fut = tx . send ( Msg { } ) ;
857
847
858
- let tx_handle = tokio:: spawn ( async move {
859
- let _ = tx. send ( Msg { } ) . await . unwrap ( ) ;
860
- let _ = tx. send ( Msg { } ) . await . unwrap ( ) ;
848
+ let _ = rx. recv ( ) . await . unwrap ( ) ;
849
+ let _ = rx. recv ( ) . await . unwrap ( ) ;
861
850
862
- // This msg will be pending and should be dropped when `rx` is dropped
863
- let _ = tx. send ( Msg { } ) . await . unwrap ( ) ;
864
- time:: advance ( ms ( 1 ) ) . await ;
851
+ let _ = sent_fut. await . unwrap ( ) ;
865
852
866
- // This msg will not be put onto `Tx` list anymore, since `Rx` is closed.
867
- time:: sleep ( ms ( 1 ) ) . await ;
868
- assert ! ( tx. send( Msg { } ) . await . is_err( ) ) ;
853
+ drop ( rx) ;
869
854
870
- // Ensure that third message isn't put onto the channel anymore
871
- assert_eq ! ( NUM_DROPPED . load( Acquire ) , 4 ) ;
872
- } ) ;
855
+ assert_eq ! ( NUM_DROPPED . load( Acquire ) , 3 ) ;
873
856
874
- let ( _, _) = join ! ( rx_handle, tx_handle) ;
857
+ // This msg will not be put onto `Tx` list anymore, since `Rx` is closed.
858
+ assert ! ( tx. send( Msg { } ) . await . is_err( ) ) ;
859
+
860
+ assert_eq ! ( NUM_DROPPED . load( Acquire ) , 4 ) ;
875
861
}
876
862
877
863
// Tests that a `WeakSender` is upgradeable when other `Sender`s exist.
878
864
#[ tokio:: test]
879
865
async fn downgrade_upgrade_sender_success ( ) {
880
866
let ( tx, _rx) = mpsc:: channel :: < i32 > ( 1 ) ;
881
- let weak_tx = tx. clone ( ) . downgrade ( ) ;
867
+ let weak_tx = tx. downgrade ( ) ;
882
868
assert ! ( weak_tx. upgrade( ) . is_some( ) ) ;
883
869
}
884
870
@@ -897,6 +883,7 @@ async fn downgrade_upgrade_sender_failure() {
897
883
async fn downgrade_drop_upgrade ( ) {
898
884
let ( tx, _rx) = mpsc:: channel :: < i32 > ( 1 ) ;
899
885
886
+ // the cloned `Tx` is dropped right away
900
887
let weak_tx = tx. clone ( ) . downgrade ( ) ;
901
888
drop ( tx) ;
902
889
assert ! ( weak_tx. upgrade( ) . is_none( ) ) ;
@@ -907,7 +894,7 @@ async fn downgrade_drop_upgrade() {
907
894
#[ tokio:: test]
908
895
async fn downgrade_get_permit_upgrade_no_senders ( ) {
909
896
let ( tx, _rx) = mpsc:: channel :: < i32 > ( 1 ) ;
910
- let weak_tx = tx. clone ( ) . downgrade ( ) ;
897
+ let weak_tx = tx. downgrade ( ) ;
911
898
let _permit = tx. reserve_owned ( ) . await . unwrap ( ) ;
912
899
assert ! ( weak_tx. upgrade( ) . is_some( ) ) ;
913
900
}
@@ -920,12 +907,13 @@ async fn downgrade_upgrade_get_permit_no_senders() {
920
907
let tx2 = tx. clone ( ) ;
921
908
let _permit = tx. reserve_owned ( ) . await . unwrap ( ) ;
922
909
let weak_tx = tx2. downgrade ( ) ;
910
+ drop ( tx2) ;
923
911
assert ! ( weak_tx. upgrade( ) . is_some( ) ) ;
924
912
}
925
913
926
- // Tests that `Clone` of `WeakSender` doesn't decrement `tx_count`.
914
+ // Tests that `downgrade` does not change the `tx_count` of the channel .
927
915
#[ tokio:: test]
928
- async fn test_weak_sender_clone ( ) {
916
+ async fn test_tx_count_weak_sender ( ) {
929
917
let ( tx, _rx) = mpsc:: channel :: < i32 > ( 1 ) ;
930
918
let tx_weak = tx. downgrade ( ) ;
931
919
let tx_weak2 = tx. downgrade ( ) ;
0 commit comments