aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync/tests/rwlock.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-sync/tests/rwlock.rs')
-rw-r--r--embassy-sync/tests/rwlock.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/embassy-sync/tests/rwlock.rs b/embassy-sync/tests/rwlock.rs
new file mode 100644
index 000000000..301cb0492
--- /dev/null
+++ b/embassy-sync/tests/rwlock.rs
@@ -0,0 +1,81 @@
1use embassy_sync::blocking_mutex::raw::NoopRawMutex;
2use embassy_sync::rwlock::RwLock;
3use futures_executor::block_on;
4
5#[futures_test::test]
6async fn test_rwlock_read() {
7 let lock = RwLock::<NoopRawMutex, _>::new(5);
8
9 {
10 let read_guard = lock.read().await;
11 assert_eq!(*read_guard, 5);
12 }
13
14 {
15 let read_guard = lock.read().await;
16 assert_eq!(*read_guard, 5);
17 }
18}
19
20#[futures_test::test]
21async fn test_rwlock_write() {
22 let lock = RwLock::<NoopRawMutex, _>::new(5);
23
24 {
25 let mut write_guard = lock.write().await;
26 *write_guard = 10;
27 }
28
29 {
30 let read_guard = lock.read().await;
31 assert_eq!(*read_guard, 10);
32 }
33}
34
35#[futures_test::test]
36async fn test_rwlock_try_read() {
37 let lock = RwLock::<NoopRawMutex, _>::new(5);
38
39 {
40 let read_guard = lock.try_read().unwrap();
41 assert_eq!(*read_guard, 5);
42 }
43
44 {
45 let read_guard = lock.try_read().unwrap();
46 assert_eq!(*read_guard, 5);
47 }
48}
49
50#[futures_test::test]
51async fn test_rwlock_try_write() {
52 let lock = RwLock::<NoopRawMutex, _>::new(5);
53
54 {
55 let mut write_guard = lock.try_write().unwrap();
56 *write_guard = 10;
57 }
58
59 {
60 let read_guard = lock.try_read().unwrap();
61 assert_eq!(*read_guard, 10);
62 }
63}
64
65#[futures_test::test]
66async fn test_rwlock_fairness() {
67 let lock = RwLock::<NoopRawMutex, _>::new(5);
68
69 let read1 = lock.read().await;
70 let read2 = lock.read().await;
71
72 let write_fut = lock.write();
73 futures_util::pin_mut!(write_fut);
74
75 assert!(futures_util::poll!(write_fut.as_mut()).is_pending());
76
77 drop(read1);
78 drop(read2);
79
80 assert!(futures_util::poll!(write_fut.as_mut()).is_ready());
81}