Echo Writes Code

file_locking.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
pub mod errors;
pub mod file_ext;

#[cfg(unix)]
mod unix;

#[cfg(windows)]
mod windows;

pub use errors::{ Error, Result };
pub use file_ext::{ FileExt };

#[cfg(test)]
mod tests {
	use super::*;

	use std::fs::{ File, self };
	use std::path::{ Path, PathBuf };

	struct TestFile {
		path: PathBuf,
		file: File,
	}

	impl TestFile {
		fn new<P: AsRef<Path>>(path: P) -> Result<TestFile> {
			let path = path.as_ref();

			let file = File::options()
				.create_new(true)
				.write(true)
				.open(path)?;

			Ok(TestFile {
				path: path.to_path_buf(),
				file,
			})
		}
	}

	impl Drop for TestFile {
		fn drop(&mut self) {
			let _ = fs::remove_file(&self.path);
		}
	}

	#[test]
	fn lock_shared_works() {
		const TEST_FILE_PATH: &str = "test_lock_shared.txt";
		let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened");
		let _guard = f.file.lock_shared().expect("Shared file lock should be obtained");
	}

	#[test]
	fn lock_exclusive_works() {
		const TEST_FILE_PATH: &str = "test_lock_exclusive.txt";
		let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened");
		let _guard = f.file.lock_exclusive().expect("Exclusive file lock should be obtained");
	}

	#[test]
	fn try_lock_shared_works() {
		const TEST_FILE_PATH: &str = "test_try_lock_shared.txt";
		let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened");
		let _guard = f.file.try_lock_shared().expect("Shared file lock should be obtained");
	}

	#[test]
	fn try_lock_exclusive_works() {
		const TEST_FILE_PATH: &str = "test_try_lock_exclusive.txt";
		let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened");
		let _guard = f.file.try_lock_shared().expect("Shared file lock should be obtained");
	}

	#[test]
	fn try_lock_exclusive_does_not_block() {
		const TEST_FILE_PATH: &str = "test_try_lock_exclusive_does_not_block.txt";
		let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened");
		let _guard = f.file.try_lock_exclusive().expect("First exclusive file lock should be obtained");
		let result = f.file.try_lock_exclusive();

		match result {
			Ok(_) => panic!("Second exclusive file lock should not be obtained"),
			Err(Error::AlreadyLocked) => {},
			Err(_) => panic!("Result of trying to obtain a second exclusive file lock should be Error::AlreadyLocked"),
		}
	}
}