proptest/arbitrary/_std/
thread.rs

1//-
2// Copyright 2017, 2018 The proptest developers
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Arbitrary implementations for `std::thread`.
11
12use crate::std_facade::String;
13use std::thread::*;
14
15use crate::arbitrary::*;
16use crate::option::prob;
17use crate::strategy::statics::static_map;
18
19arbitrary!(Builder, SMapped<(Option<usize>, Option<String>), Self>; {
20    let prob = prob(0.7);
21    let args = product_pack![
22        product_pack![prob, Default::default()],
23        product_pack![prob, Default::default()]
24    ];
25    static_map(arbitrary_with(args), |(os, on)| {
26        let mut b = Builder::new();
27        b = if let Some(size) = os { b.stack_size(size) } else { b };
28        if let Some(name) = on { b.name(name) } else { b }
29    })
30});
31
32/*
33 * The usefulness of this impl is debatable - as are its semantics.
34 * Perhaps a CoArbitrary-based solution is preferable.
35
36arbitrary!([A: 'static + Send + Arbitrary<'a>] JoinHandle<A>,
37    SMapped<'a, (A, Option<()>, u8), Self>, A::Parameters;
38    args => {
39        let prob  = prob(0.1);
40        let args2 = product_pack![
41            args,
42            product_pack![prob, default()],
43            default()
44        ];
45        any_with_smap(args2, |(val, panic, sleep)| thread::spawn(move || {
46            // Sleep a random amount:
47            use std::time::Duration;
48            thread::sleep(Duration::from_millis(sleep as u64));
49
50            // Randomly panic:
51            if panic.is_some() {
52                panic!("Arbitrary for JoinHandle randomly paniced!");
53            }
54
55            // Move value into thread and then just return it:
56            val
57        }))
58    }
59);
60*/
61
62#[cfg(test)]
63mod test {
64    no_panic_test!(
65        builder => Builder
66    );
67
68    /*
69    use super::*;
70    proptest! {
71        #[test]
72        fn join_handle_works(ref jh in any::<JoinHandle<u8>>()) {
73            use std::panic::catch_unwind;
74            catch_unwind(|| {
75                jh.join();
76                ()
77            })
78        }
79    }
80    */
81}