|
|
@ -6,13 +6,22 @@ use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
|
|
use async_std::sync::channel;
|
|
|
|
use async_std::sync::channel;
|
|
|
|
use async_std::task;
|
|
|
|
use async_std::task;
|
|
|
|
use rand::{thread_rng, Rng};
|
|
|
|
use rand::{Rng, SeedableRng};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "unknown"))]
|
|
|
|
|
|
|
|
use async_std::task::spawn;
|
|
|
|
|
|
|
|
#[cfg(target_os = "unknown")]
|
|
|
|
|
|
|
|
use async_std::task::spawn_local as spawn;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
|
|
|
|
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
|
|
|
|
|
|
|
|
|
|
|
|
fn ms(ms: u64) -> Duration {
|
|
|
|
fn ms(ms: u64) -> Duration {
|
|
|
|
Duration::from_millis(ms)
|
|
|
|
Duration::from_millis(ms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn smoke() {
|
|
|
|
fn smoke() {
|
|
|
|
task::block_on(async {
|
|
|
|
task::block_on(async {
|
|
|
|
let (s, r) = channel(1);
|
|
|
|
let (s, r) = channel(1);
|
|
|
@ -35,6 +44,7 @@ fn smoke() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn capacity() {
|
|
|
|
fn capacity() {
|
|
|
|
for i in 1..10 {
|
|
|
|
for i in 1..10 {
|
|
|
|
let (s, r) = channel::<()>(i);
|
|
|
|
let (s, r) = channel::<()>(i);
|
|
|
@ -44,6 +54,7 @@ fn capacity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn len_empty_full() {
|
|
|
|
fn len_empty_full() {
|
|
|
|
#![allow(clippy::cognitive_complexity)]
|
|
|
|
#![allow(clippy::cognitive_complexity)]
|
|
|
|
task::block_on(async {
|
|
|
|
task::block_on(async {
|
|
|
@ -86,11 +97,12 @@ fn len_empty_full() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn recv() {
|
|
|
|
fn recv() {
|
|
|
|
task::block_on(async {
|
|
|
|
task::block_on(async {
|
|
|
|
let (s, r) = channel(100);
|
|
|
|
let (s, r) = channel(100);
|
|
|
|
|
|
|
|
|
|
|
|
task::spawn(async move {
|
|
|
|
spawn(async move {
|
|
|
|
assert_eq!(r.recv().await.unwrap(), 7);
|
|
|
|
assert_eq!(r.recv().await.unwrap(), 7);
|
|
|
|
task::sleep(ms(1000)).await;
|
|
|
|
task::sleep(ms(1000)).await;
|
|
|
|
assert_eq!(r.recv().await.unwrap(), 8);
|
|
|
|
assert_eq!(r.recv().await.unwrap(), 8);
|
|
|
@ -107,11 +119,12 @@ fn recv() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn send() {
|
|
|
|
fn send() {
|
|
|
|
task::block_on(async {
|
|
|
|
task::block_on(async {
|
|
|
|
let (s, r) = channel(1);
|
|
|
|
let (s, r) = channel(1);
|
|
|
|
|
|
|
|
|
|
|
|
task::spawn(async move {
|
|
|
|
spawn(async move {
|
|
|
|
s.send(7).await;
|
|
|
|
s.send(7).await;
|
|
|
|
task::sleep(ms(1000)).await;
|
|
|
|
task::sleep(ms(1000)).await;
|
|
|
|
s.send(8).await;
|
|
|
|
s.send(8).await;
|
|
|
@ -129,6 +142,7 @@ fn send() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn recv_after_disconnect() {
|
|
|
|
fn recv_after_disconnect() {
|
|
|
|
task::block_on(async {
|
|
|
|
task::block_on(async {
|
|
|
|
let (s, r) = channel(100);
|
|
|
|
let (s, r) = channel(100);
|
|
|
@ -147,6 +161,7 @@ fn recv_after_disconnect() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn len() {
|
|
|
|
fn len() {
|
|
|
|
const COUNT: usize = 25_000;
|
|
|
|
const COUNT: usize = 25_000;
|
|
|
|
const CAP: usize = 1000;
|
|
|
|
const CAP: usize = 1000;
|
|
|
@ -184,7 +199,7 @@ fn len() {
|
|
|
|
assert_eq!(s.len(), 0);
|
|
|
|
assert_eq!(s.len(), 0);
|
|
|
|
assert_eq!(r.len(), 0);
|
|
|
|
assert_eq!(r.len(), 0);
|
|
|
|
|
|
|
|
|
|
|
|
let child = task::spawn({
|
|
|
|
let child = spawn({
|
|
|
|
let r = r.clone();
|
|
|
|
let r = r.clone();
|
|
|
|
async move {
|
|
|
|
async move {
|
|
|
|
for i in 0..COUNT {
|
|
|
|
for i in 0..COUNT {
|
|
|
@ -209,11 +224,12 @@ fn len() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn disconnect_wakes_receiver() {
|
|
|
|
fn disconnect_wakes_receiver() {
|
|
|
|
task::block_on(async {
|
|
|
|
task::block_on(async {
|
|
|
|
let (s, r) = channel::<()>(1);
|
|
|
|
let (s, r) = channel::<()>(1);
|
|
|
|
|
|
|
|
|
|
|
|
let child = task::spawn(async move {
|
|
|
|
let child = spawn(async move {
|
|
|
|
assert!(r.recv().await.is_err());
|
|
|
|
assert!(r.recv().await.is_err());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
@ -225,13 +241,14 @@ fn disconnect_wakes_receiver() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn spsc() {
|
|
|
|
fn spsc() {
|
|
|
|
const COUNT: usize = 100_000;
|
|
|
|
const COUNT: usize = 100_000;
|
|
|
|
|
|
|
|
|
|
|
|
task::block_on(async {
|
|
|
|
task::block_on(async {
|
|
|
|
let (s, r) = channel(3);
|
|
|
|
let (s, r) = channel(3);
|
|
|
|
|
|
|
|
|
|
|
|
let child = task::spawn(async move {
|
|
|
|
let child = spawn(async move {
|
|
|
|
for i in 0..COUNT {
|
|
|
|
for i in 0..COUNT {
|
|
|
|
assert_eq!(r.recv().await.unwrap(), i);
|
|
|
|
assert_eq!(r.recv().await.unwrap(), i);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -248,6 +265,7 @@ fn spsc() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn mpmc() {
|
|
|
|
fn mpmc() {
|
|
|
|
const COUNT: usize = 25_000;
|
|
|
|
const COUNT: usize = 25_000;
|
|
|
|
const TASKS: usize = 4;
|
|
|
|
const TASKS: usize = 4;
|
|
|
@ -262,7 +280,7 @@ fn mpmc() {
|
|
|
|
for _ in 0..TASKS {
|
|
|
|
for _ in 0..TASKS {
|
|
|
|
let r = r.clone();
|
|
|
|
let r = r.clone();
|
|
|
|
let v = v.clone();
|
|
|
|
let v = v.clone();
|
|
|
|
tasks.push(task::spawn(async move {
|
|
|
|
tasks.push(spawn(async move {
|
|
|
|
for _ in 0..COUNT {
|
|
|
|
for _ in 0..COUNT {
|
|
|
|
let n = r.recv().await.unwrap();
|
|
|
|
let n = r.recv().await.unwrap();
|
|
|
|
v[n].fetch_add(1, Ordering::SeqCst);
|
|
|
|
v[n].fetch_add(1, Ordering::SeqCst);
|
|
|
@ -272,7 +290,7 @@ fn mpmc() {
|
|
|
|
|
|
|
|
|
|
|
|
for _ in 0..TASKS {
|
|
|
|
for _ in 0..TASKS {
|
|
|
|
let s = s.clone();
|
|
|
|
let s = s.clone();
|
|
|
|
tasks.push(task::spawn(async move {
|
|
|
|
tasks.push(spawn(async move {
|
|
|
|
for i in 0..COUNT {
|
|
|
|
for i in 0..COUNT {
|
|
|
|
s.send(i).await;
|
|
|
|
s.send(i).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -290,6 +308,7 @@ fn mpmc() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn oneshot() {
|
|
|
|
fn oneshot() {
|
|
|
|
const COUNT: usize = 10_000;
|
|
|
|
const COUNT: usize = 10_000;
|
|
|
|
|
|
|
|
|
|
|
@ -297,8 +316,8 @@ fn oneshot() {
|
|
|
|
for _ in 0..COUNT {
|
|
|
|
for _ in 0..COUNT {
|
|
|
|
let (s, r) = channel(1);
|
|
|
|
let (s, r) = channel(1);
|
|
|
|
|
|
|
|
|
|
|
|
let c1 = task::spawn(async move { r.recv().await.unwrap() });
|
|
|
|
let c1 = spawn(async move { r.recv().await.unwrap() });
|
|
|
|
let c2 = task::spawn(async move { s.send(0).await });
|
|
|
|
let c2 = spawn(async move { s.send(0).await });
|
|
|
|
|
|
|
|
|
|
|
|
c1.await;
|
|
|
|
c1.await;
|
|
|
|
c2.await;
|
|
|
|
c2.await;
|
|
|
@ -307,6 +326,7 @@ fn oneshot() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
|
|
fn drops() {
|
|
|
|
fn drops() {
|
|
|
|
const RUNS: usize = 100;
|
|
|
|
const RUNS: usize = 100;
|
|
|
|
|
|
|
|
|
|
|
@ -321,17 +341,16 @@ fn drops() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _ in 0..RUNS {
|
|
|
|
for _ in 0..RUNS {
|
|
|
|
task::block_on(async {
|
|
|
|
let mut rng = rand_xorshift::XorShiftRng::seed_from_u64(0);
|
|
|
|
|
|
|
|
task::block_on(async move {
|
|
|
|
let steps = rng.gen_range(0, 10_000);
|
|
|
|
let steps = rng.gen_range(0, 10_000);
|
|
|
|
let additional = rng.gen_range(0, 50);
|
|
|
|
let additional = rng.gen_range(0, 50);
|
|
|
|
|
|
|
|
|
|
|
|
DROPS.store(0, Ordering::SeqCst);
|
|
|
|
DROPS.store(0, Ordering::SeqCst);
|
|
|
|
let (s, r) = channel::<DropCounter>(50);
|
|
|
|
let (s, r) = channel::<DropCounter>(50);
|
|
|
|
|
|
|
|
|
|
|
|
let child = task::spawn({
|
|
|
|
let child = spawn({
|
|
|
|
let r = r.clone();
|
|
|
|
let r = r.clone();
|
|
|
|
async move {
|
|
|
|
async move {
|
|
|
|
for _ in 0..steps {
|
|
|
|
for _ in 0..steps {
|
|
|
|