1 extern crate futures;
2 extern crate tokio_mock_task;
3 extern crate tokio_sync;
4 
5 use tokio_mock_task::*;
6 use tokio_sync::lock::Lock;
7 
8 macro_rules! assert_ready {
9     ($e:expr) => {{
10         match $e {
11             futures::Async::Ready(v) => v,
12             futures::Async::NotReady => panic!("not ready"),
13         }
14     }};
15 }
16 
17 macro_rules! assert_not_ready {
18     ($e:expr) => {{
19         match $e {
20             futures::Async::NotReady => {}
21             futures::Async::Ready(v) => panic!("ready; value = {:?}", v),
22         }
23     }};
24 }
25 
26 #[test]
straight_execution()27 fn straight_execution() {
28     let mut l = Lock::new(100);
29 
30     // We can immediately acquire the lock and take the value
31     let mut g = assert_ready!(l.poll_lock());
32     assert_eq!(&*g, &100);
33     *g = 99;
34     drop(g);
35 
36     let mut g = assert_ready!(l.poll_lock());
37     assert_eq!(&*g, &99);
38     *g = 98;
39     drop(g);
40 
41     let mut g = assert_ready!(l.poll_lock());
42     assert_eq!(&*g, &98);
43 
44     // We can continue to access the guard even if the lock is dropped
45     drop(l);
46     *g = 97;
47     assert_eq!(&*g, &97);
48 }
49 
50 #[test]
readiness()51 fn readiness() {
52     let mut task = MockTask::new();
53 
54     let mut l = Lock::new(100);
55     let g = assert_ready!(l.poll_lock());
56 
57     // We can't now acquire the lease since it's already held in g
58     task.enter(|| {
59         assert_not_ready!(l.poll_lock());
60     });
61 
62     // But once g unlocks, we can acquire it
63     drop(g);
64     assert!(task.is_notified());
65     assert_ready!(l.poll_lock());
66 }
67