Skip to content

Commit

Permalink
chore(mutex): add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
gmoshkin committed Apr 4, 2022
1 parent 3bd8dff commit b0e0db7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
43 changes: 42 additions & 1 deletion tests/src/fiber/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use tarantool::{
fiber::{defer_proc, sleep, start, start_proc, Channel, Mutex},
fiber::{defer, defer_proc, sleep, start, start_proc, Channel, Mutex},
util::IntoClones,
};

Expand Down Expand Up @@ -103,3 +103,44 @@ pub fn advanced() {
]
);
}

pub fn lazy_static() {
use once_cell::unsync::Lazy;
use std::cell::Cell;

thread_local! {
pub static FOO: Lazy<Mutex<u32>> = Lazy::new(|| Default::default());
pub static BAR: Lazy<Cell<u32>> = Lazy::new(|| Default::default());
}

fn with_mutex() -> u32 {
FOO.with(|value| {
let mut lock = value.lock();
let v = *lock + 1;
sleep(Duration::ZERO);
*lock = v;
v
})
}

fn without_mutex() -> u32 {
BAR.with(|value| {
let v = value.get() + 1;
sleep(Duration::ZERO);
value.set(v);
v
})
}

let jh1 = defer(|| with_mutex());
let jh2 = defer(|| with_mutex());

assert_eq!(jh1.join(), 1);
assert_eq!(jh2.join(), 2);

let jh1 = defer(|| without_mutex());
let jh2 = defer(|| without_mutex());

assert_eq!(jh1.join(), 1);
assert_eq!(jh2.join(), 1);
}
1 change: 1 addition & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ fn run_tests(cfg: TestConfig) -> Result<bool, io::Error> {
fiber::mutex::try_lock,
fiber::mutex::debug,
fiber::mutex::advanced,
fiber::mutex::lazy_static,

r#box::space_get_by_name,
r#box::space_get_system,
Expand Down

0 comments on commit b0e0db7

Please sign in to comment.