proptest_arbitrary/_std/
thread.rs

1//! Arbitrary implementations for `std::thread`.
2
3use super::*;
4use std::thread::*;
5
6arbitrary!(Builder, SMapped<'a, (Option<usize>, Option<String>), Self>; {
7    let prob = prob(0.7);
8    let args = product_pack![
9        product_pack![prob, default()],
10        product_pack![prob, default()]
11    ];
12    any_with_smap(args, |(os, on)| {
13        let mut b = Builder::new();
14        b = if let Some(size) = os { b.stack_size(size) } else { b };
15        if let Some(name) = on { b.name(name) } else { b }
16    })
17});
18
19/*
20 * The usefulness of this impl is debatable - as are its semantics.
21 * Perhaps a CoArbitrary-based solution is preferable.
22
23arbitrary!([A: 'static + Send + Arbitrary<'a>] JoinHandle<A>,
24    SMapped<'a, (A, Option<()>, u8), Self>, A::Parameters;
25    args => {
26        let prob  = prob(0.1);
27        let args2 = product_pack![
28            args, 
29            product_pack![prob, default()],
30            default()
31        ];
32        any_with_smap(args2, |(val, panic, sleep)| thread::spawn(move || {
33            // Sleep a random amount:
34            use std::time::Duration;
35            thread::sleep(Duration::from_millis(sleep as u64));
36
37            // Randomly panic:
38            if panic.is_some() {
39                panic!("Arbitrary for JoinHandle randomly paniced!");
40            }
41
42            // Move value into thread and then just return it:
43            val
44        }))
45    }
46);
47*/
48
49#[cfg(feature = "unstable")]
50arbitrary!(LocalKeyState,
51    TupleUnion<(W<Just<Self>>, W<Just<Self>>, W<Just<Self>>)>;
52    prop_oneof![
53        Just(LocalKeyState::Uninitialized),
54        Just(LocalKeyState::Valid),
55        Just(LocalKeyState::Destroyed)
56    ]
57);
58
59#[cfg(test)]
60mod test {
61    no_panic_test!(
62        builder => Builder
63    );
64
65    #[cfg(feature = "unstable")]
66    no_panic_test!(
67        local_key_state => LocalKeyState
68    );
69
70    /*
71    use super::*;
72    proptest! {
73        #[test]
74        fn join_handle_works(ref jh in any::<JoinHandle<u8>>()) {
75            use std::panic::catch_unwind;
76            catch_unwind(|| {
77                jh.join();
78                ()
79            })
80        }
81    }
82    */
83}