Skip to content

Commit b0e0db7

Browse files
committed
chore(mutex): add a test
1 parent 3bd8dff commit b0e0db7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

tests/src/fiber/mutex.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use tarantool::{
7-
fiber::{defer_proc, sleep, start, start_proc, Channel, Mutex},
7+
fiber::{defer, defer_proc, sleep, start, start_proc, Channel, Mutex},
88
util::IntoClones,
99
};
1010

@@ -103,3 +103,44 @@ pub fn advanced() {
103103
]
104104
);
105105
}
106+
107+
pub fn lazy_static() {
108+
use once_cell::unsync::Lazy;
109+
use std::cell::Cell;
110+
111+
thread_local! {
112+
pub static FOO: Lazy<Mutex<u32>> = Lazy::new(|| Default::default());
113+
pub static BAR: Lazy<Cell<u32>> = Lazy::new(|| Default::default());
114+
}
115+
116+
fn with_mutex() -> u32 {
117+
FOO.with(|value| {
118+
let mut lock = value.lock();
119+
let v = *lock + 1;
120+
sleep(Duration::ZERO);
121+
*lock = v;
122+
v
123+
})
124+
}
125+
126+
fn without_mutex() -> u32 {
127+
BAR.with(|value| {
128+
let v = value.get() + 1;
129+
sleep(Duration::ZERO);
130+
value.set(v);
131+
v
132+
})
133+
}
134+
135+
let jh1 = defer(|| with_mutex());
136+
let jh2 = defer(|| with_mutex());
137+
138+
assert_eq!(jh1.join(), 1);
139+
assert_eq!(jh2.join(), 2);
140+
141+
let jh1 = defer(|| without_mutex());
142+
let jh2 = defer(|| without_mutex());
143+
144+
assert_eq!(jh1.join(), 1);
145+
assert_eq!(jh2.join(), 1);
146+
}

tests/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ fn run_tests(cfg: TestConfig) -> Result<bool, io::Error> {
406406
fiber::mutex::try_lock,
407407
fiber::mutex::debug,
408408
fiber::mutex::advanced,
409+
fiber::mutex::lazy_static,
409410

410411
r#box::space_get_by_name,
411412
r#box::space_get_system,

0 commit comments

Comments
 (0)