Skip to content

Commit e80133d

Browse files
sanders41ppamorim
authored andcommitted
Returning None on timeout
1 parent cd248f4 commit e80133d

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/errors.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ pub enum Error {
1515
error_link: String,
1616
},
1717

18-
/// The MeiliSearch request timed out.
19-
MeiliSearchTimeoutError { message: String },
2018
/// There is no MeiliSearch server listening on the [specified host]
2119
/// (../client/struct.Client.html#method.new).
2220
UnreachableServer,
@@ -267,7 +265,6 @@ impl std::fmt::Display for Error {
267265
message,
268266
error_link,
269267
),
270-
Error::MeiliSearchTimeoutError { message } => write!(fmt, "MeiliSearch {}", message),
271268
Error::UnreachableServer => write!(fmt, "The MeiliSearch server can't be reached."),
272269
Error::InvalidRequest => write!(fmt, "Unable to generate a valid HTTP request. It probably comes from an invalid API key."),
273270
Error::ParseError(e) => write!(fmt, "Error parsing response JSON: {}", e),

src/progress.rs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)