-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace lazy_static crate with std::sync::OnceLock #159
base: master
Are you sure you want to change the base?
Conversation
Not directly related, but the use of |
I think this implementation would make the lock drop before panic which occurs when each test fails. |
I don't see how: The guard is dropped during unwinding in either case. Directly when |
I think |
Do you mean that this already happens or that an explicit call to |
Do you have the reproduction which show that they behaves the same? |
Not sure what you want to see but if you apply the following patch diff --git a/src/request/proxy.rs b/src/request/proxy.rs
index d2c81d8..cb49302 100644
--- a/src/request/proxy.rs
+++ b/src/request/proxy.rs
@@ -226,6 +226,16 @@ where
let _guard = LOCK.lock().unwrap();
+ struct ShowDrop;
+
+ impl Drop for ShowDrop {
+ fn drop(&mut self) {
+ println!("dropped");
+ }
+ }
+
+ let _show_drop = ShowDrop;
+
env::remove_var("ALL_PROXY");
env::remove_var("HTTP_PROXY");
env::remove_var("HTTPS_PROXY");
@@ -240,6 +250,12 @@ where
}
}
+#[test]
+#[should_panic]
+fn when_with_reset_proxy_vars_drops() {
+ with_reset_proxy_vars(|| panic!("in the disco"));
+}
+
#[test]
fn test_proxy_from_env_all_proxy() {
with_reset_proxy_vars(|| { you'll get running 1 test
thread 'request::proxy::when_with_reset_proxy_vars_drops' panicked at src/request/proxy.rs:256:30:
in the disco
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
dropped
test request::proxy::when_with_reset_proxy_vars_drops - should panic ... ok which stays unchanged if you additionally apply diff --git a/src/request/proxy.rs b/src/request/proxy.rs
index cb49302..6a60e08 100644
--- a/src/request/proxy.rs
+++ b/src/request/proxy.rs
@@ -241,13 +241,7 @@ where
env::remove_var("HTTPS_PROXY");
env::remove_var("NO_PROXY");
- let result = std::panic::catch_unwind(test);
-
- // teardown if ever needed
-
- if let Err(ctx) = result {
- std::panic::resume_unwind(ctx);
- }
+ test();
}
#[test] |
Thanks, I have known that behavior. In my test, without manually dropping the guard before calling resume_unwind, when one of the tests (which are called in this helper function) fails for testing, the others (which are actually successful) will go failure as well. |
But this already happens with the current code before and after this PR just due to poising, i.e. when one test fails and unwinds, the mutex is poisoned and acquiring it by the other fails (if they happen to run afterwards which is not guaranteed due to multi-threaded testing). So what might want to do is diff --git a/src/request/proxy.rs b/src/request/proxy.rs
index d2c81d8..9f18601 100644
--- a/src/request/proxy.rs
+++ b/src/request/proxy.rs
@@ -224,7 +224,7 @@ where
static ref LOCK: Mutex<()> = Mutex::new(());
};
- let _guard = LOCK.lock().unwrap();
+ let guard = LOCK.lock().unwrap();
env::remove_var("ALL_PROXY");
env::remove_var("HTTP_PROXY");
@@ -233,7 +233,7 @@ where
let result = std::panic::catch_unwind(test);
- // teardown if ever needed
+ drop(guard);
if let Err(ctx) = result {
std::panic::resume_unwind(ctx); in addition to what is proposed here. But then again, this is really only necessary if one of the tests is supposed to unwind, i.e. would only be helpful during development. |
Yes, and more precisely, manual dropping is only necessary when the test fails, so it seems better to move it just before resume_unwind. |
Personally, I would rather suggest to make the whole complexity go away. The dependency tree for the tests already contains diff --git a/Cargo.toml b/Cargo.toml
index ab7914f..27359a7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -39,6 +39,7 @@ http02 = {package = "http", version = "0.2"}
hyper = "0.14.20"
lazy_static = "1.4.0"
multipart = {version = "0.18.0", default-features = false, features = ["server"]}
+parking_lot = "0.12"
rustls-pemfile = "2"
tokio = {version = "1.20.1", features = ["full"]}
tokio-rustls = "0.25.0"
diff --git a/src/request/proxy.rs b/src/request/proxy.rs
index d2c81d8..ae24389 100644
--- a/src/request/proxy.rs
+++ b/src/request/proxy.rs
@@ -218,26 +218,18 @@ fn with_reset_proxy_vars<T>(test: T)
where
T: FnOnce() + std::panic::UnwindSafe,
{
- use std::sync::Mutex;
+ use parking_lot::{Mutex, const_mutex};
- lazy_static::lazy_static! {
- static ref LOCK: Mutex<()> = Mutex::new(());
- };
+ static LOCK: Mutex<()> = const_mutex(());
- let _guard = LOCK.lock().unwrap();
+ let _guard = LOCK.lock();
env::remove_var("ALL_PROXY");
env::remove_var("HTTP_PROXY");
env::remove_var("HTTPS_PROXY");
env::remove_var("NO_PROXY");
- let result = std::panic::catch_unwind(test);
-
- // teardown if ever needed
-
- if let Err(ctx) = result {
- std::panic::resume_unwind(ctx);
- }
+ test();
}
#[test] |
Thanks for another solution. Using |
std::sync::OnceLock
can be used instead oflazy_static
crate.