Skip to content

Commit

Permalink
tests: add multi threaded version of tests/borrow_basic.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
darsto committed Apr 21, 2024
1 parent 1e0222c commit 03d973a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/borrow_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct TestObject {
}

#[test]
fn borrow_single_threaded() {
fn borrow_basic_single_thread() {
let mutex = BorrowMutex::<16, TestObject>::new();

let t1 = async {
Expand Down
78 changes: 78 additions & 0 deletions tests/borrow_basic_mt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: MIT
// Copyright(c) 2024 Darek Stojaczyk

use std::sync::Arc;
use std::time::Duration;

use futures::FutureExt;
use smol::Timer;

use borrow_mutex::BorrowMutex;

#[derive(Debug)]
struct TestObject {
counter: usize,
}

#[test]
fn borrow_basic_double_thread() {
std::env::set_var("SMOL_THREADS", "2");

let mutex = Arc::new(BorrowMutex::<16, TestObject>::new());

let t1_mutex = mutex.clone();
let t1 = smol::spawn(async move {
let mut test = TestObject { counter: 1 };

eprintln!("t1 thread: {:?}", std::thread::current());

loop {
if test.counter >= 20 {
break;
}

let mut timer = Timer::after(Duration::from_millis(100)).fuse();
let mut lender = t1_mutex.wait_for_borrow().fuse();

futures::select! {
_ = timer => {
if test.counter < 10 {
test.counter += 1;
}
eprintln!("t1: counter: {}", test.counter);
}
_ = lender => {
t1_mutex.lend(&mut test).unwrap().await;
}
}
}

t1_mutex.terminate();
if let Some(lender) = t1_mutex.lend(&mut test) {
lender.await;
};
});

let t2_mutex = mutex.clone();
let t2 = smol::spawn(async move {
eprintln!("t2 thread: {:?}", std::thread::current());

loop {
let Some(test) = t2_mutex.borrow_mut() else {
break;
};

{
let mut test = test.await;
test.counter += 1;
eprintln!("t2: counter: {}", test.counter);
}
Timer::after(Duration::from_millis(200)).await;
}
});

smol::block_on(async {
t1.await;
t2.await;
});
}

0 comments on commit 03d973a

Please sign in to comment.