mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-02 20:18:38 -05:00
Tests: folder search tests wait for the walker by handshake, not by sleeping
The deadline test and the kept-files test polled the search's progress with a 2 ms sleep until the walker was blocked reading a file, which test-lint rejects. The blocking visitors now send on a channel as their read begins, and the tests wait on that channel instead.
This commit is contained in:
1 parent
5c7bceaa53
commit
e97b699b95
1 file changed
+30
-30
@@ -30,19 +30,29 @@ impl Visitor for Names {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads every file, and waits on reading one until its channel sends or closes.
|
||||
struct Blocked(Mutex<mpsc::Receiver<()>>);
|
||||
/// Reads every file, says on its second channel that a read began, and waits on reading one until
|
||||
/// its first channel sends or closes.
|
||||
struct Blocked(Mutex<mpsc::Receiver<()>>, mpsc::Sender<()>);
|
||||
|
||||
impl Visitor for Blocked {
|
||||
fn wants(&self, _: &str) -> bool {
|
||||
true
|
||||
}
|
||||
fn items(&self, _: &Path) -> Vec<usize> {
|
||||
let _ = self.1.send(());
|
||||
let _ = self.0.lock().unwrap().recv();
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
/// A [`Blocked`] visitor with the sender that lets its reads end and the receiver that hears each
|
||||
/// read begin.
|
||||
fn blocked() -> (mpsc::Sender<()>, mpsc::Receiver<()>, Arc<dyn Visitor>) {
|
||||
let (go, wait_for_go) = mpsc::channel();
|
||||
let (entered, reading) = mpsc::channel();
|
||||
(go, reading, Arc::new(Blocked(Mutex::new(wait_for_go), entered)))
|
||||
}
|
||||
|
||||
/// A test's folder tree, removed with everything in it when dropped, also when the test panics,
|
||||
/// and also when the test locked a folder in it. A removal that fails (a walker still holding a
|
||||
/// file open) is ignored.
|
||||
@@ -316,14 +326,11 @@ fn the_search_ends_at_its_limits_and_when_stopped() {
|
||||
#[test]
|
||||
fn the_search_answers_at_its_deadline_while_a_walker_is_blocked() {
|
||||
let root = tree("blocked", &["f.ttf"]);
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let (tx, reading, visitor) = blocked();
|
||||
let started = Instant::now();
|
||||
let s = begin(root.clone(), rules(&root), Limits::default(), 1, 1, Arc::new(Blocked(Mutex::new(rx))));
|
||||
let s = begin(root.clone(), rules(&root), Limits::default(), 1, 1, visitor);
|
||||
// The walker is blocked reading the file.
|
||||
let until = Instant::now() + WAIT;
|
||||
while s.progress().read == 0 && Instant::now() < until {
|
||||
std::thread::sleep(Duration::from_millis(2));
|
||||
}
|
||||
assert!(reading.recv_timeout(WAIT).is_ok(), "the walker reads the file");
|
||||
assert_eq!((s.progress().state(), s.progress().read), ("searching", 1));
|
||||
s.expire();
|
||||
let p = s.progress();
|
||||
@@ -333,23 +340,21 @@ fn the_search_answers_at_its_deadline_while_a_walker_is_blocked() {
|
||||
assert!(p.seconds <= waited, "{p:?}, {waited} s");
|
||||
assert_eq!(s.progress().seconds, p.seconds, "the time it ended at stays");
|
||||
// A limit of no seconds has run out as the search starts; a long one hasn't.
|
||||
let (_tx0, rx0) = mpsc::channel();
|
||||
let none = begin(root.clone(), rules(&root), Limits { seconds: 0.0, ..Limits::default() }, 1, 1, Arc::new(Blocked(Mutex::new(rx0))));
|
||||
let (_tx0, _, visitor) = blocked();
|
||||
let none = begin(root.clone(), rules(&root), Limits { seconds: 0.0, ..Limits::default() }, 1, 1, visitor);
|
||||
assert_eq!(none.progress().stopped(), Some("time"));
|
||||
let (tx2, rx2) = mpsc::channel();
|
||||
let s2 = begin(root.clone(), rules(&root), Limits::default(), 1, 1, Arc::new(Blocked(Mutex::new(rx2))));
|
||||
let until = Instant::now() + WAIT;
|
||||
while s2.progress().read == 0 && Instant::now() < until {
|
||||
std::thread::sleep(Duration::from_millis(2));
|
||||
}
|
||||
let (tx2, reading2, visitor) = blocked();
|
||||
let s2 = begin(root.clone(), rules(&root), Limits::default(), 1, 1, visitor);
|
||||
assert!(reading2.recv_timeout(WAIT).is_ok(), "the walker reads the file");
|
||||
assert_eq!(s2.progress().state(), "searching");
|
||||
s2.stop();
|
||||
assert_eq!(s2.progress().stopped(), Some("stop"));
|
||||
drop((tx, tx2));
|
||||
}
|
||||
|
||||
/// Every file is item 0; reading the second file waits until the channel closes.
|
||||
struct SecondBlocks(Mutex<(usize, mpsc::Receiver<()>)>);
|
||||
/// Every file is item 0; reading the second file says so on the second channel, then waits until
|
||||
/// the first channel closes.
|
||||
struct SecondBlocks(Mutex<(usize, mpsc::Receiver<()>)>, mpsc::Sender<()>);
|
||||
|
||||
impl Visitor for SecondBlocks {
|
||||
fn wants(&self, _: &str) -> bool {
|
||||
@@ -359,6 +364,7 @@ impl Visitor for SecondBlocks {
|
||||
let mut read = self.0.lock().unwrap();
|
||||
read.0 += 1;
|
||||
if read.0 == 2 {
|
||||
let _ = self.1.send(());
|
||||
let _ = read.1.recv();
|
||||
}
|
||||
vec![0]
|
||||
@@ -371,11 +377,9 @@ impl Visitor for SecondBlocks {
|
||||
fn files_found_are_kept_as_they_are_read() {
|
||||
let root = tree("kept", &["a.ttf", "b.ttf", "c.ttf"]);
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let s = begin(root.clone(), rules(&root), Limits::default(), 1, 2, Arc::new(SecondBlocks(Mutex::new((0, rx)))));
|
||||
let until = Instant::now() + WAIT;
|
||||
while s.progress().read < 2 && Instant::now() < until {
|
||||
std::thread::sleep(Duration::from_millis(2));
|
||||
}
|
||||
let (second, reading_second) = mpsc::channel();
|
||||
let s = begin(root.clone(), rules(&root), Limits::default(), 1, 2, Arc::new(SecondBlocks(Mutex::new((0, rx)), second)));
|
||||
assert!(reading_second.recv_timeout(WAIT).is_ok(), "the walker reads a second file");
|
||||
let p = s.progress();
|
||||
assert_eq!((p.state(), p.hits.len()), ("searching", 1), "{p:?}");
|
||||
s.stop();
|
||||
@@ -538,16 +542,12 @@ fn system_roots_and_homes_are_told_by_what_they_hold() {
|
||||
fn a_search_starts_with_the_threads_left_and_fails_when_none_is() {
|
||||
static POOL: Pool = Pool::new(3);
|
||||
let root = tree("pool", &["f.ttf"]);
|
||||
let blocked = || {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
(tx, Arc::new(Blocked(Mutex::new(rx))) as Arc<dyn Visitor>)
|
||||
};
|
||||
let (tx_a, a) = blocked();
|
||||
let (tx_b, b) = blocked();
|
||||
let (tx_a, _, a) = blocked();
|
||||
let (tx_b, _, b) = blocked();
|
||||
let a = start_in(&POOL, root.clone(), Some(rules(&root)), Limits::default(), 2, 1, a).unwrap();
|
||||
let b = start_in(&POOL, root.clone(), Some(rules(&root)), Limits::default(), 4, 1, b).unwrap();
|
||||
assert_eq!(POOL.live(), 3);
|
||||
let (_, c) = blocked();
|
||||
let (_, _, c) = blocked();
|
||||
let c = start_in(&POOL, root.clone(), Some(rules(&root)), Limits::default(), 1, 1, c);
|
||||
assert_eq!(c.err().as_deref(), Some("the search couldn't start: try again in a moment"));
|
||||
drop((tx_a, tx_b));
|
||||
|
||||
Reference in new issue
Block a user