-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fairness to futures_util::{select, try_select, select_ok}, fixes #…
- Loading branch information
Showing
8 changed files
with
162 additions
and
31 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
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
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
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
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
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,16 @@ | ||
use std::future::ready; | ||
|
||
use futures::future::select; | ||
use futures_executor::block_on; | ||
|
||
#[test] | ||
fn is_fair() { | ||
let mut results = Vec::with_capacity(100); | ||
for _ in 0..100 { | ||
let (i, _) = block_on(select(ready(0), ready(1))).factor_first(); | ||
results.push(i); | ||
} | ||
const THRESHOLD: usize = 30; | ||
assert_eq!(results.iter().filter(|i| **i == 0).take(THRESHOLD).count(), THRESHOLD); | ||
assert_eq!(results.iter().filter(|i| **i == 1).take(THRESHOLD).count(), THRESHOLD) | ||
} |
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 |
---|---|---|
@@ -1,30 +1,67 @@ | ||
use std::fmt::Debug; | ||
use std::time::Duration; | ||
|
||
use futures::executor::block_on; | ||
use futures::future::{err, ok, select_ok}; | ||
use futures::future::{err, ok, select_ok, Future}; | ||
use futures_channel::oneshot; | ||
use std::thread; | ||
|
||
#[test] | ||
fn ignore_err() { | ||
let v = vec![err(1), err(2), ok(3), ok(4)]; | ||
|
||
let (i, v) = block_on(select_ok(v)).ok().unwrap(); | ||
assert_eq!(i, 3); | ||
assert!(i == 3 || i == 4); | ||
|
||
assert_eq!(v.len(), 1); | ||
assert!(v.len() < 4); | ||
|
||
let (i, v) = block_on(select_ok(v)).ok().unwrap(); | ||
assert_eq!(i, 4); | ||
let (j, v) = block_on(select_ok(v)).ok().unwrap(); | ||
assert!(j == 3 || j == 4); | ||
assert_ne!(j, i); | ||
|
||
assert!(v.is_empty()); | ||
assert!(v.len() < 3); | ||
} | ||
|
||
#[test] | ||
fn last_err() { | ||
let v = vec![ok(1), err(2), err(3)]; | ||
let (ok_sender, ok_receiver) = oneshot::channel(); | ||
let (first_err_sender, first_err_receiver) = oneshot::channel(); | ||
let (second_err_sender, second_err_receiver) = oneshot::channel(); | ||
async fn await_unwrap<T, E: Debug>(o: impl Future<Output = Result<T, E>>) -> T { | ||
o.await.unwrap() | ||
} | ||
let v = vec![ | ||
Box::pin(await_unwrap(ok_receiver)), | ||
Box::pin(await_unwrap(first_err_receiver)), | ||
Box::pin(await_unwrap(second_err_receiver)), | ||
]; | ||
ok_sender.send(Ok(1)).unwrap(); | ||
|
||
let (i, v) = block_on(select_ok(v)).ok().unwrap(); | ||
assert_eq!(i, 1); | ||
|
||
assert_eq!(v.len(), 2); | ||
first_err_sender.send(Err(2)).unwrap(); | ||
|
||
thread::spawn(move || { | ||
thread::sleep(Duration::from_millis(100)); | ||
second_err_sender.send(Err(3)).unwrap(); | ||
}); | ||
|
||
let i = block_on(select_ok(v)).err().unwrap(); | ||
assert_eq!(i, 3); | ||
} | ||
|
||
#[test] | ||
fn is_fair() { | ||
let mut results = Vec::with_capacity(100); | ||
for _ in 0..100 { | ||
let v = vec![err(1), err(2), ok(3), ok(4)]; | ||
|
||
let (i, _v) = block_on(select_ok(v)).ok().unwrap(); | ||
results.push(i); | ||
} | ||
const THRESHOLD: usize = 30; | ||
assert_eq!(results.iter().filter(|i| **i == 3).take(THRESHOLD).count(), THRESHOLD); | ||
assert_eq!(results.iter().filter(|i| **i == 4).take(THRESHOLD).count(), THRESHOLD); | ||
} |
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,14 @@ | ||
use futures::future::{ok, try_select}; | ||
use futures_executor::block_on; | ||
|
||
#[test] | ||
fn is_fair() { | ||
let mut results = Vec::with_capacity(100); | ||
for _ in 0..100 { | ||
let (i, _) = block_on(try_select(ok::<_, ()>(0), ok::<_, ()>(1))).unwrap().factor_first(); | ||
results.push(i); | ||
} | ||
const THRESHOLD: usize = 30; | ||
assert_eq!(results.iter().filter(|i| **i == 0).take(THRESHOLD).count(), THRESHOLD); | ||
assert_eq!(results.iter().filter(|i| **i == 1).take(THRESHOLD).count(), THRESHOLD) | ||
} |