@@ -58,6 +58,8 @@ impl<'a> Progress<'a> {
5858 /// interval_ms = The frequency at which the server should be polled. Default = 50ms
5959 /// timeout_ms = The maximum time to wait for processing to complete. Default = 5000ms
6060 ///
61+ /// If the time waited excedes timeout_ms then None will be returned.
62+ ///
6163 /// # Example
6264 ///
6365 /// ```
@@ -91,14 +93,14 @@ impl<'a> Progress<'a> {
9193 /// let status = progress.wait_for_pending_update(None, None).await.unwrap();
9294 ///
9395 /// # client.delete_index("movies_wait_for_pending").await.unwrap();
94- /// assert!(matches!(status, UpdateStatus::Processed { .. }));
96+ /// assert!(matches!(status.unwrap() , UpdateStatus::Processed { .. }));
9597 /// # });
9698 /// ```
9799 pub async fn wait_for_pending_update (
98100 & self ,
99101 interval_ms : Option < Duration > ,
100102 timeout_ms : Option < Duration > ,
101- ) -> Result < UpdateStatus , Error > {
103+ ) -> Option < Result < UpdateStatus , Error > > {
102104 let interval: Duration ;
103105 let timeout: Duration ;
104106
@@ -113,30 +115,28 @@ impl<'a> Progress<'a> {
113115 }
114116
115117 let mut elapsed_time = Duration :: new ( 0 , 0 ) ;
116- let mut status : UpdateStatus ;
118+ let mut status_result : Result < UpdateStatus , Error > ;
117119
118120 while timeout > elapsed_time {
119- status = self . get_status ( ) . await ? ;
121+ status_result = self . get_status ( ) . await ;
120122
121- match status {
122- UpdateStatus :: Failed { .. } | UpdateStatus :: Processed { .. } => {
123- return self . get_status ( ) . await ;
124- } ,
125- UpdateStatus :: Enqueued { .. } => {
126- elapsed_time += interval;
127- async_sleep ( interval) . await ;
123+ match status_result {
124+ Ok ( status) => {
125+ match status {
126+ UpdateStatus :: Failed { .. } | UpdateStatus :: Processed { .. } => {
127+ return Some ( self . get_status ( ) . await ) ;
128+ } ,
129+ UpdateStatus :: Enqueued { .. } => {
130+ elapsed_time += interval;
131+ async_sleep ( interval) . await ;
132+ } ,
133+ }
128134 } ,
135+ Err ( error) => return Some ( Err ( error) ) ,
129136 } ;
130137 }
131138
132- Err (
133- Error :: MeiliSearchTimeoutError {
134- message : format ! (
135- "timeout of {:?}ms has been exceeded when waiting for pending update to resolve." ,
136- timeout,
137- ) ,
138- }
139- )
139+ None
140140 }
141141}
142142
@@ -290,7 +290,31 @@ mod test {
290290 ) . await . unwrap ( ) ;
291291
292292 client. delete_index ( "movies_wait_for_pending_args" ) . await . unwrap ( ) ;
293- assert ! ( matches!( status, UpdateStatus :: Processed { .. } ) ) ;
293+ assert ! ( matches!( status. unwrap( ) , UpdateStatus :: Processed { .. } ) ) ;
294+ }
295+
296+ #[ async_test]
297+ async fn test_wait_for_pending_updates_time_out ( ) {
298+ let client = Client :: new ( "http://localhost:7700" , "masterKey" ) ;
299+ let movies = client. create_index ( "movies_wait_for_pending_timeout" , None ) . await . unwrap ( ) ;
300+ let progress = movies. add_documents ( & [
301+ Document {
302+ id : 0 ,
303+ kind : "title" . into ( ) ,
304+ value : "The Social Network" . to_string ( ) ,
305+ } ,
306+ Document {
307+ id : 1 ,
308+ kind : "title" . into ( ) ,
309+ value : "Harry Potter and the Sorcerer's Stone" . to_string ( ) ,
310+ } ,
311+ ] , None ) . await . unwrap ( ) ;
312+ let status = progress. wait_for_pending_update (
313+ Some ( Duration :: from_millis ( 1 ) ) , Some ( Duration :: from_nanos ( 1 ) )
314+ ) . await ;
315+
316+ client. delete_index ( "movies_wait_for_pending_timeout" ) . await . unwrap ( ) ;
317+ assert_eq ! ( status. is_none( ) , true ) ;
294318 }
295319
296320 #[ async_test]
0 commit comments