Skip to content

Commit d1acf08

Browse files
LizardWizzardtaiki-e
authored andcommitted
tests: restore disabled tests and benches for BiLock (#2715)
1 parent 0ffaaa0 commit d1acf08

File tree

3 files changed

+103
-155
lines changed

3 files changed

+103
-155
lines changed

Diff for: futures-util/benches/bilock.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#![feature(test)]
2+
#![cfg(feature = "bilock")]
3+
4+
extern crate test;
5+
6+
use futures::task::Poll;
7+
use futures_test::task::noop_context;
8+
use futures_util::lock::BiLock;
9+
10+
use crate::test::Bencher;
11+
12+
#[bench]
13+
fn contended(b: &mut Bencher) {
14+
let mut context = noop_context();
15+
16+
b.iter(|| {
17+
let (x, y) = BiLock::new(1);
18+
19+
for _ in 0..1000 {
20+
let x_guard = match x.poll_lock(&mut context) {
21+
Poll::Ready(guard) => guard,
22+
_ => panic!(),
23+
};
24+
25+
// Try poll second lock while first lock still holds the lock
26+
match y.poll_lock(&mut context) {
27+
Poll::Pending => (),
28+
_ => panic!(),
29+
};
30+
31+
drop(x_guard);
32+
33+
let y_guard = match y.poll_lock(&mut context) {
34+
Poll::Ready(guard) => guard,
35+
_ => panic!(),
36+
};
37+
38+
drop(y_guard);
39+
}
40+
(x, y)
41+
});
42+
}
43+
44+
#[bench]
45+
fn lock_unlock(b: &mut Bencher) {
46+
let mut context = noop_context();
47+
48+
b.iter(|| {
49+
let (x, y) = BiLock::new(1);
50+
51+
for _ in 0..1000 {
52+
let x_guard = match x.poll_lock(&mut context) {
53+
Poll::Ready(guard) => guard,
54+
_ => panic!(),
55+
};
56+
57+
drop(x_guard);
58+
59+
let y_guard = match y.poll_lock(&mut context) {
60+
Poll::Ready(guard) => guard,
61+
_ => panic!(),
62+
};
63+
64+
drop(y_guard);
65+
}
66+
(x, y)
67+
})
68+
}

Diff for: futures-util/benches_disabled/bilock.rs

-122
This file was deleted.

Diff for: futures/tests_disabled/bilock.rs renamed to futures/tests/bilock.rs

+35-33
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
1+
#![cfg(feature = "bilock")]
2+
3+
use futures::executor::block_on;
14
use futures::future;
25
use futures::stream;
3-
use futures::task;
6+
use futures::task::{Context, Poll};
7+
use futures::Future;
8+
use futures::StreamExt;
9+
use futures_test::task::noop_context;
410
use futures_util::lock::BiLock;
11+
use std::pin::Pin;
512
use std::thread;
613

7-
// mod support;
8-
// use support::*;
9-
1014
#[test]
1115
fn smoke() {
12-
let future = future::lazy(|_| {
16+
let future = future::lazy(|cx| {
1317
let (a, b) = BiLock::new(1);
1418

1519
{
16-
let mut lock = match a.poll_lock() {
20+
let mut lock = match a.poll_lock(cx) {
1721
Poll::Ready(l) => l,
1822
Poll::Pending => panic!("poll not ready"),
1923
};
2024
assert_eq!(*lock, 1);
2125
*lock = 2;
2226

23-
assert!(b.poll_lock().is_pending());
24-
assert!(a.poll_lock().is_pending());
27+
assert!(b.poll_lock(cx).is_pending());
28+
assert!(a.poll_lock(cx).is_pending());
2529
}
2630

27-
assert!(b.poll_lock().is_ready());
28-
assert!(a.poll_lock().is_ready());
31+
assert!(b.poll_lock(cx).is_ready());
32+
assert!(a.poll_lock(cx).is_ready());
2933

3034
{
31-
let lock = match b.poll_lock() {
35+
let lock = match b.poll_lock(cx) {
3236
Poll::Ready(l) => l,
3337
Poll::Pending => panic!("poll not ready"),
3438
};
@@ -40,34 +44,32 @@ fn smoke() {
4044
Ok::<(), ()>(())
4145
});
4246

43-
assert!(task::spawn(future)
44-
.poll_future_notify(&notify_noop(), 0)
45-
.expect("failure in poll")
46-
.is_ready());
47+
assert_eq!(block_on(future), Ok(()));
4748
}
4849

4950
#[test]
5051
fn concurrent() {
5152
const N: usize = 10000;
53+
let mut cx = noop_context();
5254
let (a, b) = BiLock::new(0);
5355

5456
let a = Increment { a: Some(a), remaining: N };
55-
let b = stream::iter_ok(0..N).fold(b, |b, _n| {
56-
b.lock().map(|mut b| {
57-
*b += 1;
58-
b.unlock()
59-
})
57+
let b = stream::iter(0..N).fold(b, |b, _n| async {
58+
let mut g = b.lock().await;
59+
*g += 1;
60+
drop(g);
61+
b
6062
});
6163

62-
let t1 = thread::spawn(move || a.wait());
63-
let b = b.wait().expect("b error");
64-
let a = t1.join().unwrap().expect("a error");
64+
let t1 = thread::spawn(move || block_on(a));
65+
let b = block_on(b);
66+
let a = t1.join().unwrap();
6567

66-
match a.poll_lock() {
68+
match a.poll_lock(&mut cx) {
6769
Poll::Ready(l) => assert_eq!(*l, 2 * N),
6870
Poll::Pending => panic!("poll not ready"),
6971
}
70-
match b.poll_lock() {
72+
match b.poll_lock(&mut cx) {
7173
Poll::Ready(l) => assert_eq!(*l, 2 * N),
7274
Poll::Pending => panic!("poll not ready"),
7375
}
@@ -80,22 +82,22 @@ fn concurrent() {
8082
}
8183

8284
impl Future for Increment {
83-
type Item = BiLock<usize>;
84-
type Error = ();
85+
type Output = BiLock<usize>;
8586

86-
fn poll(&mut self) -> Poll<BiLock<usize>, ()> {
87+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<BiLock<usize>> {
8788
loop {
8889
if self.remaining == 0 {
89-
return Ok(self.a.take().unwrap().into());
90+
return self.a.take().unwrap().into();
9091
}
9192

92-
let a = self.a.as_ref().unwrap();
93-
let mut a = match a.poll_lock() {
93+
let a = self.a.as_mut().unwrap();
94+
let mut a = match a.poll_lock(cx) {
9495
Poll::Ready(l) => l,
95-
Poll::Pending => return Ok(Poll::Pending),
96+
Poll::Pending => return Poll::Pending,
9697
};
97-
self.remaining -= 1;
9898
*a += 1;
99+
drop(a);
100+
self.remaining -= 1;
99101
}
100102
}
101103
}

0 commit comments

Comments
 (0)