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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! Run assertions multiple times
//!
//! `repeated_assert` runs assertions until they either pass
//! or the maximum amount of repetitions has been reached.
//! The current thread will be blocked between tries.
//!
//! This is useful when waiting for events from another thread (or process).
//! Waiting for a short time might result in a failing test, while waiting too long is a waste of time.
//!
//! # Examples
//!
//! Waiting for a file to appear (re-try up to 10 times, wait 50 ms between tries)
//!
//! ```rust,ignore
//! repeated_assert::that(10, Duration::from_millis(50), || {
//!     assert!(Path::new("should_appear_soon.txt").exists());
//! });
//! ```
//!
//! Waiting for variable `x` to equal `3`
//!
//! ```rust,ignore
//! repeated_assert::that(10, Duration::from_millis(50), || {
//!     assert_eq!(x, 3);
//! });
//! ```
//!
//! Temporary variables
//!
//! ```rust,ignore
//! repeated_assert::that(10, Duration::from_millis(50), || {
//!     let checksum = crc("should_appear_soon.txt");
//!     assert_eq!(checksum, 1234);
//! });
//! ```
//!
//! Return result
//!
//! ```rust,ignore
//! repeated_assert::that(10, Duration::from_millis(50), || -> Result<_, Box<dyn std::error::Error>> {
//!     let checksum = crc("should_appear_soon.txt")?;
//!     assert_eq!(checksum, 1234);
//! })?;
//! ```
//!
//! # Catch failing tests
//!
//! It's also possible to "catch" failing tests by executing some code if the expressions couldn't be asserted in order to trigger an alternate strategy.
//! This can be useful if the tested program relies on an unreliable service.
//! This counters the idea of a test to some degree, so use it only if the unreliable service is not critical for your program.
//!
//! Poke unreliable service after 5 unsuccessful assertion attempts
//!
//! ```rust,ignore
//! repeated_assert::with_catch(10, Duration::from_millis(50), 5,
//!     || {
//!         // poke unreliable service
//!     },
//!     || {
//!         assert!(Path::new("should_appear_soon.txt").exists());
//!     }
//! );
//! ```

use lazy_static::lazy_static;

use std::{collections::HashSet, panic, sync::Mutex, thread, time::Duration};

mod macros;

lazy_static! {
    static ref IGNORE_THREADS: Mutex<HashSet<String>> = {
        // get original panic hook
        let panic_hook = panic::take_hook();
        // set custom panic hook
        panic::set_hook(Box::new(move |panic_info| {
            let ignore_threads = IGNORE_THREADS.lock().expect("lock ignore threads");
            if let Some(thread_name) = thread::current().name() {
                if !ignore_threads.contains(thread_name) {
                    // call original panic hook
                    panic_hook(panic_info);
                }
            } else {
                // call original panic hook
                panic_hook(panic_info);
            }
        }));
        Mutex::new(HashSet::new())
    };
}

struct IgnoreGuard;

impl IgnoreGuard {
    fn new() -> IgnoreGuard {
        if let Some(thread_name) = thread::current().name() {
            IGNORE_THREADS
                .lock()
                .expect("lock ignore threads")
                .insert(thread_name.to_string());
        }
        IgnoreGuard
    }
}

impl Drop for IgnoreGuard {
    fn drop(&mut self) {
        if let Some(thread_name) = thread::current().name() {
            IGNORE_THREADS
                .lock()
                .expect("lock ignore threads")
                .remove(thread_name);
        }
    }
}

/// Run the provided function `assert` up to `repetitions` times with a `delay` in between tries.
///
/// Panics (including failed assertions) will be caught and ignored until the last try is executed.
///
/// # Examples
///
/// Waiting for a file to appear (re-try up to 10 times, wait 50 ms between tries)
///
/// ```rust,ignore
/// repeated_assert::that(10, Duration::from_millis(50), || {
///     assert!(Path::new("should_appear_soon.txt").exists());
/// });
/// ```
///
/// # Info
///
/// Behind the scene `std::panic::set_hook` is used to set a custom panic handler.
/// For every iteration but the last, panics are ignored and re-tried after a delay.
/// Only when the last iteration is reached, panics are handled by the panic handler that was registered prior to calling `repeated_assert`.
///
/// The panic handler can only be registerd for the entire process, and it is done on demand the first time `repeated_assert` is used.
/// `repeated_assert` works with multiple threads. Each thread is identified by its name, which is automatically set for tests.
pub fn that<A, R>(repetitions: usize, delay: Duration, assert: A) -> R
where
    A: Fn() -> R,
{
    // add current thread to ignore list
    let ignore_guard = IgnoreGuard::new();

    for _ in 0..(repetitions - 1) {
        // run assertions, catching panics
        let result = panic::catch_unwind(panic::AssertUnwindSafe(|| assert()));
        // return if assertions succeeded
        if let Ok(value) = result {
            return value;
        }
        // or sleep until the next try
        thread::sleep(delay);
    }

    // remove current thread from ignore list
    drop(ignore_guard);

    // run assertions without catching panics
    assert()
}

