Skip to content

Commit

Permalink
Added tokio integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coder137 committed Jul 20, 2024
1 parent 545a1d8 commit 6cd106f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 79 deletions.
80 changes: 1 addition & 79 deletions src/ticked_async_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod tests {
fn test_multiple_tasks() {
let executor = TickedAsyncExecutor::default();
executor
.spawn_local("A", async move {
.spawn("A", async move {
tokio::task::yield_now().await;
})
.detach();
Expand Down Expand Up @@ -195,82 +195,4 @@ mod tests {
executor.tick();
}
}

#[test]
fn test_tokio_join() {
let executor = TickedAsyncExecutor::default();

let (tx1, mut rx1) = tokio::sync::mpsc::channel::<usize>(1);
let (tx2, mut rx2) = tokio::sync::mpsc::channel::<usize>(1);
executor
.spawn("ThreadedFuture", async move {
let (a, b) = tokio::join!(rx1.recv(), rx2.recv());
assert_eq!(a.unwrap(), 10);
assert_eq!(b.unwrap(), 20);
})
.detach();

let (tx3, mut rx3) = tokio::sync::mpsc::channel::<usize>(1);
let (tx4, mut rx4) = tokio::sync::mpsc::channel::<usize>(1);
executor
.spawn("LocalFuture", async move {
let (a, b) = tokio::join!(rx3.recv(), rx4.recv());
assert_eq!(a.unwrap(), 10);
assert_eq!(b.unwrap(), 20);
})
.detach();

tx1.try_send(10).unwrap();
tx3.try_send(10).unwrap();
for _ in 0..10 {
executor.tick();
}
tx2.try_send(20).unwrap();
tx4.try_send(20).unwrap();

while executor.num_tasks() != 0 {
executor.tick();
}
}

#[test]
fn test_tokio_select() {
let executor = TickedAsyncExecutor::default();

let (tx1, mut rx1) = tokio::sync::mpsc::channel::<usize>(1);
let (_tx2, mut rx2) = tokio::sync::mpsc::channel::<usize>(1);
executor
.spawn("ThreadedFuture", async move {
tokio::select! {
data = rx1.recv() => {
assert_eq!(data.unwrap(), 10);
}
_ = rx2.recv() => {}
}
})
.detach();

let (tx3, mut rx3) = tokio::sync::mpsc::channel::<usize>(1);
let (_tx4, mut rx4) = tokio::sync::mpsc::channel::<usize>(1);
executor
.spawn("LocalFuture", async move {
tokio::select! {
data = rx3.recv() => {
assert_eq!(data.unwrap(), 10);
}
_ = rx4.recv() => {}
}
})
.detach();

for _ in 0..10 {
executor.tick();
}

tx1.try_send(10).unwrap();
tx3.try_send(10).unwrap();
while executor.num_tasks() != 0 {
executor.tick();
}
}
}
79 changes: 79 additions & 0 deletions tests/tokio_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use ticked_async_executor::TickedAsyncExecutor;

#[test]
fn test_tokio_join() {
let executor = TickedAsyncExecutor::default();

let (tx1, mut rx1) = tokio::sync::mpsc::channel::<usize>(1);
let (tx2, mut rx2) = tokio::sync::mpsc::channel::<usize>(1);
executor
.spawn("ThreadedFuture", async move {
let (a, b) = tokio::join!(rx1.recv(), rx2.recv());
assert_eq!(a.unwrap(), 10);
assert_eq!(b.unwrap(), 20);
})
.detach();

let (tx3, mut rx3) = tokio::sync::mpsc::channel::<usize>(1);
let (tx4, mut rx4) = tokio::sync::mpsc::channel::<usize>(1);
executor
.spawn("LocalFuture", async move {
let (a, b) = tokio::join!(rx3.recv(), rx4.recv());
assert_eq!(a.unwrap(), 10);
assert_eq!(b.unwrap(), 20);
})
.detach();

tx1.try_send(10).unwrap();
tx3.try_send(10).unwrap();
for _ in 0..10 {
executor.tick();
}
tx2.try_send(20).unwrap();
tx4.try_send(20).unwrap();

while executor.num_tasks() != 0 {
executor.tick();
}
}

#[test]
fn test_tokio_select() {
let executor = TickedAsyncExecutor::default();

let (tx1, mut rx1) = tokio::sync::mpsc::channel::<usize>(1);
let (_tx2, mut rx2) = tokio::sync::mpsc::channel::<usize>(1);
executor
.spawn("ThreadedFuture", async move {
tokio::select! {
data = rx1.recv() => {
assert_eq!(data.unwrap(), 10);
}
_ = rx2.recv() => {}
}
})
.detach();

let (tx3, mut rx3) = tokio::sync::mpsc::channel::<usize>(1);
let (_tx4, mut rx4) = tokio::sync::mpsc::channel::<usize>(1);
executor
.spawn("LocalFuture", async move {
tokio::select! {
data = rx3.recv() => {
assert_eq!(data.unwrap(), 10);
}
_ = rx4.recv() => {}
}
})
.detach();

for _ in 0..10 {
executor.tick();
}

tx1.try_send(10).unwrap();
tx3.try_send(10).unwrap();
while executor.num_tasks() != 0 {
executor.tick();
}
}

0 comments on commit 6cd106f

Please sign in to comment.