Skip to main content

test_lock

Function test_lock 

Source
pub fn test_lock() -> LocaleTestGuard
Expand description

Serialization lock for tests that mutate the global locale. Prevents test races when multiple tests call set_locale, AND restores the original locale on guard drop so a test that flips to ZhCn doesn’t leak into the next test that assumes the default En.

Exposed unconditionally (not #[cfg(test)]-gated) because tests in downstream crates (atomcode-tuix, etc.) need to take this lock too, and cfg(test) only applies to the crate currently being tested. The lock is a OnceLock so it costs nothing at runtime until first use.

Return value is a custom guard that:

  1. Owns the underlying MutexGuard<'static, ()> so the lock is released when it drops.
  2. Captures current_locale() at construction.
  3. Restores that captured locale in its own Drop (runs BEFORE the inner MutexGuard’s Drop, since fields drop in declaration order — so the next test sees the restored locale AND the lock is still held while restoration happens).