/// Run the provided function `assert` up to `repetitions` times with a `delay` in between tries.
/// Execute the provided function `catch` after `repetitions_catch` failed tries in order to trigger an alternate strategy.
///
/// Panics (including failed assertions) will be caught and ignored until the last try is executed.
///
/// # Examples
///
/// ```rust,ignore
/// repeated_assert::with_catch(10, Duration::from_millis(50), 5,
///     || {
///         // poke unreliable service
///     },
///     || {
///         assert!(Path::new("should_appear_soon.txt").exists());
///     }
/// );
/// ```
///
/// # Info
///
/// See [`that`].
pub fn with_catch<A, C, R>(
    repetitions: usize,
    delay: Duration,
    repetitions_catch: usize,
    catch: C,
    assert: A,
) -> R
where
    A: Fn() -> R,
    C: FnOnce() -> (),
{
    let ignore_guard = IgnoreGuard::new();

    for _ in 0..repetitions_catch {
        // run assertions, catching panics
        let result = panic::catch_unwind(panic::AssertUnwindSafe(|| assert()));
        // return if assertions succeeded
        if let Ok(value) = result {
            return value;
        }
        // or sleep until the next try
        thread::sleep(delay);
    }

    let thread_name = thread::current()
        .name()
        .unwrap_or("<unnamed thread>")
        .to_string();
    println!("{}: executing repeated-assert catch block", thread_name);
    catch();

    for _ in repetitions_catch..(repetitions - 1) {
        // run assertions, catching panics
        let result = panic::catch_unwind(panic::AssertUnwindSafe(|| assert()));
        // return if assertions succeeded
        if let Ok(value) = result {
            return value;
        }
        // or sleep until the next try
        thread::sleep(delay);
    }

    // remove current thread from ignore list
    drop(ignore_guard);

    // run assertions without catching panics
    assert()
}

#[cfg(test)]
mod tests {
    use crate as repeated_assert;
    use std::sync::{Arc, Mutex};
    use std::thread;
    use std::time::Duration;

    static STEP_MS: u64 = 100;

    fn spawn_thread(x: Arc<Mutex<i32>>) {
        thread::spawn(move || loop {
            thread::sleep(Duration::from_millis(10 * STEP_MS));
            if let Ok(mut x) = x.lock() {
                *x += 1;
            }
        });
    }

    // #[test]
    // fn slow() {
    //     let x = Arc::new(Mutex::new(0));

    //     spawn_thread(x.clone());

    //     repeated_assert::that(10, Duration::from_millis(10 * STEP_MS), || {
    //         assert!(*x.lock().unwrap() > 5);
    //     });
    // }

    // #[test]
    // fn panic() {
    //     let x = Arc::new(Mutex::new(0));

    //     spawn_thread(x.clone());

    //     repeated_assert::that(3, Duration::from_millis(10 * STEP_MS), || {
    //         assert!(*x.lock().unwrap() > 5);
    //     });
    // }

    #[test]
    fn single_success() {
        let x = Arc::new(Mutex::new(0));

        spawn_thread(x.clone());

        repeated_assert::that(5, Duration::from_millis(5 * STEP_MS), || {
            assert!(*x.lock().unwrap() > 0);
        });
    }

    #[test]
    #[should_panic(expected = "assertion failed: *x.lock().unwrap() > 0")]
    fn single_failure() {
        let x = Arc::new(Mutex::new(0));

        spawn_thread(x.clone());

        repeated_assert::that(3, Duration::from_millis(1 * STEP_MS), || {
            assert!(*x.lock().unwrap() > 0);
        });
    }

    #[test]
    fn multiple_success() {
        let x = Arc::new(Mutex::new(0));
        let a = 11;
        let b = 11;

        spawn_thread(x.clone());

        repeated_assert::that(5, Duration::from_millis(5 * STEP_MS), || {
            assert!(*x.lock().unwrap() > 0);
            assert_eq!(a, b);
        });
    }

    #[test]
    #[should_panic(expected = "assertion failed: *x.lock().unwrap() > 0")]
    fn multiple_failure_1() {
        let x = Arc::new(Mutex::new(0));
        let a = 11;
        let b = 11;

        spawn_thread(x.clone());

        repeated_assert::that(3, Duration::from_millis(1 * STEP_MS), || {
            assert!(*x.lock().unwrap() > 0);
            assert_eq!(a, b);
        });
    }

    #[test]
    #[should_panic(expected = "assertion failed: `(left == right)")]
    fn multiple_failure_2() {
        let x = Arc::new(Mutex::new(0));
        let a = 11;
        let b = 12;

        spawn_thread(x.clone());

        repeated_assert::that(5, Duration::from_millis(5 * STEP_MS), || {
            assert!(*x.lock().unwrap() > 0);
            assert_eq!(a, b);
        });
    }

    #[test]
    fn catch() {
        let x = Arc::new(Mutex::new(-1_000));

        spawn_thread(x.clone());

        repeated_assert::with_catch(
            10,
            Duration::from_millis(5 * STEP_MS),
            5,
            || {
                *x.lock().unwrap() = 0;
            },
            || {
                assert!(*x.lock().unwrap() > 0);
            },
        );
    }
}