dioxus/packages/generational-box/tests/sync.rs
Evan Almloff c7c0f4481a
Fix generation race condition with sync storage (#2638)
* Fix generation race condition with sync storage

* Document GenerationalPointer

* check generation before recycling generational box

* fix clippy

* make race_condition_regression test panic if it fails
2024-07-23 10:30:57 -07:00

42 lines
1.1 KiB
Rust

// Regression test for https://github.com/DioxusLabs/dioxus/issues/2636
use std::time::Duration;
use generational_box::{AnyStorage, GenerationalBox, SyncStorage};
#[test]
fn race_condition_regression() {
for _ in 0..100 {
let handle = {
let owner = SyncStorage::owner();
let key = owner.insert(1u64);
let handle = std::thread::spawn(move || reader(key));
std::thread::sleep(Duration::from_millis(10));
handle
// owner is dropped now
};
// owner is *recycled*
let owner = SyncStorage::owner();
let _key = owner.insert(2u64);
let _ = handle.join();
}
}
fn reader(key: GenerationalBox<u64, SyncStorage>) {
for _ in 0..1000000 {
match key.try_read() {
Ok(value) => {
if *value == 2 {
panic!("Read a new value with the old generation");
} else {
// fine
}
}
Err(err) => {
eprintln!("bailing out - {err:?}");
break;
}
}
}
}