@@ -14,6 +14,7 @@ use crate::transport::topology::{CollectionType, ColumnKind, CqlType, NativeType
1414use crate :: CachingSession ;
1515use crate :: QueryResult ;
1616use crate :: { IntoTypedRows , Session , SessionBuilder } ;
17+ use assert_matches:: assert_matches;
1718use bytes:: Bytes ;
1819use futures:: { FutureExt , StreamExt } ;
1920use itertools:: Itertools ;
@@ -1181,6 +1182,74 @@ async fn test_timestamp() {
11811182 assert_eq ! ( results, expected_results) ;
11821183}
11831184
1185+ #[ ignore = "works on remote Scylla instances only (local ones are too fast)" ]
1186+ #[ tokio:: test]
1187+ async fn test_request_timeout ( ) {
1188+ use std:: time:: Duration ;
1189+ let uri = std:: env:: var ( "SCYLLA_URI" ) . unwrap_or_else ( |_| "127.0.0.1:9042" . to_string ( ) ) ;
1190+
1191+ {
1192+ let session = SessionBuilder :: new ( )
1193+ . known_node ( uri. as_str ( ) )
1194+ . build ( )
1195+ . await
1196+ . unwrap ( ) ;
1197+
1198+ let mut query: Query = Query :: new ( "SELECT * FROM system_schema.tables" ) ;
1199+ query. set_request_timeout ( Some ( Duration :: from_millis ( 1 ) ) ) ;
1200+ match session. query ( query, & [ ] ) . await {
1201+ Ok ( _) => panic ! ( "the query should have failed due to a client-side timeout" ) ,
1202+ Err ( e) => assert_matches ! ( e, QueryError :: RequestTimeout ( _) ) ,
1203+ }
1204+
1205+ let mut prepared = session
1206+ . prepare ( "SELECT * FROM system_schema.tables" )
1207+ . await
1208+ . unwrap ( ) ;
1209+
1210+ prepared. set_request_timeout ( Some ( Duration :: from_millis ( 1 ) ) ) ;
1211+ match session. execute ( & prepared, & [ ] ) . await {
1212+ Ok ( _) => panic ! ( "the prepared query should have failed due to a client-side timeout" ) ,
1213+ Err ( e) => assert_matches ! ( e, QueryError :: RequestTimeout ( _) ) ,
1214+ } ;
1215+ }
1216+ {
1217+ let timeouting_session = SessionBuilder :: new ( )
1218+ . known_node ( uri)
1219+ . request_timeout ( Some ( Duration :: from_millis ( 1 ) ) )
1220+ . build ( )
1221+ . await
1222+ . unwrap ( ) ;
1223+
1224+ let mut query = Query :: new ( "SELECT * FROM system_schema.tables" ) ;
1225+
1226+ match timeouting_session. query ( query. clone ( ) , & [ ] ) . await {
1227+ Ok ( _) => panic ! ( "the query should have failed due to a client-side timeout" ) ,
1228+ Err ( e) => assert_matches ! ( e, QueryError :: RequestTimeout ( _) ) ,
1229+ } ;
1230+
1231+ query. set_request_timeout ( Some ( Duration :: from_secs ( 10000 ) ) ) ;
1232+
1233+ timeouting_session. query ( query, & [ ] ) . await . expect (
1234+ "the query should have not failed, because no client-side timeout was specified" ,
1235+ ) ;
1236+
1237+ let mut prepared = timeouting_session
1238+ . prepare ( "SELECT * FROM system_schema.tables" )
1239+ . await
1240+ . unwrap ( ) ;
1241+
1242+ match timeouting_session. execute ( & prepared, & [ ] ) . await {
1243+ Ok ( _) => panic ! ( "the prepared query should have failed due to a client-side timeout" ) ,
1244+ Err ( e) => assert_matches ! ( e, QueryError :: RequestTimeout ( _) ) ,
1245+ } ;
1246+
1247+ prepared. set_request_timeout ( Some ( Duration :: from_secs ( 10000 ) ) ) ;
1248+
1249+ timeouting_session. execute ( & prepared, & [ ] ) . await . expect ( "the prepared query should have not failed, because no client-side timeout was specified" ) ;
1250+ }
1251+ }
1252+
11841253#[ tokio:: test]
11851254async fn test_prepared_config ( ) {
11861255 let uri = std:: env:: var ( "SCYLLA_URI" ) . unwrap_or_else ( |_| "127.0.0.1:9042" . to_string ( ) ) ;
0 commit comments