Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_core/tests/lock_poison_leak.rs

1.7 KiB, 1 run

created by r1403719401:77593, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! A poisoned lock's error names where it was taken, never what it guards.
2
3use oxedyne_fe2o3_core::prelude::*;
4
5use std::{
6 sync::{
7 Arc,
8 Mutex,
9 RwLock,
10 },
11 thread,
12};
13
14const TOKEN: &str = "sk-guarded-7f3a9c1e5b";
15
16#[derive(Debug)]
17struct Key(&'static str); // Debug prints the token, as a careless type's would
18
19fn poisoned_rw() -> Arc<RwLock<Key>> {
20 let lock = Arc::new(RwLock::new(Key(TOKEN)));
21 let held = Arc::clone(&lock);
22 let _ = thread::spawn(move || {
23 let _guard = held.write();
24 panic!("poisoning the lock on purpose");
25 }).join();
26 lock
27}
28
29fn poisoned_mutex() -> Arc<Mutex<Key>> {
30 let lock = Arc::new(Mutex::new(Key(TOKEN)));
31 let held = Arc::clone(&lock);
32 let _ = thread::spawn(move || {
33 let _guard = held.lock();
34 panic!("poisoning the lock on purpose");
35 }).join();
36 lock
37}
38
39fn read_it(l: &Arc<RwLock<Key>>) -> Outcome<usize> {
40 let g = lock_read!(l);
41 Ok(g.0.len())
42}
43
44fn write_it(l: &Arc<RwLock<Key>>) -> Outcome<usize> {
45 let g = lock_write!(l);
46 Ok(g.0.len())
47}
48
49fn mutex_it(l: &Arc<Mutex<Key>>) -> Outcome<usize> {
50 let g = lock_mutex!(l);
51 Ok(g.0.len())
52}
53
54#[test]
55fn poisoned_lock_error_names_place_not_value() {
56 let rw = poisoned_rw();
57 let mx = poisoned_mutex();
58 assert!(rw.is_poisoned() && mx.is_poisoned());
59 for (what, out) in [
60 ("lock_read!", read_it(&rw)),
61 ("lock_write!", write_it(&rw)),
62 ("lock_mutex!", mutex_it(&mx)),
63 ] {
64 let e = match out {
65 Ok(_) => panic!("{} took a poisoned lock without an error", what),
66 Err(e) => e,
67 };
68 for text in [fmt!("{}", e), fmt!("{:?}", e)] {
69 assert!(!text.contains(TOKEN), "{} put the guarded value in its error: {}", what, text);
70 assert!(text.contains("lock_poison_leak.rs"), "{} does not name the place: {}", what, text);
71 }
72 }
73}