Skip to content

Commit 76df945

Browse files
committed
test(shared): consolidate env-var MTU resolution tests into single function
Merge multiple parallel tests that mutate `std::env` into one sequential test to eliminate race conditions from concurrent environment variable manipulation.
1 parent bb4319c commit 76df945

1 file changed

Lines changed: 14 additions & 49 deletions

File tree

shared/src/lib.rs

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -313,19 +313,6 @@ mod tests {
313313
assert!(result.is_err());
314314
}
315315

316-
#[test]
317-
fn resolve_tun_mtu_none_returns_default() {
318-
// Env var tests run in a single function to avoid parallel race conditions
319-
// on std::env::set_var/remove_var.
320-
let prev = std::env::var("VPN_MTU").ok();
321-
std::env::remove_var("VPN_MTU");
322-
assert_eq!(resolve_tun_mtu(None), DEFAULT_TUN_MTU);
323-
// Restore
324-
if let Some(v) = prev {
325-
std::env::set_var("VPN_MTU", v);
326-
}
327-
}
328-
329316
#[test]
330317
fn resolve_tun_mtu_explicit_valid() {
331318
assert_eq!(resolve_tun_mtu(Some(1300)), 1300);
@@ -334,60 +321,38 @@ mod tests {
334321
}
335322

336323
#[test]
337-
fn resolve_tun_mtu_explicit_out_of_range_falls_through() {
324+
fn resolve_tun_mtu_env_var_scenarios() {
325+
// All env var scenarios are in a single test to avoid race conditions
326+
// from parallel tests mutating std::env concurrently.
338327
let prev = std::env::var("VPN_MTU").ok();
328+
329+
// None + no env → default
339330
std::env::remove_var("VPN_MTU");
331+
assert_eq!(resolve_tun_mtu(None), DEFAULT_TUN_MTU);
332+
333+
// Explicit out-of-range + no env → default (falls through)
340334
assert_eq!(resolve_tun_mtu(Some(500)), DEFAULT_TUN_MTU);
341335
assert_eq!(resolve_tun_mtu(Some(2000)), DEFAULT_TUN_MTU);
342336
assert_eq!(resolve_tun_mtu(Some(0)), DEFAULT_TUN_MTU);
343-
if let Some(v) = prev {
344-
std::env::set_var("VPN_MTU", v);
345-
}
346-
}
347337

348-
#[test]
349-
fn resolve_tun_mtu_env_var_fallback() {
350-
let prev = std::env::var("VPN_MTU").ok();
338+
// Env var fallback
351339
std::env::set_var("VPN_MTU", "1300");
352340
assert_eq!(resolve_tun_mtu(None), 1300);
353-
if let Some(v) = prev {
354-
std::env::set_var("VPN_MTU", v);
355-
} else {
356-
std::env::remove_var("VPN_MTU");
357-
}
358-
}
359341

360-
#[test]
361-
fn resolve_tun_mtu_explicit_takes_priority_over_env() {
362-
let prev = std::env::var("VPN_MTU").ok();
363-
std::env::set_var("VPN_MTU", "1300");
342+
// Explicit takes priority over env
364343
assert_eq!(resolve_tun_mtu(Some(1340)), 1340);
365-
if let Some(v) = prev {
366-
std::env::set_var("VPN_MTU", v);
367-
} else {
368-
std::env::remove_var("VPN_MTU");
369-
}
370-
}
371344

372-
#[test]
373-
fn resolve_tun_mtu_invalid_env_falls_through() {
374-
let prev = std::env::var("VPN_MTU").ok();
345+
// Invalid env falls through to default
375346
std::env::set_var("VPN_MTU", "not_a_number");
376347
assert_eq!(resolve_tun_mtu(None), DEFAULT_TUN_MTU);
377348
std::env::set_var("VPN_MTU", "99999");
378349
assert_eq!(resolve_tun_mtu(None), DEFAULT_TUN_MTU);
379-
if let Some(v) = prev {
380-
std::env::set_var("VPN_MTU", v);
381-
} else {
382-
std::env::remove_var("VPN_MTU");
383-
}
384-
}
385350

386-
#[test]
387-
fn resolve_tun_mtu_invalid_env_with_valid_explicit() {
388-
let prev = std::env::var("VPN_MTU").ok();
351+
// Invalid env with valid explicit → explicit wins
389352
std::env::set_var("VPN_MTU", "bad");
390353
assert_eq!(resolve_tun_mtu(Some(1300)), 1300);
354+
355+
// Restore previous env state
391356
if let Some(v) = prev {
392357
std::env::set_var("VPN_MTU", v);
393358
} else {

0 commit comments

Comments
 (0)