Files
arena-tracker/crates/core/tests/import/concurrency.rs
T

49 lines
1.9 KiB
Rust

use super::*;
#[test]
fn readers_are_not_blocked_while_a_fetch_is_in_flight() {
let dir = tempfile::tempdir().unwrap();
let (entered_tx, entered_rx) = mpsc::channel();
let (release_tx, release_rx) = mpsc::channel();
let fake = FakeHistory::new(recorded_account(), recorded_matches()[..1].to_vec())
.gated(entered_tx, release_rx);
let tracker = open(&dir.path().join("arena.db"), fake);
thread::scope(|scope| {
let import = scope.spawn(|| sync(&tracker));
entered_rx.recv().unwrap();
// The import is now waiting on the source; the UI thread must still get answers.
assert_eq!(tracker.match_count(SELF_PUUID).unwrap(), 0);
assert_eq!(tracker.accounts().unwrap().len(), 1);
release_tx.send(()).unwrap();
assert_eq!(import.join().unwrap().added, 1);
});
}
#[test]
fn concurrent_syncs_run_one_after_the_other_and_fetch_each_match_once() {
let dir = tempfile::tempdir().unwrap();
let (entered_tx, entered_rx) = mpsc::channel();
let (release_tx, release_rx) = mpsc::channel();
let fake = FakeHistory::new(recorded_account(), recorded_matches()[..1].to_vec())
.gated(entered_tx, release_rx);
let fetches = fake.fetch_counter();
let tracker = open(&dir.path().join("arena.db"), fake);
let (overlapped, added) = thread::scope(|scope| {
let first = scope.spawn(|| sync(&tracker));
entered_rx.recv().unwrap();
let second = scope.spawn(|| sync(&tracker));
// Without the lock, the second sync would reach the source while the first waits there.
let overlapped = entered_rx.recv_timeout(Duration::from_millis(300)).is_ok();
release_tx.send(()).unwrap();
release_tx.send(()).unwrap();
let added = (first.join().unwrap().added, second.join().unwrap().added);
(overlapped, added)
});
assert!(!overlapped);
assert_eq!(added, (1, 0));
assert_eq!(fetches.load(Ordering::SeqCst), 1);
}