ok/jj
1
0
Fork 0
forked from mirrors/jj

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", "async-trait",
"backoff", "backoff",
"blake2", "blake2",
"byteorder",
"bytes 1.5.0", "bytes 1.5.0",
"chrono", "chrono",
"config", "config",

View file

@ -23,7 +23,6 @@ assert_matches = "1.5.0"
async-trait = "0.1.75" async-trait = "0.1.75"
backoff = "0.4.0" backoff = "0.4.0"
blake2 = "0.10.6" blake2 = "0.10.6"
byteorder = "1.5.0"
bytes = "1.5.0" bytes = "1.5.0"
cargo_metadata = "0.17.0" cargo_metadata = "0.17.0"
clap = { version = "4.4.11", features = ["derive", "deprecated", "wrap_help"] } clap = { version = "4.4.11", features = ["derive", "deprecated", "wrap_help"] }

View file

@ -22,7 +22,6 @@ version_check = { workspace = true }
async-trait = { workspace = true } async-trait = { workspace = true }
backoff = { workspace = true } backoff = { workspace = true }
blake2 = { workspace = true } blake2 = { workspace = true }
byteorder = { workspace = true }
bytes = { workspace = true } bytes = { workspace = true }
chrono = { workspace = true } chrono = { workspace = true }
config = { workspace = true } config = { workspace = true }

View file

@ -23,11 +23,8 @@ pub use platform::FileLock;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::cmp::max; use std::cmp::max;
use std::fs::OpenOptions;
use std::thread;
use std::time::Duration; use std::time::Duration;
use std::{fs, thread};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use super::*; use super::*;
@ -48,32 +45,21 @@ mod tests {
let temp_dir = testutils::new_temp_dir(); let temp_dir = testutils::new_temp_dir();
let data_path = temp_dir.path().join("test"); let data_path = temp_dir.path().join("test");
let lock_path = temp_dir.path().join("test.lock"); let lock_path = temp_dir.path().join("test.lock");
let mut data_file = OpenOptions::new() fs::write(&data_path, 0_u32.to_le_bytes()).unwrap();
.create(true)
.write(true)
.open(data_path.clone())
.unwrap();
data_file.write_u32::<LittleEndian>(0).unwrap();
let num_threads = max(num_cpus::get(), 4); let num_threads = max(num_cpus::get(), 4);
thread::scope(|s| { thread::scope(|s| {
for _ in 0..num_threads { for _ in 0..num_threads {
let data_path = data_path.clone(); s.spawn(|| {
let lock_path = lock_path.clone(); let _lock = FileLock::lock(lock_path.clone());
s.spawn(move || { let data = fs::read(&data_path).unwrap();
let _lock = FileLock::lock(lock_path); let value = u32::from_le_bytes(data.try_into().unwrap());
let mut data_file = OpenOptions::new()
.read(true)
.open(data_path.clone())
.unwrap();
let value = data_file.read_u32::<LittleEndian>().unwrap();
thread::sleep(Duration::from_millis(1)); thread::sleep(Duration::from_millis(1));
let mut data_file = OpenOptions::new().write(true).open(data_path).unwrap(); fs::write(&data_path, (value + 1).to_le_bytes()).unwrap();
data_file.write_u32::<LittleEndian>(value + 1).unwrap();
}); });
} }
}); });
let mut data_file = OpenOptions::new().read(true).open(data_path).unwrap(); let data = fs::read(&data_path).unwrap();
let value = data_file.read_u32::<LittleEndian>().unwrap(); let value = u32::from_le_bytes(data.try_into().unwrap());
assert_eq!(value, num_threads as u32); assert_eq!(value, num_threads as u32);
} }
} }