-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: restore disabled tests and benches for BiLock
- Loading branch information
1 parent
d757d87
commit 0007315
Showing
4 changed files
with
175 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
|
||
#[cfg(feature = "bilock")] | ||
mod bench { | ||
use futures::task::Poll; | ||
use futures_test::task::noop_context; | ||
use futures_util::lock::BiLock; | ||
|
||
use crate::test::Bencher; | ||
|
||
#[bench] | ||
fn contended(b: &mut Bencher) { | ||
let mut context = noop_context(); | ||
|
||
b.iter(|| { | ||
let (x, y) = BiLock::new(1); | ||
|
||
for _ in 0..1000 { | ||
let x_guard = match x.poll_lock(&mut context) { | ||
Poll::Ready(guard) => guard, | ||
_ => panic!(), | ||
}; | ||
|
||
// Try poll second lock while first lock still holds the lock | ||
match y.poll_lock(&mut context) { | ||
Poll::Pending => (), | ||
_ => panic!(), | ||
}; | ||
|
||
drop(x_guard); | ||
|
||
let y_guard = match y.poll_lock(&mut context) { | ||
Poll::Ready(guard) => guard, | ||
_ => panic!(), | ||
}; | ||
|
||
drop(y_guard); | ||
} | ||
(x, y) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn lock_unlock(b: &mut Bencher) { | ||
let mut context = noop_context(); | ||
|
||
b.iter(|| { | ||
let (x, y) = BiLock::new(1); | ||
|
||
for _ in 0..1000 { | ||
let x_guard = match x.poll_lock(&mut context) { | ||
Poll::Ready(guard) => guard, | ||
_ => panic!(), | ||
}; | ||
|
||
drop(x_guard); | ||
|
||
let y_guard = match y.poll_lock(&mut context) { | ||
Poll::Ready(guard) => guard, | ||
_ => panic!(), | ||
}; | ||
|
||
drop(y_guard); | ||
} | ||
(x, y) | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#[cfg(feature = "bilock")] | ||
mod tests { | ||
use futures::executor::block_on; | ||
use futures::future; | ||
use futures::stream; | ||
use futures::task::{Context, Poll}; | ||
use futures::Future; | ||
use futures::StreamExt; | ||
use futures_test::task::noop_context; | ||
use futures_util::lock::BiLock; | ||
use std::pin::Pin; | ||
use std::thread; | ||
|
||
#[test] | ||
fn smoke() { | ||
let future = future::lazy(|cx| { | ||
let (a, b) = BiLock::new(1); | ||
|
||
{ | ||
let mut lock = match a.poll_lock(cx) { | ||
Poll::Ready(l) => l, | ||
Poll::Pending => panic!("poll not ready"), | ||
}; | ||
assert_eq!(*lock, 1); | ||
*lock = 2; | ||
|
||
assert!(b.poll_lock(cx).is_pending()); | ||
assert!(a.poll_lock(cx).is_pending()); | ||
} | ||
|
||
assert!(b.poll_lock(cx).is_ready()); | ||
assert!(a.poll_lock(cx).is_ready()); | ||
|
||
{ | ||
let lock = match b.poll_lock(cx) { | ||
Poll::Ready(l) => l, | ||
Poll::Pending => panic!("poll not ready"), | ||
}; | ||
assert_eq!(*lock, 2); | ||
} | ||
|
||
assert_eq!(a.reunite(b).expect("bilock/smoke: reunite error"), 2); | ||
|
||
Ok::<(), ()>(()) | ||
}); | ||
|
||
assert_eq!(block_on(future), Ok(())); | ||
} | ||
|
||
#[test] | ||
fn concurrent() { | ||
const N: usize = 10000; | ||
let mut cx = noop_context(); | ||
let (a, b) = BiLock::new(0); | ||
|
||
let a = Increment { a: Some(a), remaining: N }; | ||
let b = stream::iter(0..N).fold(b, |b, _n| async { | ||
let mut g = b.lock().await; | ||
*g += 1; | ||
drop(g); | ||
b | ||
}); | ||
|
||
let t1 = thread::spawn(move || block_on(a)); | ||
let b = block_on(b); | ||
let a = t1.join().unwrap(); | ||
|
||
match a.poll_lock(&mut cx) { | ||
Poll::Ready(l) => assert_eq!(*l, 2 * N), | ||
Poll::Pending => panic!("poll not ready"), | ||
} | ||
match b.poll_lock(&mut cx) { | ||
Poll::Ready(l) => assert_eq!(*l, 2 * N), | ||
Poll::Pending => panic!("poll not ready"), | ||
} | ||
|
||
assert_eq!(a.reunite(b).expect("bilock/concurrent: reunite error"), 2 * N); | ||
|
||
struct Increment { | ||
remaining: usize, | ||
a: Option<BiLock<usize>>, | ||
} | ||
|
||
impl Future for Increment { | ||
type Output = BiLock<usize>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<BiLock<usize>> { | ||
loop { | ||
if self.remaining == 0 { | ||
return self.a.take().unwrap().into(); | ||
} | ||
|
||
let a = self.a.as_mut().unwrap(); | ||
let mut a = match a.poll_lock(cx) { | ||
Poll::Ready(l) => l, | ||
Poll::Pending => return Poll::Pending, | ||
}; | ||
*a += 1; | ||
drop(a); | ||
self.remaining -= 1; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.