summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serial_test/src/file_lock.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/serial_test/src/file_lock.rs
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serial_test/src/file_lock.rs')
-rw-r--r--third_party/rust/serial_test/src/file_lock.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/third_party/rust/serial_test/src/file_lock.rs b/third_party/rust/serial_test/src/file_lock.rs
new file mode 100644
index 0000000000..20cd99eddb
--- /dev/null
+++ b/third_party/rust/serial_test/src/file_lock.rs
@@ -0,0 +1,86 @@
+use fslock::LockFile;
+use std::{env, fs, path::Path};
+
+struct Lock {
+ lockfile: LockFile,
+}
+
+impl Lock {
+ fn unlock(self: &mut Lock) {
+ self.lockfile.unlock().unwrap();
+ println!("Unlock");
+ }
+}
+
+fn do_lock(path: &str) -> Lock {
+ if !Path::new(path).exists() {
+ fs::write(path, "").unwrap_or_else(|_| panic!("Lock file path was {:?}", path))
+ }
+ let mut lockfile = LockFile::open(path).unwrap();
+ println!("Waiting on {:?}", path);
+ lockfile.lock().unwrap();
+ println!("Locked for {:?}", path);
+ Lock { lockfile }
+}
+
+fn path_for_name(name: &str) -> String {
+ let mut pathbuf = env::temp_dir();
+ pathbuf.push(format!("serial-test-{}", name));
+ pathbuf.into_os_string().into_string().unwrap()
+}
+
+fn make_lock_for_name_and_path(name: &str, path: Option<&str>) -> Lock {
+ if let Some(opt_path) = path {
+ do_lock(opt_path)
+ } else {
+ let default_path = path_for_name(name);
+ do_lock(&default_path)
+ }
+}
+
+#[doc(hidden)]
+pub fn fs_serial_core(name: &str, path: Option<&str>, function: fn()) {
+ let mut lock = make_lock_for_name_and_path(name, path);
+ function();
+ lock.unlock();
+}
+
+#[doc(hidden)]
+pub fn fs_serial_core_with_return<E>(
+ name: &str,
+ path: Option<&str>,
+ function: fn() -> Result<(), E>,
+) -> Result<(), E> {
+ let mut lock = make_lock_for_name_and_path(name, path);
+ let ret = function();
+ lock.unlock();
+ ret
+}
+
+#[doc(hidden)]
+pub async fn fs_async_serial_core_with_return<E>(
+ name: &str,
+ path: Option<&str>,
+ fut: impl std::future::Future<Output = Result<(), E>>,
+) -> Result<(), E> {
+ let mut lock = make_lock_for_name_and_path(name, path);
+ let ret = fut.await;
+ lock.unlock();
+ ret
+}
+
+#[doc(hidden)]
+pub async fn fs_async_serial_core(
+ name: &str,
+ path: Option<&str>,
+ fut: impl std::future::Future<Output = ()>,
+) {
+ let mut lock = make_lock_for_name_and_path(name, path);
+ fut.await;
+ lock.unlock();
+}
+
+#[test]
+fn test_serial() {
+ fs_serial_core("test", None, || {});
+}