Skip to content

Commit 3f6b4c2

Browse files
committed
Add a better-for-testing optimistic_check() for pipes with cfg(test).
1 parent cccfa8a commit 3f6b4c2

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/libstd/rt/comm.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,25 @@ impl<T> PortOne<T> {
180180
}
181181

182182
impl<T> Select for PortOne<T> {
183-
#[inline]
183+
#[inline] #[cfg(not(test))]
184184
fn optimistic_check(&mut self) -> bool {
185185
unsafe { (*self.packet()).state.load(Acquire) == STATE_ONE }
186186
}
187187

188+
#[inline] #[cfg(test)]
189+
fn optimistic_check(&mut self) -> bool {
190+
// The optimistic check is never necessary for correctness. For testing
191+
// purposes, making it randomly return false simulates a racing sender.
192+
use rand::{Rand, rng};
193+
let mut rng = rng();
194+
let actually_check = Rand::rand(&mut rng);
195+
if actually_check {
196+
unsafe { (*self.packet()).state.load(Acquire) == STATE_ONE }
197+
} else {
198+
false
199+
}
200+
}
201+
188202
fn block_on(&mut self, sched: &mut Scheduler, task: BlockedTask) -> bool {
189203
unsafe {
190204
// Atomically swap the task pointer into the Packet state, issuing

0 commit comments

Comments
 (0)