]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/sync/test_lockorder_checks.rs
Merge pull request #2868 from orbitalturtle/export-send-onion-path
[rust-lightning] / lightning / src / sync / test_lockorder_checks.rs
1 use crate::sync::debug_sync::{RwLock, Mutex};
2
3 use super::{LockHeldState, LockTestExt};
4
5 use std::sync::Arc;
6
7 #[test]
8 #[should_panic]
9 #[cfg(not(feature = "backtrace"))]
10 fn recursive_lock_fail() {
11         let mutex = Mutex::new(());
12         let _a = mutex.lock().unwrap();
13         let _b = mutex.lock().unwrap();
14 }
15
16 #[test]
17 #[should_panic]
18 #[cfg(not(feature = "backtrace"))]
19 fn recursive_read() {
20         let lock = RwLock::new(());
21         let _a = lock.read().unwrap();
22         let _b = lock.read().unwrap();
23 }
24
25 #[test]
26 #[should_panic]
27 fn lockorder_fail() {
28         let a = Mutex::new(());
29         let b = Mutex::new(());
30         {
31                 let _a = a.lock().unwrap();
32                 let _b = b.lock().unwrap();
33         }
34         {
35                 let _b = b.lock().unwrap();
36                 let _a = a.lock().unwrap();
37         }
38 }
39
40 #[test]
41 #[should_panic]
42 fn write_lockorder_fail() {
43         let a = RwLock::new(());
44         let b = RwLock::new(());
45         {
46                 let _a = a.write().unwrap();
47                 let _b = b.write().unwrap();
48         }
49         {
50                 let _b = b.write().unwrap();
51                 let _a = a.write().unwrap();
52         }
53 }
54
55 #[test]
56 #[should_panic]
57 fn read_lockorder_fail() {
58         let a = RwLock::new(());
59         let b = RwLock::new(());
60         {
61                 let _a = a.read().unwrap();
62                 let _b = b.read().unwrap();
63         }
64         {
65                 let _b = b.read().unwrap();
66                 let _a = a.read().unwrap();
67         }
68 }
69
70 #[test]
71 #[should_panic]
72 fn read_write_lockorder_fail() {
73         let a = RwLock::new(());
74         let b = RwLock::new(());
75         {
76                 let _a = a.write().unwrap();
77                 let _b = b.read().unwrap();
78         }
79         {
80                 let _b = b.read().unwrap();
81                 let _a = a.write().unwrap();
82         }
83 }
84
85 #[test]
86 fn test_thread_locked_state() {
87         let mtx = Arc::new(Mutex::new(()));
88         let mtx_ref = Arc::clone(&mtx);
89         assert_eq!(mtx.held_by_thread(), LockHeldState::NotHeldByThread);
90
91         let lck = mtx.lock().unwrap();
92         assert_eq!(mtx.held_by_thread(), LockHeldState::HeldByThread);
93
94         let thrd = std::thread::spawn(move || {
95                 assert_eq!(mtx_ref.held_by_thread(), LockHeldState::NotHeldByThread);
96         });
97         thrd.join().unwrap();
98         assert_eq!(mtx.held_by_thread(), LockHeldState::HeldByThread);
99
100         std::mem::drop(lck);
101         assert_eq!(mtx.held_by_thread(), LockHeldState::NotHeldByThread);
102 }