From a9f201463e41a3d38f6c57104c914a2f7bff96a2 Mon Sep 17 00:00:00 2001 From: Varphone Wong Date: Sat, 20 Jan 2024 19:56:11 +0800 Subject: [PATCH] Add concurrent testing for tr! --- tests/multi_threading.rs | 42 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/multi_threading.rs b/tests/multi_threading.rs index 7d55b52..45053c9 100644 --- a/tests/multi_threading.rs +++ b/tests/multi_threading.rs @@ -2,7 +2,7 @@ use std::ops::Add; use std::thread::spawn; use std::time::{Duration, Instant}; -use rust_i18n::{set_locale, t}; +use rust_i18n::{set_locale, t, tr}; rust_i18n::i18n!("locales", fallback = "en"); @@ -32,3 +32,43 @@ fn test_load_and_store() { store.join().unwrap(); load.join().unwrap(); } + +#[test] +fn test_tr_concurrent() { + let end = Instant::now().add(Duration::from_secs(3)); + let store = spawn(move || { + let mut i = 0u32; + while Instant::now() < end { + for _ in 0..100 { + i = i.wrapping_add(1); + if i % 2 == 0 { + set_locale(&format!("en-{i}")); + } else { + set_locale(&format!("fr-{i}")); + } + } + } + }); + let tasks: Vec<_> = (0..4) + .map(|_| { + spawn(move || { + let locales = rust_i18n::available_locales!(); + let num_locales = locales.len(); + while Instant::now() < end { + for i in 0..100 { + let m = i % num_locales; + if m == 0 { + tr!("hello"); + } else { + tr!("hello", locale = locales[m]); + } + } + } + }) + }) + .collect(); + store.join().unwrap(); + for task in tasks { + task.join().unwrap(); + } +}