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