]>
git.proxmox.com Git - cargo.git/blob - vendor/proptest/src/arbitrary/_std/sync.rs
2 // Copyright 2017, 2018 The proptest developers
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.
10 //! Arbitrary implementations for `std::sync`.
13 use std
::sync
::mpsc
::*;
16 use std
::time
::Duration
;
18 use crate::arbitrary
::*;
19 use crate::strategy
::statics
::static_map
;
20 use crate::strategy
::*;
22 // OnceState can not escape Once::call_once_force.
23 // PoisonError depends implicitly on the lifetime on MutexGuard, etc.
24 // This transitively applies to TryLockError.
26 // Not doing Weak because .upgrade() would always return None.
28 #[cfg(not(feature = "unstable"))]
30 #[cfg(feature = "unstable")]
33 #[cfg(not(feature = "unstable"))]
35 #[cfg(feature = "unstable")]
38 arbitrary
!(Barrier
, SMapped
<u16, Self>; // usize would be extreme!
39 static_map(any
::<u16>(), |n
| Barrier
::new(n
as usize))
42 arbitrary
!(BarrierWaitResult
,
43 LazyTupleUnion
<(WA
<LazyJustFn
<Self>>, WA
<LazyJustFn
<Self>>)>;
44 prop_oneof
![LazyJust
::new(bwr_true
), LazyJust
::new(bwr_false
)]
48 Condvar
, Default
::default;
52 arbitrary
!(WaitTimeoutResult
, LazyTupleUnion
<(WA
<Just
<Self>>, WA
<Just
<Self>>)>;
53 prop_oneof
![Just(wtr_true()), Just(wtr_false())]
56 fn bwr_true() -> BarrierWaitResult
{
57 Barrier
::new(1).wait()
60 fn bwr_false() -> BarrierWaitResult
{
61 let barrier
= Arc
::new(Barrier
::new(2));
62 let b2
= barrier
.clone();
63 let jh
= thread
::spawn(move || b2
.wait());
64 let bwr1
= barrier
.wait();
65 let bwr2
= jh
.join().unwrap();
73 fn wtr_false() -> WaitTimeoutResult
{
74 let cvar
= Arc
::new(Condvar
::new());
75 let cvar2
= cvar
.clone();
76 thread
::spawn(move || {
79 let lock
= Mutex
::new(());
80 let wt
= cvar
.wait_timeout(lock
.lock().unwrap(), Duration
::from_millis(1));
81 let (_
, wtr
) = wt
.unwrap();
85 fn wtr_true() -> WaitTimeoutResult
{
86 let cvar
= Condvar
::new();
87 let lock
= Mutex
::new(());
88 let wt
= cvar
.wait_timeout(lock
.lock().unwrap(), Duration
::from_millis(0));
89 let (_
, wtr
) = wt
.unwrap();
93 arbitrary
!(RecvError
; RecvError
);
95 arbitrary
!([T
: Arbitrary
] SendError
<T
>, SMapped
<T
, Self>, T
::Parameters
;
96 args
=> static_map(any_with
::<T
>(args
), SendError
)
99 arbitrary
!(RecvTimeoutError
, LazyTupleUnion
<(WA
<Just
<Self>>, WA
<Just
<Self>>)>;
101 Just(RecvTimeoutError
::Disconnected
),
102 Just(RecvTimeoutError
::Timeout
)
106 arbitrary
!(TryRecvError
, LazyTupleUnion
<(WA
<Just
<Self>>, WA
<Just
<Self>>)>;
108 Just(TryRecvError
::Disconnected
),
109 Just(TryRecvError
::Empty
)
114 [P
: Clone
+ Default
, T
: Arbitrary
<Parameters
= P
>] TrySendError
<T
>,
115 LazyTupleUnion
<(WA
<SMapped
<T
, Self>>, WA
<SMapped
<T
, Self>>)>, P
;
117 static_map(any_with
::<T
>(args
.clone()), TrySendError
::Disconnected
),
118 static_map(any_with
::<T
>(args
), TrySendError
::Full
),
122 // If only half of a pair is generated then you will get a hang-up.
123 // Thus the only meaningful impls are in pairs.
124 arbitrary
!([A
] (Sender
<A
>, Receiver
<A
>), LazyJustFn
<Self>;
125 LazyJust
::new(channel
)
128 arbitrary
!([A
: fmt
::Debug
] (Sender
<A
>, IntoIter
<A
>), LazyJustFn
<Self>;
130 let (rx
, tx
) = channel();
135 arbitrary
!([A
] (SyncSender
<A
>, Receiver
<A
>), SMapped
<u16, Self>;
136 static_map(any
::<u16>(), |size
| sync_channel(size
as usize))
139 arbitrary
!([A
: fmt
::Debug
] (SyncSender
<A
>, IntoIter
<A
>), SMapped
<u16, Self>;
140 static_map(any
::<u16>(), |size
| {
141 let (rx
, tx
) = sync_channel(size
as usize);
150 rw_lock
=> RwLock
<u8>,
152 barrier_wait_result
=> BarrierWaitResult
,
155 wait_timeout_result
=> WaitTimeoutResult
,
156 recv_error
=> RecvError
,
157 send_error
=> SendError
<u8>,
158 recv_timeout_error
=> RecvTimeoutError
,
159 try_recv_error
=> TryRecvError
,
160 try_send_error
=> TrySendError
<u8>,
161 rx_tx
=> (Sender
<u8>, Receiver
<u8>),
162 rx_txiter
=> (Sender
<u8>, IntoIter
<u8>),
163 syncrx_tx
=> (SyncSender
<u8>, Receiver
<u8>),
164 syncrx_txiter
=> (SyncSender
<u8>, IntoIter
<u8>)