@@ -996,4 +996,142 @@ mod client {
996996 assert ! ( stats. out_bytes. load( Ordering :: Relaxed ) != 0 ) ;
997997 assert_eq ! ( stats. connects. load( Ordering :: Relaxed ) , 2 ) ;
998998 }
999+
1000+ #[ tokio:: test]
1001+ async fn drain_subscription_basic ( ) {
1002+ use std:: error:: Error ;
1003+ let server = nats_server:: run_basic_server ( ) ;
1004+ let client = async_nats:: connect ( server. client_url ( ) ) . await . unwrap ( ) ;
1005+
1006+ let mut sub = client. subscribe ( "test" ) . await . unwrap ( ) ;
1007+
1008+ // publish some data
1009+ client. publish ( "test" , "data" . into ( ) ) . await . unwrap ( ) ;
1010+ client. flush ( ) . await . unwrap ( ) ;
1011+
1012+ // confirm we receive that data
1013+ assert ! ( sub. next( ) . await . is_some( ) ) ;
1014+
1015+ // now drain the subscription
1016+ let result = sub. drain ( ) . await ;
1017+ match result {
1018+ Ok ( ( ) ) => println ! ( "ok" ) ,
1019+ Err ( err) => {
1020+ println ! ( "error: {}" , err) ;
1021+ println ! ( "source: {:?}" , err. source( ) )
1022+ }
1023+ }
1024+
1025+ // assert the stream is closed after draining
1026+ assert ! ( sub. next( ) . await . is_none( ) ) ;
1027+
1028+ // confirm we can still reconnect and send messages on a new subscription
1029+ let mut sub2 = client. subscribe ( "test2" ) . await . unwrap ( ) ;
1030+ client. publish ( "test2" , "data" . into ( ) ) . await . unwrap ( ) ;
1031+ client. flush ( ) . await . unwrap ( ) ;
1032+ assert ! ( sub2. next( ) . await . is_some( ) ) ;
1033+ }
1034+
1035+ #[ tokio:: test]
1036+ async fn drain_subscription_unsub_after ( ) {
1037+ let server = nats_server:: run_basic_server ( ) ;
1038+ let client = async_nats:: connect ( server. client_url ( ) ) . await . unwrap ( ) ;
1039+
1040+ let mut sub = client. subscribe ( "test" ) . await . unwrap ( ) ;
1041+
1042+ sub. unsubscribe_after ( 120 )
1043+ . await
1044+ . expect ( "Expected to send unsub_after" ) ;
1045+
1046+ // publish some data
1047+ client. publish ( "test" , "data" . into ( ) ) . await . unwrap ( ) ;
1048+ client. publish ( "test" , "data" . into ( ) ) . await . unwrap ( ) ;
1049+ client. flush ( ) . await . unwrap ( ) ;
1050+
1051+ // Send the drain command
1052+ sub. drain ( ) . await . expect ( "Expected to drain the sub" ) ;
1053+
1054+ // we should receive all published data then close immediately
1055+ assert ! ( sub. next( ) . await . is_some( ) ) ;
1056+ assert ! ( sub. next( ) . await . is_some( ) ) ;
1057+ assert ! ( sub. next( ) . await . is_none( ) ) ;
1058+ }
1059+
1060+ #[ tokio:: test]
1061+ async fn drain_subscription_active ( ) {
1062+ let server = nats_server:: run_basic_server ( ) ;
1063+ let client = async_nats:: connect ( server. client_url ( ) ) . await . unwrap ( ) ;
1064+
1065+ // spawn a task to constantly write to the subscription
1066+ let constant_writer = tokio:: spawn ( {
1067+ let client = client. clone ( ) ;
1068+ async move {
1069+ loop {
1070+ client. publish ( "test" , "data" . into ( ) ) . await . unwrap ( ) ;
1071+ client. flush ( ) . await . unwrap ( ) ;
1072+ }
1073+ }
1074+ } ) ;
1075+
1076+ let mut sub = client. subscribe ( "test" ) . await . unwrap ( ) ;
1077+
1078+ // confirm we receive some data
1079+ assert ! ( sub. next( ) . await . is_some( ) ) ;
1080+
1081+ // now drain the subscription
1082+ sub. drain ( ) . await . unwrap ( ) ;
1083+
1084+ // yield to the runtime to ensure constant_writer gets a chance to publish a message or two to the subject
1085+ tokio:: time:: sleep ( Duration :: from_millis ( 1 ) ) . await ;
1086+
1087+ // assert the subscription stream is closed after draining
1088+ let sleep_fut = async move { while let Some ( _) = sub. next ( ) . await { } } ;
1089+ tokio:: time:: timeout ( Duration :: from_secs ( 10 ) , sleep_fut)
1090+ . await
1091+ . expect ( "Expected stream to drain within 10s" ) ;
1092+
1093+ // assert constant_writer doesn't fail to write after the only sub is drained (i.e. client operations still work fine)
1094+ assert ! ( !constant_writer. is_finished( ) ) ;
1095+
1096+ // confirm we can still reconnect and receive messages on the same subject on a new subscription
1097+ let mut sub2 = client. subscribe ( "test" ) . await . unwrap ( ) ;
1098+ assert ! ( sub2. next( ) . await . is_some( ) ) ;
1099+ }
1100+
1101+ #[ tokio:: test]
1102+ async fn drain_client_basic ( ) {
1103+ let server = nats_server:: run_basic_server ( ) ;
1104+ let client = async_nats:: connect ( server. client_url ( ) ) . await . unwrap ( ) ;
1105+
1106+ let mut sub = client. subscribe ( "test" ) . await . unwrap ( ) ;
1107+
1108+ // publish some data
1109+ client. publish ( "test" , "data" . into ( ) ) . await . unwrap ( ) ;
1110+ client. flush ( ) . await . unwrap ( ) ;
1111+
1112+ // confirm we receive that data
1113+ assert ! ( sub. next( ) . await . is_some( ) ) ;
1114+
1115+ // now drain the client
1116+ client. drain ( ) . await . unwrap ( ) ;
1117+
1118+ // assert the sub's stream is closed after draining
1119+ assert ! ( sub. next( ) . await . is_none( ) ) ;
1120+
1121+ // we should not be able to perform any more operations on a drained client
1122+ client
1123+ . subscribe ( "test2" )
1124+ . await
1125+ . expect_err ( "Expected client to be drained" ) ;
1126+
1127+ client
1128+ . publish ( "test" , "data" . into ( ) )
1129+ . await
1130+ . expect_err ( "Expected client to be drained" ) ;
1131+
1132+ // we should be able to connect with a new client
1133+ let _client2 = async_nats:: connect ( server. client_url ( ) )
1134+ . await
1135+ . expect ( "Expected to be able to create a new client" ) ;
1136+ }
9991137}
0 commit comments