|
| 1 | +use std::time::Duration; |
| 2 | +use libtest_mimic::{Trial, Arguments}; |
| 3 | + |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn check_test_on_main_thread() { |
| 7 | + let outer_thread = std::thread::current().id(); |
| 8 | + |
| 9 | + let mut args = Arguments::default(); |
| 10 | + args.test_threads = Some(1); |
| 11 | + let conclusion = libtest_mimic::run(&args, vec![Trial::test("check", move || { |
| 12 | + assert_eq!(outer_thread, std::thread::current().id()); |
| 13 | + Ok(()) |
| 14 | + })]); |
| 15 | + |
| 16 | + assert_eq!(conclusion.num_passed, 1); |
| 17 | +} |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn all_tests_run_on_single_thread() { |
| 21 | + let mut args = Arguments::default(); |
| 22 | + args.test_threads = Some(1); |
| 23 | + let trials = vec![ |
| 24 | + Trial::test("a", move || Ok(())), |
| 25 | + Trial::test("b", move || Ok(())), |
| 26 | + Trial::test("c", move || Ok(())), |
| 27 | + ]; |
| 28 | + let conclusion = libtest_mimic::run(&args, trials); |
| 29 | + assert_eq!(conclusion.num_passed, 3); |
| 30 | +} |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn all_tests_run_on_two_threads() { |
| 34 | + let mut args = Arguments::default(); |
| 35 | + args.test_threads = Some(2); |
| 36 | + let trials = vec![ |
| 37 | + Trial::test("a", move || Ok(())), |
| 38 | + Trial::test("b", move || Ok(())), |
| 39 | + Trial::test("c", move || Ok(())), |
| 40 | + Trial::test("d", move || Ok(())), |
| 41 | + Trial::test("e", move || Ok(())), |
| 42 | + ]; |
| 43 | + let conclusion = libtest_mimic::run(&args, trials); |
| 44 | + assert_eq!(conclusion.num_passed, 5); |
| 45 | +} |
| 46 | + |
| 47 | +// I know this is not a super clean test, but I think spurious failures should |
| 48 | +// be veeeery limited. The value this test provides outweights the potential |
| 49 | +// jankiness I think. |
| 50 | +#[test] |
| 51 | +fn multi_threads_are_used() { |
| 52 | + let mut args = Arguments::default(); |
| 53 | + args.test_threads = Some(4); |
| 54 | + let trials = vec![ |
| 55 | + Trial::test("a", move || { std::thread::sleep(Duration::from_secs(1)); Ok(()) }), |
| 56 | + Trial::test("b", move || { std::thread::sleep(Duration::from_secs(1)); Ok(()) }), |
| 57 | + Trial::test("c", move || { std::thread::sleep(Duration::from_secs(1)); Ok(()) }), |
| 58 | + Trial::test("d", move || { std::thread::sleep(Duration::from_secs(1)); Ok(()) }), |
| 59 | + ]; |
| 60 | + let before = std::time::Instant::now(); |
| 61 | + let _ = libtest_mimic::run(&args, trials); |
| 62 | + if before.elapsed() >= Duration::from_secs(2) { |
| 63 | + panic!("Seems like tests are not executed in parallel"); |
| 64 | + } |
| 65 | +} |
0 commit comments