lock: remove byteorder dependency from tests, use fs helper functions

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.
This commit is contained in:
Yuya Nishihara 2023-12-22 09:22:59 +09:00
parent 9de6273e10
commit 7a44e590dc
4 changed files with 9 additions and 26 deletions

1
Cargo.lock generated
View file

@ -1647,7 +1647,6 @@ dependencies = [
"async-trait",
"backoff",
"blake2",
"byteorder",
"bytes 1.5.0",
"chrono",
"config",

View file

@ -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"] }

View file

@ -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 }

View file

@ -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::*;
@ -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);
}
}