diff --git a/tests/src/fiber/mutex.rs b/tests/src/fiber/mutex.rs index b40ea189..d9b49dcd 100644 --- a/tests/src/fiber/mutex.rs +++ b/tests/src/fiber/mutex.rs @@ -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, }; @@ -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> = Lazy::new(|| Default::default()); + pub static BAR: Lazy> = 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); +} diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 317cb7f1..64e1ab53 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -406,6 +406,7 @@ fn run_tests(cfg: TestConfig) -> Result { 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,