summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio/tests/sync_semaphore.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/tokio/tests/sync_semaphore.rs
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/tokio/tests/sync_semaphore.rs')
-rw-r--r--third_party/rust/tokio/tests/sync_semaphore.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/third_party/rust/tokio/tests/sync_semaphore.rs b/third_party/rust/tokio/tests/sync_semaphore.rs
new file mode 100644
index 0000000000..1cb0c749db
--- /dev/null
+++ b/third_party/rust/tokio/tests/sync_semaphore.rs
@@ -0,0 +1,81 @@
+#![cfg(feature = "full")]
+
+use std::sync::Arc;
+use tokio::sync::Semaphore;
+
+#[test]
+fn no_permits() {
+ // this should not panic
+ Semaphore::new(0);
+}
+
+#[test]
+fn try_acquire() {
+ let sem = Semaphore::new(1);
+ {
+ let p1 = sem.try_acquire();
+ assert!(p1.is_ok());
+ let p2 = sem.try_acquire();
+ assert!(p2.is_err());
+ }
+ let p3 = sem.try_acquire();
+ assert!(p3.is_ok());
+}
+
+#[tokio::test]
+async fn acquire() {
+ let sem = Arc::new(Semaphore::new(1));
+ let p1 = sem.try_acquire().unwrap();
+ let sem_clone = sem.clone();
+ let j = tokio::spawn(async move {
+ let _p2 = sem_clone.acquire().await;
+ });
+ drop(p1);
+ j.await.unwrap();
+}
+
+#[tokio::test]
+async fn add_permits() {
+ let sem = Arc::new(Semaphore::new(0));
+ let sem_clone = sem.clone();
+ let j = tokio::spawn(async move {
+ let _p2 = sem_clone.acquire().await;
+ });
+ sem.add_permits(1);
+ j.await.unwrap();
+}
+
+#[test]
+fn forget() {
+ let sem = Arc::new(Semaphore::new(1));
+ {
+ let p = sem.try_acquire().unwrap();
+ assert_eq!(sem.available_permits(), 0);
+ p.forget();
+ assert_eq!(sem.available_permits(), 0);
+ }
+ assert_eq!(sem.available_permits(), 0);
+ assert!(sem.try_acquire().is_err());
+}
+
+#[tokio::test]
+async fn stresstest() {
+ let sem = Arc::new(Semaphore::new(5));
+ let mut join_handles = Vec::new();
+ for _ in 0..1000 {
+ let sem_clone = sem.clone();
+ join_handles.push(tokio::spawn(async move {
+ let _p = sem_clone.acquire().await;
+ }));
+ }
+ for j in join_handles {
+ j.await.unwrap();
+ }
+ // there should be exactly 5 semaphores available now
+ let _p1 = sem.try_acquire().unwrap();
+ let _p2 = sem.try_acquire().unwrap();
+ let _p3 = sem.try_acquire().unwrap();
+ let _p4 = sem.try_acquire().unwrap();
+ let _p5 = sem.try_acquire().unwrap();
+ assert!(sem.try_acquire().is_err());
+}