@@ -922,12 +922,14 @@ mod endpoint_tests {
922922 tracing:: subscriber:: set_global_default ( subscriber) . unwrap ( ) ;
923923
924924 let request_count = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
925+ let done = Arc :: new ( tokio:: sync:: Notify :: new ( ) ) ;
925926 let listener = TcpListener :: bind ( "127.0.0.1:0" ) . await ?;
926927 let addr = listener. local_addr ( ) ?;
927928 let base: http:: Uri = format ! ( "http://{addr}" ) . parse ( ) ?;
928929
929930 let server_handle = {
930931 let request_count = request_count. clone ( ) ;
932+ let done = done. clone ( ) ;
931933 tokio:: spawn ( async move {
932934 loop {
933935 let ( tcp, _) = match listener. accept ( ) . await {
@@ -936,8 +938,10 @@ mod endpoint_tests {
936938 } ;
937939
938940 let request_count = request_count. clone ( ) ;
941+ let done = done. clone ( ) ;
939942 let service = service_fn ( move |req : Request < Incoming > | {
940943 let request_count = request_count. clone ( ) ;
944+ let done = done. clone ( ) ;
941945 async move {
942946 let ( parts, body) = req. into_parts ( ) ;
943947 if parts. method == Method :: POST {
@@ -957,6 +961,7 @@ mod endpoint_tests {
957961 . unwrap ( ) ;
958962 return Ok :: < _ , Infallible > ( res) ;
959963 } else {
964+ done. notify_one ( ) ;
960965 let res = Response :: builder ( )
961966 . status ( StatusCode :: NO_CONTENT )
962967 . body ( Full :: new ( Bytes :: new ( ) ) )
@@ -1023,14 +1028,9 @@ mod endpoint_tests {
10231028
10241029 let runtime_handle = tokio:: spawn ( async move { runtime. run_concurrent ( ) . await } ) ;
10251030
1026- loop {
1027- tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
1028- let count = request_count. load ( Ordering :: SeqCst ) ;
1029- if count >= 300 {
1030- tokio:: time:: sleep ( Duration :: from_millis ( 500 ) ) . await ;
1031- break ;
1032- }
1033- }
1031+ done. notified ( ) . await ;
1032+ // Give handlers time to complete after server signals done
1033+ tokio:: time:: sleep ( Duration :: from_millis ( 500 ) ) . await ;
10341034
10351035 runtime_handle. abort ( ) ;
10361036 server_handle. abort ( ) ;
0 commit comments