-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.rs
323 lines (292 loc) · 10 KB
/
sync.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use std::{
sync::{Arc, Mutex},
thread::sleep,
time::{Duration, Instant},
};
use crate::{
SituwaitionBase, SituwaitionError, SituwaitionOpts, SyncSituwaition, WaiterCreationError,
DEFAULT_SITUWAITION_CHECK_INTERVAL_MS, DEFAULT_SITUWAITION_TIMEOUT_MS,
};
/// Synchronous situwaitioner
#[allow(dead_code)]
pub struct SyncWaiter<R, E, F>
where
R: Send,
E: Send + 'static,
F: Fn() -> Result<R, E> + Send + Sync + 'static,
{
/// Options for the situwaition
opts: SituwaitionOpts,
/// Function that can be run to decide whether the executor should finish
check_fn: Option<Box<F>>,
}
impl<R, E, F> SituwaitionBase for SyncWaiter<R, E, F>
where
R: Send,
E: Send + 'static,
F: Fn() -> Result<R, E> + Send + Sync + 'static,
{
type Result = R;
type Error = E;
fn options(&self) -> &SituwaitionOpts {
&self.opts
}
fn set_options(
&mut self,
update_fn: impl Fn(&SituwaitionOpts) -> SituwaitionOpts,
) -> Result<(), SituwaitionError<()>> {
self.opts = update_fn(&self.opts);
Ok(())
}
}
impl<R, E, F> SyncSituwaition for SyncWaiter<R, E, F>
where
R: Send + 'static,
E: Send + 'static,
F: Fn() -> Result<R, E> + Send + Sync + 'static,
{
fn exec(&mut self) -> Result<R, SituwaitionError<E>> {
let start = Instant::now();
let check_fn = self
.check_fn
.take()
.ok_or_else(|| SituwaitionError::UnexpectedError("no check fn specified".into()))?;
let result = Mutex::new(None);
let result_read = Arc::new(result);
let result_write = result_read.clone();
let err: Mutex<Option<SituwaitionError<E>>> = Mutex::new(None);
let err_read = Arc::new(err);
let err_write = err_read.clone();
let timeout = self.opts.timeout;
let check_cooldown = self.opts.check_cooldown;
// We run the check function in a scoped thread in order to ensure
// that we can handle the case where the check function never returns in time
std::thread::spawn(move || loop {
let res = check_fn();
match res {
Ok(v) => {
let mut mutex = result_write.lock().map_err(|_| ())?;
*mutex = Some(v);
break Ok::<(), ()>(());
}
Err(e) => {
let now = Instant::now();
if now - start > timeout {
let mut mutex = err_write.lock().map_err(|_| ())?;
*mutex = Some(SituwaitionError::TimeoutError(e));
break Ok::<(), ()>(());
}
// If a cooldown was specified, wait a bit
if let Some(t) = check_cooldown {
sleep(t);
}
}
}
});
// Wait until the handle is finished, without being blocked by it
loop {
// Sleep & check for timeout until finished
match (result_read.lock(), err_read.lock()) {
(Ok(mut r), Ok(mut err)) => {
// If an error has returned, it *must* be the timeout error, return that quickly
if err.is_some() {
return Err((*err).take().unwrap());
}
// If we've timed out otherwise while doing the check, we timed out *during* a check
if Instant::now() - start > self.opts.timeout {
return Err(SituwaitionError::CheckTimeoutError);
}
// If a result came through, we can return that happily
if r.is_some() {
return Ok((*r).take().unwrap());
}
}
_ => panic!("failed to lock mutex"),
}
// Sleep before checking again
sleep(self.opts.check_interval);
}
}
}
#[allow(dead_code)]
impl<R, E, F> SyncWaiter<R, E, F>
where
R: Send + 'static,
E: Send + 'static,
F: Fn() -> Result<R, E> + Send + Sync + 'static,
{
pub fn from_fn(check_fn: F) -> Self {
SyncWaiter {
opts: SituwaitionOpts::default(),
check_fn: Some(Box::new(check_fn)),
}
}
/// Create a sync executor with options fully specified
pub fn with_opts(check_fn: F, opts: SituwaitionOpts) -> Self {
SyncWaiter {
opts,
check_fn: Some(Box::new(check_fn)),
}
}
/// Create a SyncWaiter with only timeout customized
pub fn with_timeout(check_fn: F, timeout: Duration) -> Result<Self, WaiterCreationError> {
if timeout < Duration::from_millis(DEFAULT_SITUWAITION_CHECK_INTERVAL_MS) {
return Err(WaiterCreationError::InvalidTimeout(
format!("supplied timeout ({}ms) is shorter the default timeout ({DEFAULT_SITUWAITION_CHECK_INTERVAL_MS}ms)", timeout.as_millis())
));
}
Ok(Self::with_opts(
check_fn,
SituwaitionOpts {
timeout,
..SituwaitionOpts::default()
},
))
}
/// Create a SyncWaiter with only check interval customized
pub fn with_check_interval(
check_fn: F,
check_interval: Duration,
) -> Result<Self, WaiterCreationError> {
if check_interval > Duration::from_millis(DEFAULT_SITUWAITION_TIMEOUT_MS) {
return Err(WaiterCreationError::InvalidTimeout(
format!("supplied check interval ({}ms) is larger than the default timeout ({DEFAULT_SITUWAITION_TIMEOUT_MS}ms)", check_interval.as_millis())
));
}
Ok(Self::with_opts(
check_fn,
SituwaitionOpts {
check_interval,
..SituwaitionOpts::default()
},
))
}
}
/////////////////////
// Implementations //
/////////////////////
/// Wait for a given function to resolve with a given result.
///
/// Returning a result (as opposed to the error) will end waiting, otherwise
/// the function will be retried up until the default timeout (see SituwaitionOpts)
#[allow(dead_code)]
pub fn wait_for<R, E, F>(check_fn: F) -> Result<R, SituwaitionError<E>>
where
R: Send + 'static,
E: std::error::Error + Send + 'static,
F: Fn() -> Result<R, E> + Send + Sync + 'static,
{
SyncWaiter::from_fn(check_fn).exec()
}
#[cfg(all(test, not(any(feature = "async-std", feature = "tokio"))))]
mod tests {
use std::io::ErrorKind;
use super::*;
#[test]
fn test_unit_wait_for_fn() {
assert!(
matches!(wait_for(|| Ok::<bool, std::io::Error>(true)), Ok(true)),
"wait_for_fn with a simple fn is true"
);
}
#[test]
fn test_unit_sync_executor_from_fn() {
assert!(
matches!(
SyncWaiter::from_fn(|| Ok::<bool, std::io::Error>(true)).exec(),
Ok(true)
),
"wait_for_fn with a simple fn is true"
);
}
#[test]
fn test_unit_sync_executor_exec_fail() {
assert!(matches!(
SyncWaiter::with_timeout(
|| Err::<(), std::io::Error>(std::io::Error::new(ErrorKind::Other, "test")),
Duration::from_millis(500)
)
.expect("failed to create")
.exec(),
Err(SituwaitionError::TimeoutError(std::io::Error { .. })),
),);
}
#[test]
fn test_unit_sync_executor_exec_pass() {
assert!(
matches!(
SyncWaiter::with_check_interval(
|| Ok::<bool, std::io::Error>(true),
Duration::from_millis(100)
)
.expect("failed to create")
.exec(),
Ok(true)
),
"always passing check passes in 100m with check interval of 100ms"
);
}
#[test]
fn test_unit_wait_for_sync_executor_with_timeout() {
let start = Instant::now();
assert!(
matches!(
SyncWaiter::with_timeout(
|| Err::<(), std::io::Error>(std::io::Error::new(ErrorKind::Other, "test")),
Duration::from_millis(500)
)
.expect("failed to create")
.exec(),
Err(SituwaitionError::TimeoutError(std::io::Error { .. })),
),
"always erroring check fails in 100ms with timeout of 100ms"
);
assert!(
Instant::now() - start >= Duration::from_millis(500),
"failing check passed faster than timeout"
);
}
#[test]
fn test_unit_sync_executor_with_check_interval() {
let start = Instant::now();
assert!(
matches!(
SyncWaiter::with_check_interval(
|| Ok::<bool, std::io::Error>(true),
Duration::from_millis(100)
)
.expect("failed to create")
.exec(),
Ok(true)
),
"always passing check passes in 100m with check interval of 100ms"
);
assert!(
Instant::now() - start < Duration::from_millis(250),
"passed faster than default interval"
);
}
#[test]
fn test_unit_sync_executor_with_long_check() {
let start = Instant::now();
assert!(
matches!(
SyncWaiter::with_timeout(
|| {
std::thread::sleep(Duration::from_millis(500));
Ok::<bool, std::io::Error>(true)
},
Duration::from_millis(250)
)
.expect("failed to create")
.exec(),
Err(SituwaitionError::CheckTimeoutError),
),
"check that finishes in 500ms times out in 100ms as configured"
);
assert!(
Instant::now() - start < Duration::from_millis(500),
"timed out before the check completed"
);
}
}