Skip to content

Commit

Permalink
Add concurrent testing for tr!
Browse files Browse the repository at this point in the history
  • Loading branch information
varphone committed Jan 20, 2024
1 parent 07bc184 commit a9f2014
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion tests/multi_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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();
}
}

0 comments on commit a9f2014

Please sign in to comment.