M9: the browser hands each team library over as it comes, not after the whole check

exchange::libraries kept the fetches' answers until every job had run, and
Driver::follow handed them over only at Next::Done, so a library that had
answered waited behind later libraries and reports that connected or timed out.
A new Next::Tell gives the UI its events, then goes on; libraries ends each
fetch with it, before connecting for the next job, as the desktop's
library_background does.
Test: exchange_tests::a_library_check_hands_each_fetch_over_as_it_comes (the
first library is in the inbox while a later report's server never answers).
This commit is contained in:
bflatastic committed 2026-11-18 14:11:11 +07:00
1 parent 33c697e712
commit 546808a03d
4 files changed
+50 -22

No files matched your search

+6
View File
@@ -152,6 +152,12 @@ impl<S: Socket> Driver<S> {
None => self.done = true,
}
}
exchange::Next::Tell(events, then) => {
for e in events {
self.inbox.push(e);
}
self.follow(*then, now);
}
}
}
+15 -20
View File
@@ -55,6 +55,8 @@ pub(crate) enum Next {
Ask(Box<Exchange>, Then),
/// Done: tell the UI these, then join the file this invite names (`None`: nothing to join).
Done(Vec<Event>, Option<Invite>),
/// Tell the UI these now, then go on with what follows.
Tell(Vec<Event>, Box<Next>),
}
impl Exchange {
@@ -248,30 +250,23 @@ fn library_reply(answers: Result<Answers, String>) -> Result<LibraryReply, Strin
}
/// Ask about the team libraries `jobs` `(link, request)`, in order, each on a connection of its
/// own: the events their fetches end in, with the others' answers left out (a report's says
/// nothing, and an older server refuses it). `fetching` goes down at the end.
pub(crate) fn libraries(mut jobs: VecDeque<(String, LibraryReq)>, mut events: Vec<Event>, fetching: Arc<AtomicBool>) -> Next {
/// own. A fetch's answer goes to the UI as it comes, before the next connection, as the desktop's
/// does, so a slow or silent server further on holds no library back; the others' answers are left
/// out (a report's says nothing, and an older server refuses it). `fetching` goes down at the end.
pub(crate) fn libraries(mut jobs: VecDeque<(String, LibraryReq)>, fetching: Arc<AtomicBool>) -> Next {
let Some((link, req)) = jobs.pop_front() else {
fetching.store(false, Ordering::SeqCst);
return Next::Done(events, None);
return Next::Done(Vec::new(), None);
};
let fetch = matches!(req, LibraryReq::Fetch { .. });
match Invite::parse(&link) {
Err(reply) => {
if fetch {
events.push(Event::Library { link, reply: Err(reply) });
}
libraries(jobs, events, fetching)
}
Ok(invite) => Next::Ask(
Box::new(Exchange::library(invite, req)),
Box::new(move |answers| {
if fetch {
events.push(Event::Library { link, reply: library_reply(answers) });
}
libraries(jobs, events, fetching)
}),
),
let invite = Invite::parse(&link);
let then = move |reply: Result<LibraryReply, String>| {
let rest = libraries(jobs, fetching);
if fetch { Next::Tell(vec![Event::Library { link, reply }], Box::new(rest)) } else { rest }
};
match invite {
Err(e) => then(Err(e)),
Ok(invite) => Next::Ask(Box::new(Exchange::library(invite, req)), Box::new(move |answers| then(library_reply(answers)))),
}
}
+28 -1
View File
@@ -411,7 +411,7 @@ fn a_library_check_runs_in_order_and_answers_only_its_fetches() {
];
let fetching = Arc::new(AtomicBool::new(true));
let t0 = Instant::now();
let (mut d, inbox) = run(exchange::libraries(jobs.into(), Vec::new(), fetching.clone()), t0);
let (mut d, inbox) = run(exchange::libraries(jobs.into(), fetching.clone()), t0);
let ready = library(LibraryReply::Ready { access: Access::View });
d.opened(t0);
say(&mut d, &ready, t0);
@@ -429,3 +429,30 @@ fn a_library_check_runs_in_order_and_answers_only_its_fetches() {
assert!(replies[0].is_err());
assert!(matches!(replies[1], Ok(LibraryReply::Empty)));
}
/// A fetch's answer reaches the UI as it comes, not at the end of the check: a report after it
/// whose server never answers holds the first library back no longer than its own connection.
#[test]
fn a_library_check_hands_each_fetch_over_as_it_comes() {
let fetch = LibraryReq::Fetch { have: None, usage: None };
let jobs = vec![(link(), fetch.clone()), (link(), LibraryReq::ReportDetached(Detached::default())), (link(), fetch)];
let fetching = Arc::new(AtomicBool::new(true));
let t0 = Instant::now();
let (mut d, inbox) = run(exchange::libraries(jobs.into(), fetching.clone()), t0);
let fetched = |events: &[Event]| events.iter().filter(|e| matches!(e, Event::Library { .. })).count();
let ready = library(LibraryReply::Ready { access: Access::View });
d.opened(t0);
say(&mut d, &ready, t0);
say(&mut d, &library(LibraryReply::Empty), t0);
assert_eq!(fetched(&inbox.drain()), 1, "the first library is in while the report's server is asked");
// The report's connection never opens: the check gives up on it and goes on, saying nothing.
let t1 = t0 + Duration::from_secs(5);
d.tick(t1);
assert_eq!(fetched(&inbox.drain()), 0);
assert!(fetching.load(Ordering::SeqCst), "the check is still running");
d.opened(t1);
say(&mut d, &ready, t1);
say(&mut d, &library(LibraryReply::Empty), t1);
assert_eq!(fetched(&inbox.drain()), 1);
assert!(!fetching.load(Ordering::SeqCst) && d.done());
}
+1 -1
View File
@@ -247,7 +247,7 @@ impl LiveLink for NetLink {
fn library_background(&mut self, jobs: Vec<(String, LibraryReq)>) {
if !jobs.is_empty() && !self.fetching.swap(true, Ordering::SeqCst) {
self.ask(exchange::libraries(jobs.into(), Vec::new(), self.fetching.clone()));
self.ask(exchange::libraries(jobs.into(), self.fetching.clone()));
}
}