1 #![warn(rust_2018_idioms)]
2 #![cfg(feature = "full")]
3 
4 use tokio::sync::Mutex;
5 use tokio::time::{interval, timeout};
6 use tokio_test::task::spawn;
7 use tokio_test::{assert_pending, assert_ready};
8 
9 use std::sync::Arc;
10 use std::time::Duration;
11 
12 #[test]
straight_execution()13 fn straight_execution() {
14     let l = Arc::new(Mutex::new(100));
15 
16     {
17         let mut t = spawn(l.clone().lock_owned());
18         let mut g = assert_ready!(t.poll());
19         assert_eq!(&*g, &100);
20         *g = 99;
21     }
22     {
23         let mut t = spawn(l.clone().lock_owned());
24         let mut g = assert_ready!(t.poll());
25         assert_eq!(&*g, &99);
26         *g = 98;
27     }
28     {
29         let mut t = spawn(l.lock_owned());
30         let g = assert_ready!(t.poll());
31         assert_eq!(&*g, &98);
32     }
33 }
34 
35 #[test]
readiness()36 fn readiness() {
37     let l = Arc::new(Mutex::new(100));
38     let mut t1 = spawn(l.clone().lock_owned());
39     let mut t2 = spawn(l.lock_owned());
40 
41     let g = assert_ready!(t1.poll());
42 
43     // We can't now acquire the lease since it's already held in g
44     assert_pending!(t2.poll());
45 
46     // But once g unlocks, we can acquire it
47     drop(g);
48     assert!(t2.is_woken());
49     assert_ready!(t2.poll());
50 }
51 
52 #[tokio::test]
53 /// Ensure a mutex is unlocked if a future holding the lock
54 /// is aborted prematurely.
aborted_future_1()55 async fn aborted_future_1() {
56     let m1: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
57     {
58         let m2 = m1.clone();
59         // Try to lock mutex in a future that is aborted prematurely
60         timeout(Duration::from_millis(1u64), async move {
61             let mut iv = interval(Duration::from_millis(1000));
62             m2.lock_owned().await;
63             iv.tick().await;
64             iv.tick().await;
65         })
66         .await
67         .unwrap_err();
68     }
69     // This should succeed as there is no lock left for the mutex.
70     timeout(Duration::from_millis(1u64), async move {
71         m1.lock_owned().await;
72     })
73     .await
74     .expect("Mutex is locked");
75 }
76 
77 #[tokio::test]
78 /// This test is similar to `aborted_future_1` but this time the
79 /// aborted future is waiting for the lock.
aborted_future_2()80 async fn aborted_future_2() {
81     let m1: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
82     {
83         // Lock mutex
84         let _lock = m1.clone().lock_owned().await;
85         {
86             let m2 = m1.clone();
87             // Try to lock mutex in a future that is aborted prematurely
88             timeout(Duration::from_millis(1u64), async move {
89                 m2.lock_owned().await;
90             })
91             .await
92             .unwrap_err();
93         }
94     }
95     // This should succeed as there is no lock left for the mutex.
96     timeout(Duration::from_millis(1u64), async move {
97         m1.lock_owned().await;
98     })
99     .await
100     .expect("Mutex is locked");
101 }
102 
103 #[test]
try_lock_owned()104 fn try_lock_owned() {
105     let m: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
106     {
107         let g1 = m.clone().try_lock_owned();
108         assert_eq!(g1.is_ok(), true);
109         let g2 = m.clone().try_lock_owned();
110         assert_eq!(g2.is_ok(), false);
111     }
112     let g3 = m.try_lock_owned();
113     assert_eq!(g3.is_ok(), true);
114 }
115 
116 #[tokio::test]
debug_format()117 async fn debug_format() {
118     let s = "debug";
119     let m = Arc::new(Mutex::new(s.to_string()));
120     assert_eq!(format!("{:?}", s), format!("{:?}", m.lock_owned().await));
121 }
122