sat-rs/fsrc-core/tests/pool_test.rs

36 lines
1.3 KiB
Rust
Raw Normal View History

2022-09-10 13:34:04 +02:00
use fsrc_core::pool::{LocalPool, PoolCfg, PoolGuard, PoolProvider, StoreAddr};
2022-08-19 12:35:21 +02:00
use std::ops::DerefMut;
2022-08-20 23:21:36 +02:00
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Arc, RwLock};
use std::thread;
2022-08-19 12:35:21 +02:00
const DUMMY_DATA: [u8; 4] = [0, 1, 2, 3];
2022-08-19 12:35:21 +02:00
#[test]
fn threaded_usage() {
let pool_cfg = PoolCfg::new(vec![(16, 6), (32, 3), (8, 12)]);
2022-09-07 01:06:32 +02:00
let shared_pool = Arc::new(RwLock::new(LocalPool::new(pool_cfg)));
let shared_clone = shared_pool.clone();
2022-08-19 12:35:21 +02:00
let (tx, rx): (Sender<StoreAddr>, Receiver<StoreAddr>) = mpsc::channel();
let jh0 = thread::spawn(move || {
2022-09-07 01:06:32 +02:00
let mut dummy = shared_pool.write().unwrap();
2022-08-20 23:21:36 +02:00
let addr = dummy.add(&DUMMY_DATA).expect("Writing data failed");
tx.send(addr).expect("Sending store address failed");
});
2022-08-19 12:35:21 +02:00
let jh1 = thread::spawn(move || {
let mut pool_access = shared_clone.write().unwrap();
let addr;
{
2022-08-19 12:35:21 +02:00
addr = rx.recv().expect("Receiving store address failed");
let pg = PoolGuard::new(pool_access.deref_mut(), addr);
let read_res = pg.read().expect("Reading failed");
assert_eq!(read_res, DUMMY_DATA);
}
2022-08-19 12:35:21 +02:00
assert!(!pool_access.has_element_at(&addr).expect("Invalid address"));
});
jh0.join().unwrap();
jh1.join().unwrap();
}