Merge pull request #1507 from ViktorTigerstrom/2022-05-store-channels-per-peer
[rust-lightning] / lightning / src / sync / test_lockorder_checks.rs
1 use crate::sync::debug_sync::{RwLock, Mutex};
2
3 #[test]
4 #[should_panic]
5 #[cfg(not(feature = "backtrace"))]
6 fn recursive_lock_fail() {
7         let mutex = Mutex::new(());
8         let _a = mutex.lock().unwrap();
9         let _b = mutex.lock().unwrap();
10 }
11
12 #[test]
13 fn recursive_read() {
14         let lock = RwLock::new(());
15         let _a = lock.read().unwrap();
16         let _b = lock.read().unwrap();
17 }
18
19 #[test]
20 #[should_panic]
21 fn lockorder_fail() {
22         let a = Mutex::new(());
23         let b = Mutex::new(());
24         {
25                 let _a = a.lock().unwrap();
26                 let _b = b.lock().unwrap();
27         }
28         {
29                 let _b = b.lock().unwrap();
30                 let _a = a.lock().unwrap();
31         }
32 }
33
34 #[test]
35 #[should_panic]
36 fn write_lockorder_fail() {
37         let a = RwLock::new(());
38         let b = RwLock::new(());
39         {
40                 let _a = a.write().unwrap();
41                 let _b = b.write().unwrap();
42         }
43         {
44                 let _b = b.write().unwrap();
45                 let _a = a.write().unwrap();
46         }
47 }
48
49 #[test]
50 #[should_panic]
51 fn read_lockorder_fail() {
52         let a = RwLock::new(());
53         let b = RwLock::new(());
54         {
55                 let _a = a.read().unwrap();
56                 let _b = b.read().unwrap();
57         }
58         {
59                 let _b = b.read().unwrap();
60                 let _a = a.read().unwrap();
61         }
62 }
63
64 #[test]
65 fn read_recursive_no_lockorder() {
66         // Like the above, but note that no lockorder is implied when we recursively read-lock a
67         // RwLock, causing this to pass just fine.
68         let a = RwLock::new(());
69         let b = RwLock::new(());
70         let _outer = a.read().unwrap();
71         {
72                 let _a = a.read().unwrap();
73                 let _b = b.read().unwrap();
74         }
75         {
76                 let _b = b.read().unwrap();
77                 let _a = a.read().unwrap();
78         }
79 }
80
81 #[test]
82 #[should_panic]
83 fn read_write_lockorder_fail() {
84         let a = RwLock::new(());
85         let b = RwLock::new(());
86         {
87                 let _a = a.write().unwrap();
88                 let _b = b.read().unwrap();
89         }
90         {
91                 let _b = b.read().unwrap();
92                 let _a = a.write().unwrap();
93         }
94 }