Skip to content

Commit

Permalink
lock: remove byteorder dependency from tests, use fs helper functions
Browse files Browse the repository at this point in the history
This is the last use of Read/WriteBytesExt. The byteorder crate is great, but
we don't need an abstraction of endianness. Let's simply use the std functions.
  • Loading branch information
yuja committed Dec 22, 2023
1 parent 9de6273 commit 7a44e59
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ assert_matches = "1.5.0"
async-trait = "0.1.75"
backoff = "0.4.0"
blake2 = "0.10.6"
byteorder = "1.5.0"
bytes = "1.5.0"
cargo_metadata = "0.17.0"
clap = { version = "4.4.11", features = ["derive", "deprecated", "wrap_help"] }
Expand Down
1 change: 0 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ version_check = { workspace = true }
async-trait = { workspace = true }
backoff = { workspace = true }
blake2 = { workspace = true }
byteorder = { workspace = true }
bytes = { workspace = true }
chrono = { workspace = true }
config = { workspace = true }
Expand Down
32 changes: 9 additions & 23 deletions lib/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ pub use platform::FileLock;
#[cfg(test)]
mod tests {
use std::cmp::max;
use std::fs::OpenOptions;
use std::thread;
use std::time::Duration;

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::{fs, thread};

use super::*;

Expand All @@ -48,32 +45,21 @@ mod tests {
let temp_dir = testutils::new_temp_dir();
let data_path = temp_dir.path().join("test");
let lock_path = temp_dir.path().join("test.lock");
let mut data_file = OpenOptions::new()
.create(true)
.write(true)
.open(data_path.clone())
.unwrap();
data_file.write_u32::<LittleEndian>(0).unwrap();
fs::write(&data_path, 0_u32.to_le_bytes()).unwrap();
let num_threads = max(num_cpus::get(), 4);
thread::scope(|s| {
for _ in 0..num_threads {
let data_path = data_path.clone();
let lock_path = lock_path.clone();
s.spawn(move || {
let _lock = FileLock::lock(lock_path);
let mut data_file = OpenOptions::new()
.read(true)
.open(data_path.clone())
.unwrap();
let value = data_file.read_u32::<LittleEndian>().unwrap();
s.spawn(|| {
let _lock = FileLock::lock(lock_path.clone());
let data = fs::read(&data_path).unwrap();
let value = u32::from_le_bytes(data.try_into().unwrap());
thread::sleep(Duration::from_millis(1));
let mut data_file = OpenOptions::new().write(true).open(data_path).unwrap();
data_file.write_u32::<LittleEndian>(value + 1).unwrap();
fs::write(&data_path, (value + 1).to_le_bytes()).unwrap();
});
}
});
let mut data_file = OpenOptions::new().read(true).open(data_path).unwrap();
let value = data_file.read_u32::<LittleEndian>().unwrap();
let data = fs::read(&data_path).unwrap();
let value = u32::from_le_bytes(data.try_into().unwrap());
assert_eq!(value, num_threads as u32);
}
}

0 comments on commit 7a44e59

Please sign in to comment.