Skip to content

Commit eac51e2

Browse files
committed
Added stress test to force thread-safety development
1 parent 4b9f533 commit eac51e2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/core/haplotype.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,4 +933,54 @@ mod tests {
933933
assert_eq!(ht3.get_mutations().len(), 1);
934934
assert_eq!(ht3.get_mutations().get(&0), Some(&Some(0x03)));
935935
}
936+
937+
#[test]
938+
#[cfg(feature = "parallel")]
939+
fn merge_nodes_stress() {
940+
use rand::prelude::*;
941+
use std::sync::Mutex;
942+
943+
let n_sites = 7;
944+
let n_symbols = 4;
945+
946+
let bytes = vec![Some(0x00); n_sites];
947+
let wt = Wildtype::new(bytes);
948+
949+
let n_mutations = 100000;
950+
let pop_size = 11;
951+
let pop: Vec<Mutex<HaplotypeRef>> = (0..pop_size).map(|_| Mutex::new(wt.clone())).collect();
952+
953+
rayon::scope(|s| {
954+
// spawn mutator thread
955+
s.spawn(|_| {
956+
let mut rng = rand::thread_rng();
957+
958+
for i in 0..n_mutations {
959+
let from = rng.gen_range(0..pop_size);
960+
let to = rng.gen_range(0..pop_size);
961+
962+
let descendant = {
963+
let ht = pop[from].lock().expect("Failed to lock ancestor.");
964+
let pos = i % n_sites;
965+
let sym = ht.get_base(&pos).unwrap();
966+
967+
ht.create_descendant(vec![pos], vec![Some((sym + 1) % n_symbols as u8)], i)
968+
};
969+
970+
let mut field = pop[to].lock().expect("Failed to field.");
971+
*field = descendant;
972+
}
973+
});
974+
// spawn reader thread
975+
s.spawn(|_| {
976+
for _ in 0..n_mutations {
977+
let mut rng = rand::thread_rng();
978+
let index = rng.gen_range(0..pop_size);
979+
let ht = pop[index].lock().expect("Failed to lock haplotype.");
980+
let sequence = ht.get_sequence();
981+
assert_eq!(sequence.len(), n_sites);
982+
}
983+
});
984+
});
985+
}
936986
}

0 commit comments

Comments
 (0)