From b7f53369af15a8781b3c4f2bb79e56e493675f6f Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 26 Mar 2024 16:21:57 +0800 Subject: [PATCH] Revert "avoid sleep" This reverts commit d7a0be1dea6dc8d0bc97283d94a12dd5da7c6076. --- Cargo.lock | 6 ++-- src/auth/Cargo.toml | 1 - .../user_provider/watch_file_user_provider.rs | 36 ++++--------------- 3 files changed, 10 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 922dee62dd86..f4c261f768e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -703,7 +703,6 @@ dependencies = [ "common-macro", "common-telemetry", "common-test-util", - "crossbeam-channel", "digest", "hex", "notify", @@ -2417,10 +2416,11 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.12" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95" +checksum = "82a9b73a36529d9c47029b9fb3a6f0ea3cc916a261195352ba19e770fc1748b2" dependencies = [ + "cfg-if 1.0.0", "crossbeam-utils", ] diff --git a/src/auth/Cargo.toml b/src/auth/Cargo.toml index 5fa4bcaa2a7f..db8c20054589 100644 --- a/src/auth/Cargo.toml +++ b/src/auth/Cargo.toml @@ -28,4 +28,3 @@ tokio.workspace = true [dev-dependencies] common-test-util.workspace = true -crossbeam-channel = "0.5.12" diff --git a/src/auth/src/user_provider/watch_file_user_provider.rs b/src/auth/src/user_provider/watch_file_user_provider.rs index 50a7cfa56618..19b6530f21a3 100644 --- a/src/auth/src/user_provider/watch_file_user_provider.rs +++ b/src/auth/src/user_provider/watch_file_user_provider.rs @@ -36,21 +36,14 @@ type WatchedCredentialRef = Arc>>>>; /// Empty file is invalid; but file not exist means every user can be authenticated. pub(crate) struct WatchFileUserProvider { users: WatchedCredentialRef, - #[cfg(test)] - notify: crossbeam_channel::Receiver<()>, } impl WatchFileUserProvider { pub fn new(filepath: &str) -> Result { - #[cfg(test)] - let (tx_notify, rx_notify) = crossbeam_channel::unbounded(); - let credential = load_credential_from_file(filepath)?; let users = Arc::new(Mutex::new(credential)); let this = WatchFileUserProvider { users: users.clone(), - #[cfg(test)] - notify: rx_notify, }; let (tx, rx) = channel::>(); @@ -73,15 +66,13 @@ impl WatchFileUserProvider { match load_credential_from_file(&filepath) { Ok(credential) => { *users.lock().expect("users credential must be valid") = credential; - info!("User provider file {filepath} reloaded"); - #[cfg(test)] - let _ = tx_notify.send(()); + info!("User provider file {filepath} reloaded") } Err(err) => { warn!( ?err, "Fail to load credential from file {filepath}; keep the old one", - ); + ) } } } @@ -126,6 +117,7 @@ pub mod test { use std::time::Duration; use common_test_util::temp_dir::create_temp_dir; + use tokio::time::sleep; use crate::user_provider::watch_file_user_provider::WatchFileUserProvider; use crate::user_provider::{Identity, Password}; @@ -148,14 +140,12 @@ pub mod test { ok, "username: {}, password: {}", username, - password, + password ); } #[tokio::test] async fn test_file_provider() { - common_telemetry::init_default_ut_logging(); - let dir = create_temp_dir("test_file_provider"); let file_path = format!("{}/test_file_provider", dir.path().to_str().unwrap()); { @@ -174,45 +164,33 @@ pub mod test { { // update the tmp file - let _ = provider.notify.try_iter().into_iter(); let file = File::create(&file_path).unwrap(); let mut lw = LineWriter::new(file); assert!(writeln!(lw, "root=654321").is_ok()); lw.flush().unwrap(); } - provider - .notify - .recv_timeout(Duration::from_secs(1)) - .unwrap(); + sleep(Duration::from_secs(2)).await; // wait the watcher to apply the change test_authenticate(&provider, "root", "123456", false).await; test_authenticate(&provider, "root", "654321", true).await; test_authenticate(&provider, "admin", "654321", false).await; { // remove the tmp file - let _ = provider.notify.try_iter().into_iter(); std::fs::remove_file(&file_path).unwrap(); } - provider - .notify - .recv_timeout(Duration::from_secs(1)) - .unwrap(); + sleep(Duration::from_secs(2)).await; // wait the watcher to apply the change test_authenticate(&provider, "root", "123456", true).await; test_authenticate(&provider, "root", "654321", true).await; test_authenticate(&provider, "admin", "654321", true).await; { // recreate the tmp file - let _ = provider.notify.try_iter().into_iter(); let file = File::create(&file_path).unwrap(); let mut lw = LineWriter::new(file); assert!(writeln!(lw, "root=123456").is_ok()); lw.flush().unwrap(); } - provider - .notify - .recv_timeout(Duration::from_secs(1)) - .unwrap(); + sleep(Duration::from_secs(2)).await; // wait the watcher to apply the change test_authenticate(&provider, "root", "123456", true).await; test_authenticate(&provider, "root", "654321", false).await; test_authenticate(&provider, "admin", "654321", false).await;