From 7a44e590dcdd6f9613df3559572ee5ded159a9af Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 22 Dec 2023 09:22:59 +0900 Subject: [PATCH] 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. --- Cargo.lock | 1 - Cargo.toml | 1 - lib/Cargo.toml | 1 - lib/src/lock.rs | 32 +++++++++----------------------- 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4d272322c..52dbfcc49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1647,7 +1647,6 @@ dependencies = [ "async-trait", "backoff", "blake2", - "byteorder", "bytes 1.5.0", "chrono", "config", diff --git a/Cargo.toml b/Cargo.toml index fd87d5bea..e08cae120 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 9f5d65a4e..7869b42ed 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -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 } diff --git a/lib/src/lock.rs b/lib/src/lock.rs index 440387d1b..bf7b0e9d2 100644 --- a/lib/src/lock.rs +++ b/lib/src/lock.rs @@ -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::(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::().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::(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::().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); } }