|
| 1 | +// Copyright (C) 2025 Stacks Open Internet Foundation |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +use std::time::Duration; |
| 17 | +use std::{panic, thread}; |
| 18 | + |
| 19 | +use proptest::prelude::*; |
| 20 | + |
| 21 | +use crate::burnchains::bitcoin::Error as bitcoin_error; |
| 22 | +use crate::burnchains::{Burnchain, Error as burnchain_error}; |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn join_success() { |
| 26 | + proptest!(|(v in any::<u32>())| { |
| 27 | + let h = thread::spawn(move || Ok(v)); |
| 28 | + let r = Burnchain::handle_thread_join::<u32>(h, "test"); |
| 29 | + prop_assert!(r.is_ok()); |
| 30 | + prop_assert_eq!(r.unwrap(), v); |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +#[test] |
| 35 | +fn join_with_name() { |
| 36 | + proptest!(|(s in "[a-zA-Z0-9_-]{1,20}")| { |
| 37 | + let h = thread::spawn(|| Ok(42)); |
| 38 | + let r = Burnchain::handle_thread_join::<u32>(h, &s); |
| 39 | + prop_assert!(r.is_ok()); |
| 40 | + prop_assert_eq!(r.unwrap(), 42); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn join_download_error() { |
| 46 | + proptest!(|(x in any::<u32>())| { |
| 47 | + let h = thread::spawn(move || { |
| 48 | + let _ = x; |
| 49 | + Err(burnchain_error::DownloadError( |
| 50 | + bitcoin_error::ConnectionError)) |
| 51 | + }); |
| 52 | + let r = Burnchain::handle_thread_join::<u32>(h, "test"); |
| 53 | + prop_assert!(r.is_err()); |
| 54 | + match r { |
| 55 | + Err(burnchain_error::DownloadError(_)) => {} |
| 56 | + _ => return Err(TestCaseError::fail("Expected DownloadError")), |
| 57 | + } |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +#[test] |
| 62 | +fn join_parse_error() { |
| 63 | + let h = thread::spawn(move || Err(burnchain_error::ParseError)); |
| 64 | + let r = Burnchain::handle_thread_join::<u32>(h, "test"); |
| 65 | + assert!(r.is_err()); |
| 66 | + match r { |
| 67 | + Err(burnchain_error::ParseError) => {} |
| 68 | + _ => panic!("Expected ParseError"), |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn join_delay() { |
| 74 | + proptest!(|(d in 10u64..100, v in any::<u32>())| { |
| 75 | + let h = thread::spawn(move || { |
| 76 | + thread::sleep(Duration::from_millis(d)); |
| 77 | + Ok(v) |
| 78 | + }); |
| 79 | + let r = Burnchain::handle_thread_join::<u32>(h, "test"); |
| 80 | + prop_assert!(r.is_ok()); |
| 81 | + prop_assert_eq!(r.unwrap(), v); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +#[test] |
| 86 | +fn join_panics() { |
| 87 | + let h = thread::spawn(|| { |
| 88 | + panic!("boom"); |
| 89 | + #[allow(unreachable_code)] |
| 90 | + Ok(42) |
| 91 | + }); |
| 92 | + let r = Burnchain::handle_thread_join::<u32>(h, "test"); |
| 93 | + assert!(r.is_err()); |
| 94 | + match r { |
| 95 | + Err(burnchain_error::ThreadChannelError) => {} |
| 96 | + _ => panic!("Expected ThreadChannelError"), |
| 97 | + } |
| 98 | +} |
0 commit comments