From 2cb2853a21fdcd401c34ba7b3cd76bbdb03c8806 Mon Sep 17 00:00:00 2001 From: Kaz Wesley Date: Sat, 10 Nov 2018 10:54:55 -0800 Subject: [PATCH] test case for out-of-order drop Demonstrates issue #6 --- src/lib.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index a7d7d17..c408e2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -229,6 +229,33 @@ mod tests { assert_eq!(the_ultimate_question(), 42); } + #[test] + fn test_out_of_order_drop() { + assert_eq!(the_ultimate_question(), 42); + + let guard_a = patch0(the_ultimate_question, || 24); + let guard_b = patch0(the_ultimate_question, || 23); + + core::mem::drop(guard_a); + assert_eq!(the_ultimate_question(), 42); + + core::mem::drop(guard_b); + // Uh oh. + assert_eq!(the_ultimate_question(), 24); + + if let Err(e) = std::panic::catch_unwind(|| { + assert_eq!( + 42, + the_ultimate_question(), + "Guards dropped without restoring original value!" + ); + }) { + // Fix it for other tests before we re-raise + core::mem::forget(patch0(the_ultimate_question, || 42)); + std::panic::resume_unwind(e); + } + } + #[test] fn test_functions_independent() { assert_eq!(the_ultimate_question(), 42);