Skip to content

Commit

Permalink
Update the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
try-box committed Oct 5, 2023
1 parent 63287bd commit e59362e
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 36 deletions.
7 changes: 3 additions & 4 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "examples"
version = "0.7.0"
version = "0.9.0"
edition = "2021"

[[bin]]
Expand Down Expand Up @@ -37,16 +37,15 @@ rust-box = { path = "../", features = ["full"] }
futures = "0.3"
futures-lite = "1.12"
futures-util = "0.3"
linked-hash-map = "0.5"
indexmap = { version = "1.9", features = ["std"] }
fxhash = "0.2.1"
log = "0.4"
env_logger = "0.8.4"
parking_lot = "0.12"
crossbeam-queue = "0.3"
leaky-bucket = "0.12"
governor = "0.5"
governor = "0.6"
nonzero_ext = { version = "0.3.0", default-features = false }
rand = "0.8.4"

async-std = { version = "1.12", features = ["attributes", "unstable"] }
tokio = { version = "1", features = ["sync", "macros", "time", "rt-multi-thread"] }
Expand Down
62 changes: 60 additions & 2 deletions examples/src/channel-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use futures::{stream, AsyncWriteExt, FutureExt, Sink, SinkExt, StreamExt};
//use rust_box::mpsc::channel::{ChildReceiver, ChildSender};
use rust_box::mpsc::{
indexmap_channel, segqueue_channel, vecdeque_channel, with_segqueue_channel, Receiver,
SendError, Sender,
indexmap_channel, priority_channel, segqueue_channel, vecdeque_channel, with_segqueue_channel,
Receiver, SendError, Sender,
};
use std::time::Duration;
use tokio::task::spawn;
Expand All @@ -22,6 +22,8 @@ fn main() {
test_indexmap_channel().await;
test_with_segqueue_channel().await;
test_channel_tx_rx().await;
test_priority_channel().await;
test_priority_channel_drop().await;
};
// async_std::task::block_on(runner);
tokio::runtime::Runtime::new().unwrap().block_on(runner);
Expand Down Expand Up @@ -81,6 +83,62 @@ async fn test_channel() {
}
}

async fn test_priority_channel_drop() {
{
let (mut tx, mut rx) = priority_channel::<i32, i32>(3);
let mut tx1 = tx.clone();
let mut tx2 = tx.clone();
spawn(async move {
for i in 0..10 {
log::info!("send start ... {}", i);
let res = tx1.send((i % 10, i * 2)).await;
log::info!("send end ... {}, res: {:?}", i, res);
sleep(Duration::from_millis(1)).await;
}
//tx1.close().await;
});
sleep(Duration::from_secs(1)).await;
}
sleep(Duration::from_secs(4)).await;
}

async fn test_priority_channel() {
let (mut tx, mut rx) = priority_channel::<i32, i32>(100);

let mut tx1 = tx.clone();
let mut tx2 = tx.clone();

spawn(async move {
for i in 0..100 {
tx1.send((i % 10, i * 2)).await.unwrap();
sleep(Duration::from_millis(1)).await;
}
tx1.close().await;
});

spawn(async move {
for i in 100..200 {
tx2.send((i % 10, i * 2)).await.unwrap();
sleep(Duration::from_millis(2)).await;
}
tx2.close().await;
});

drop(tx);

let mut count = 0;
while let Some(item) = rx.next().await {
count += 1;
tokio::time::sleep(Duration::from_millis(20)).await;
log::info!(
"test priority_channel: {:?}, len: {}, count: {}",
item,
0,
count
);
}
}

async fn test_indexmap_channel() {
let (mut tx, mut rx) = indexmap_channel::<i32, i32>(100);

Expand Down
4 changes: 2 additions & 2 deletions examples/src/std-ext-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn test_atomic() {
}

fn test_async_rwlock() {
use rust_box::std_ext::async_std::RwLockExt;
use rust_box::std_ext::async_lock::RwLockExt;
let runner = async move {
let a = 1.rwlock().arc();
assert_eq!(*a.read().await, 1);
Expand All @@ -58,7 +58,7 @@ fn test_async_rwlock() {
}

fn test_async_mutex() {
use rust_box::std_ext::async_std::MutexExt;
use rust_box::std_ext::async_lock::MutexExt;
let runner = async move {
let m = 1.mutex().arc();
assert_eq!(*m.lock().await, 1);
Expand Down

0 comments on commit e59362e

Please sign in to comment.